switch to function local contexts

This commit is contained in:
Aaron Bieber 2024-05-11 14:24:49 -06:00
parent a8200b89c1
commit 84bdd02dc3
No known key found for this signature in database
5 changed files with 32 additions and 25 deletions

7
app.go
View File

@ -1,7 +1,6 @@
package main
import (
"context"
"log"
"net/http"
@ -14,12 +13,12 @@ import (
type App struct {
tsServer *tsnet.Server
tsLocalClient *tailscale.LocalClient
ctx context.Context
queries *data.Queries
watches *WatchResults
}
func (a *App) getOwner(r *http.Request) (*tailcfg.Node, error) {
ctx := r.Context()
who, err := a.tsLocalClient.WhoIs(r.Context(), r.RemoteAddr)
if err != nil {
return nil, err
@ -27,9 +26,9 @@ func (a *App) getOwner(r *http.Request) (*tailcfg.Node, error) {
ownerID := int64(who.Node.ID)
ownerExists, err := a.queries.GetOwner(a.ctx, ownerID)
ownerExists, err := a.queries.GetOwner(ctx, ownerID)
if err != nil || ownerExists.ID != ownerID {
_, err = a.queries.AddOwner(a.ctx, data.AddOwnerParams{
_, err = a.queries.AddOwner(ctx, data.AddOwnerParams{
ID: int64(who.Node.ID),
Name: who.Node.ComputedName,
ShowShared: false,

View File

@ -7,6 +7,7 @@ import (
"image"
"image/color"
"image/png"
"log"
"net/http"
"strconv"
"unicode"
@ -143,7 +144,7 @@ func watchitemDELETE(w http.ResponseWriter, r *http.Request) {
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
err = app.queries.DeleteWatchItem(app.ctx, data.DeleteWatchItemParams{ID: int64(watchID), OwnerID: ownerID})
err = app.queries.DeleteWatchItem(ctx, data.DeleteWatchItemParams{ID: int64(watchID), OwnerID: ownerID})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
@ -165,7 +166,7 @@ func watchitemPOST(w http.ResponseWriter, r *http.Request) {
d.OwnerID = ownerID
_, err := app.queries.AddWatchItem(app.ctx, *d)
_, err := app.queries.AddWatchItem(ctx, *d)
if err != nil {
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
return
@ -187,7 +188,7 @@ func pullrequestsPOST(w http.ResponseWriter, r *http.Request) {
d.OwnerID = ownerID
_, err := app.queries.AddPullRequest(app.ctx, *d)
_, err := app.queries.AddPullRequest(ctx, *d)
if err != nil {
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
return
@ -205,7 +206,7 @@ func pullrequestsDELETE(w http.ResponseWriter, r *http.Request) {
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
err = app.queries.DeletePullRequest(app.ctx, data.DeletePullRequestParams{ID: int64(prID), OwnerID: ownerID})
err = app.queries.DeletePullRequest(ctx, data.DeletePullRequestParams{ID: int64(prID), OwnerID: ownerID})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
@ -218,7 +219,7 @@ func pullrequestsGET(w http.ResponseWriter, r *http.Request) {
http.Error(w, http.StatusText(http.StatusUnprocessableEntity), http.StatusUnprocessableEntity)
return
}
prs, err := app.queries.GetAllPullRequests(app.ctx, ownerID)
prs, err := app.queries.GetAllPullRequests(ctx, ownerID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@ -250,7 +251,7 @@ func linksGET(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
links, err := app.queries.GetAllLinksForOwner(app.ctx, ownerID)
links, err := app.queries.GetAllLinksForOwner(ctx, ownerID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@ -292,7 +293,7 @@ func linksPOST(w http.ResponseWriter, r *http.Request) {
d.OwnerID = ownerID
_, err := app.queries.AddLink(app.ctx, *d)
_, err := app.queries.AddLink(ctx, *d)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@ -306,13 +307,15 @@ func prignoreGET(w http.ResponseWriter, r *http.Request) {
http.Error(w, http.StatusText(http.StatusUnprocessableEntity), http.StatusUnprocessableEntity)
return
}
prIgnores, err := app.queries.GetAllPullRequestIgnores(app.ctx, ownerID)
prIgnores, err := app.queries.GetAllPullRequestIgnores(ctx, ownerID)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
prJson, err := json.Marshal(prIgnores)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@ -321,6 +324,7 @@ func prignoreGET(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
_, err = w.Write(prJson)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@ -337,7 +341,7 @@ func prignoreDELETE(w http.ResponseWriter, r *http.Request) {
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
err = app.queries.DeleteIgnore(app.ctx, data.DeleteIgnoreParams{ID: int64(ignoreID), OwnerID: ownerID})
err = app.queries.DeleteIgnore(ctx, data.DeleteIgnoreParams{ID: int64(ignoreID), OwnerID: ownerID})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
@ -358,7 +362,7 @@ func prignorePOST(w http.ResponseWriter, r *http.Request) {
d.OwnerID = ownerID
_, err := app.queries.AddPullRequestIgnore(app.ctx, *d)
_, err := app.queries.AddPullRequestIgnore(ctx, *d)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@ -376,7 +380,7 @@ func linkDELETE(w http.ResponseWriter, r *http.Request) {
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
err = app.queries.DeleteLink(app.ctx, data.DeleteLinkParams{ID: int64(linkID), OwnerID: ownerID})
err = app.queries.DeleteLink(ctx, data.DeleteLinkParams{ID: int64(linkID), OwnerID: ownerID})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}

View File

@ -1,6 +1,7 @@
package main
import (
"context"
"fmt"
"io"
"log"
@ -10,7 +11,8 @@ import (
)
func updateIcons() {
links, err := app.queries.GetAllLinks(app.ctx)
ctx := context.Background()
links, err := app.queries.GetAllLinks(ctx)
if err != nil {
log.Println("can't get links: ", err)
}
@ -38,7 +40,7 @@ func updateIcons() {
}
contentType := resp.Header.Get("Content-Type")
err = app.queries.AddIcon(app.ctx, data.AddIconParams{
err = app.queries.AddIcon(ctx, data.AddIconParams{
OwnerID: link.OwnerID,
LinkID: link.ID,
ContentType: contentType,

View File

@ -29,11 +29,10 @@ var (
//go:embed assets
assets embed.FS
appCtx, cancel = context.WithTimeout(context.Background(), 3*time.Second)
appCtx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
)
var app = &App{
ctx: appCtx,
tsServer: &tsnet.Server{},
tsLocalClient: &tailscale.LocalClient{},
}
@ -63,7 +62,8 @@ func main() {
} else {
if _, err := os.Stat(*dbFile); os.IsNotExist(err) {
log.Println("Creating database..")
if _, err := db.ExecContext(app.ctx, schema); err != nil {
ctx := context.Background()
if _, err := db.ExecContext(ctx, schema); err != nil {
log.Fatal("can't create database schema: ", err)
}
}

12
tmp.go
View File

@ -1,6 +1,7 @@
package main
import (
"context"
"database/sql"
"log"
@ -8,14 +9,15 @@ import (
)
func tmpDBPopulate(db *sql.DB) error {
ctx := context.Background()
log.Println("CREATING TEMP DATABASE!")
if _, err := db.ExecContext(app.ctx, schema); err != nil {
if _, err := db.ExecContext(ctx, schema); err != nil {
return err
}
ownerID := int64(57395170551826799)
_, err := app.queries.AddOwner(app.ctx, data.AddOwnerParams{
_, err := app.queries.AddOwner(ctx, data.AddOwnerParams{
ID: 57395170551826799,
Name: "europa.humpback-trout.ts.net.",
ShowShared: true,
@ -23,7 +25,7 @@ func tmpDBPopulate(db *sql.DB) error {
if err != nil {
return err
}
_, err = app.queries.AddLink(app.ctx, data.AddLinkParams{
_, err = app.queries.AddLink(ctx, data.AddLinkParams{
OwnerID: ownerID,
Url: "https://tapenet.org",
Name: "Tape::Net",
@ -34,7 +36,7 @@ func tmpDBPopulate(db *sql.DB) error {
return err
}
_, err = app.queries.AddPullRequest(app.ctx, data.AddPullRequestParams{
_, err = app.queries.AddPullRequest(ctx, data.AddPullRequestParams{
OwnerID: ownerID,
Number: 1234,
Repo: "NixOS/nixpkgs",
@ -42,7 +44,7 @@ func tmpDBPopulate(db *sql.DB) error {
if err != nil {
return err
}
_, err = app.queries.AddWatchItem(app.ctx, data.AddWatchItemParams{
_, err = app.queries.AddWatchItem(ctx, data.AddWatchItemParams{
Name: "tailscale",
Repo: "NixOS/nixpkgs",
OwnerID: ownerID,