switch to function local contexts
This commit is contained in:
parent
a8200b89c1
commit
84bdd02dc3
7
app.go
7
app.go
@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@ -14,12 +13,12 @@ import (
|
|||||||
type App struct {
|
type App struct {
|
||||||
tsServer *tsnet.Server
|
tsServer *tsnet.Server
|
||||||
tsLocalClient *tailscale.LocalClient
|
tsLocalClient *tailscale.LocalClient
|
||||||
ctx context.Context
|
|
||||||
queries *data.Queries
|
queries *data.Queries
|
||||||
watches *WatchResults
|
watches *WatchResults
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) getOwner(r *http.Request) (*tailcfg.Node, error) {
|
func (a *App) getOwner(r *http.Request) (*tailcfg.Node, error) {
|
||||||
|
ctx := r.Context()
|
||||||
who, err := a.tsLocalClient.WhoIs(r.Context(), r.RemoteAddr)
|
who, err := a.tsLocalClient.WhoIs(r.Context(), r.RemoteAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -27,9 +26,9 @@ func (a *App) getOwner(r *http.Request) (*tailcfg.Node, error) {
|
|||||||
|
|
||||||
ownerID := int64(who.Node.ID)
|
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 {
|
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),
|
ID: int64(who.Node.ID),
|
||||||
Name: who.Node.ComputedName,
|
Name: who.Node.ComputedName,
|
||||||
ShowShared: false,
|
ShowShared: false,
|
||||||
|
26
handlers.go
26
handlers.go
@ -7,6 +7,7 @@ import (
|
|||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
"image/png"
|
"image/png"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"unicode"
|
"unicode"
|
||||||
@ -143,7 +144,7 @@ func watchitemDELETE(w http.ResponseWriter, r *http.Request) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
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 {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
@ -165,7 +166,7 @@ func watchitemPOST(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
d.OwnerID = ownerID
|
d.OwnerID = ownerID
|
||||||
|
|
||||||
_, err := app.queries.AddWatchItem(app.ctx, *d)
|
_, err := app.queries.AddWatchItem(ctx, *d)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
|
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
|
||||||
return
|
return
|
||||||
@ -187,7 +188,7 @@ func pullrequestsPOST(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
d.OwnerID = ownerID
|
d.OwnerID = ownerID
|
||||||
|
|
||||||
_, err := app.queries.AddPullRequest(app.ctx, *d)
|
_, err := app.queries.AddPullRequest(ctx, *d)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
|
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
|
||||||
return
|
return
|
||||||
@ -205,7 +206,7 @@ func pullrequestsDELETE(w http.ResponseWriter, r *http.Request) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
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 {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
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)
|
http.Error(w, http.StatusText(http.StatusUnprocessableEntity), http.StatusUnprocessableEntity)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
prs, err := app.queries.GetAllPullRequests(app.ctx, ownerID)
|
prs, err := app.queries.GetAllPullRequests(ctx, ownerID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@ -250,7 +251,7 @@ func linksGET(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
links, err := app.queries.GetAllLinksForOwner(app.ctx, ownerID)
|
links, err := app.queries.GetAllLinksForOwner(ctx, ownerID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@ -292,7 +293,7 @@ func linksPOST(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
d.OwnerID = ownerID
|
d.OwnerID = ownerID
|
||||||
|
|
||||||
_, err := app.queries.AddLink(app.ctx, *d)
|
_, err := app.queries.AddLink(ctx, *d)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@ -306,13 +307,15 @@ func prignoreGET(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, http.StatusText(http.StatusUnprocessableEntity), http.StatusUnprocessableEntity)
|
http.Error(w, http.StatusText(http.StatusUnprocessableEntity), http.StatusUnprocessableEntity)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
prIgnores, err := app.queries.GetAllPullRequestIgnores(app.ctx, ownerID)
|
prIgnores, err := app.queries.GetAllPullRequestIgnores(ctx, ownerID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
prJson, err := json.Marshal(prIgnores)
|
prJson, err := json.Marshal(prIgnores)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -321,6 +324,7 @@ func prignoreGET(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.WriteHeader(200)
|
w.WriteHeader(200)
|
||||||
_, err = w.Write(prJson)
|
_, err = w.Write(prJson)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -337,7 +341,7 @@ func prignoreDELETE(w http.ResponseWriter, r *http.Request) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
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 {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
@ -358,7 +362,7 @@ func prignorePOST(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
d.OwnerID = ownerID
|
d.OwnerID = ownerID
|
||||||
|
|
||||||
_, err := app.queries.AddPullRequestIgnore(app.ctx, *d)
|
_, err := app.queries.AddPullRequestIgnore(ctx, *d)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@ -376,7 +380,7 @@ func linkDELETE(w http.ResponseWriter, r *http.Request) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
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 {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
6
icons.go
6
icons.go
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
@ -10,7 +11,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func updateIcons() {
|
func updateIcons() {
|
||||||
links, err := app.queries.GetAllLinks(app.ctx)
|
ctx := context.Background()
|
||||||
|
links, err := app.queries.GetAllLinks(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("can't get links: ", err)
|
log.Println("can't get links: ", err)
|
||||||
}
|
}
|
||||||
@ -38,7 +40,7 @@ func updateIcons() {
|
|||||||
}
|
}
|
||||||
contentType := resp.Header.Get("Content-Type")
|
contentType := resp.Header.Get("Content-Type")
|
||||||
|
|
||||||
err = app.queries.AddIcon(app.ctx, data.AddIconParams{
|
err = app.queries.AddIcon(ctx, data.AddIconParams{
|
||||||
OwnerID: link.OwnerID,
|
OwnerID: link.OwnerID,
|
||||||
LinkID: link.ID,
|
LinkID: link.ID,
|
||||||
ContentType: contentType,
|
ContentType: contentType,
|
||||||
|
6
main.go
6
main.go
@ -29,11 +29,10 @@ var (
|
|||||||
|
|
||||||
//go:embed assets
|
//go:embed assets
|
||||||
assets embed.FS
|
assets embed.FS
|
||||||
appCtx, cancel = context.WithTimeout(context.Background(), 3*time.Second)
|
appCtx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
)
|
)
|
||||||
|
|
||||||
var app = &App{
|
var app = &App{
|
||||||
ctx: appCtx,
|
|
||||||
tsServer: &tsnet.Server{},
|
tsServer: &tsnet.Server{},
|
||||||
tsLocalClient: &tailscale.LocalClient{},
|
tsLocalClient: &tailscale.LocalClient{},
|
||||||
}
|
}
|
||||||
@ -63,7 +62,8 @@ func main() {
|
|||||||
} else {
|
} else {
|
||||||
if _, err := os.Stat(*dbFile); os.IsNotExist(err) {
|
if _, err := os.Stat(*dbFile); os.IsNotExist(err) {
|
||||||
log.Println("Creating database..")
|
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)
|
log.Fatal("can't create database schema: ", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
12
tmp.go
12
tmp.go
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
@ -8,14 +9,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func tmpDBPopulate(db *sql.DB) error {
|
func tmpDBPopulate(db *sql.DB) error {
|
||||||
|
ctx := context.Background()
|
||||||
log.Println("CREATING TEMP DATABASE!")
|
log.Println("CREATING TEMP DATABASE!")
|
||||||
if _, err := db.ExecContext(app.ctx, schema); err != nil {
|
if _, err := db.ExecContext(ctx, schema); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
ownerID := int64(57395170551826799)
|
ownerID := int64(57395170551826799)
|
||||||
|
|
||||||
_, err := app.queries.AddOwner(app.ctx, data.AddOwnerParams{
|
_, err := app.queries.AddOwner(ctx, data.AddOwnerParams{
|
||||||
ID: 57395170551826799,
|
ID: 57395170551826799,
|
||||||
Name: "europa.humpback-trout.ts.net.",
|
Name: "europa.humpback-trout.ts.net.",
|
||||||
ShowShared: true,
|
ShowShared: true,
|
||||||
@ -23,7 +25,7 @@ func tmpDBPopulate(db *sql.DB) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, err = app.queries.AddLink(app.ctx, data.AddLinkParams{
|
_, err = app.queries.AddLink(ctx, data.AddLinkParams{
|
||||||
OwnerID: ownerID,
|
OwnerID: ownerID,
|
||||||
Url: "https://tapenet.org",
|
Url: "https://tapenet.org",
|
||||||
Name: "Tape::Net",
|
Name: "Tape::Net",
|
||||||
@ -34,7 +36,7 @@ func tmpDBPopulate(db *sql.DB) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = app.queries.AddPullRequest(app.ctx, data.AddPullRequestParams{
|
_, err = app.queries.AddPullRequest(ctx, data.AddPullRequestParams{
|
||||||
OwnerID: ownerID,
|
OwnerID: ownerID,
|
||||||
Number: 1234,
|
Number: 1234,
|
||||||
Repo: "NixOS/nixpkgs",
|
Repo: "NixOS/nixpkgs",
|
||||||
@ -42,7 +44,7 @@ func tmpDBPopulate(db *sql.DB) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, err = app.queries.AddWatchItem(app.ctx, data.AddWatchItemParams{
|
_, err = app.queries.AddWatchItem(ctx, data.AddWatchItemParams{
|
||||||
Name: "tailscale",
|
Name: "tailscale",
|
||||||
Repo: "NixOS/nixpkgs",
|
Repo: "NixOS/nixpkgs",
|
||||||
OwnerID: ownerID,
|
OwnerID: ownerID,
|
||||||
|
Loading…
Reference in New Issue
Block a user