gostart/app.go

54 lines
1.1 KiB
Go
Raw Normal View History

2022-11-29 19:55:00 -07:00
package main
import (
2022-12-02 20:53:05 -07:00
"log"
2022-11-29 19:55:00 -07:00
"net/http"
"suah.dev/gostart/data"
"tailscale.com/client/tailscale"
"tailscale.com/tailcfg"
"tailscale.com/tsnet"
)
type App struct {
tsServer *tsnet.Server
tsLocalClient *tailscale.LocalClient
queries *data.Queries
watches *WatchResults
}
func (a *App) getOwner(r *http.Request) (*tailcfg.Node, error) {
2024-05-11 14:24:49 -06:00
ctx := r.Context()
2022-11-29 19:55:00 -07:00
who, err := a.tsLocalClient.WhoIs(r.Context(), r.RemoteAddr)
if err != nil {
return nil, err
}
2022-12-02 20:53:05 -07:00
ownerID := int64(who.Node.ID)
2024-05-11 14:24:49 -06:00
ownerExists, err := a.queries.GetOwner(ctx, ownerID)
2022-12-21 19:22:22 -07:00
if err != nil || ownerExists.ID != ownerID {
2024-05-11 14:24:49 -06:00
_, err = a.queries.AddOwner(ctx, data.AddOwnerParams{
ID: int64(who.Node.ID),
Name: who.Node.ComputedName,
ShowShared: false,
2022-12-02 20:53:05 -07:00
})
if err != nil {
2024-05-10 19:28:43 -06:00
log.Printf("adding owner failed (ownerID: %#v) (ownerExists: %#v): %s", ownerID, ownerExists, err)
return nil, err
2022-12-02 20:53:05 -07:00
}
}
2022-12-04 19:03:59 -07:00
2022-11-29 19:55:00 -07:00
return who.Node, nil
}
2023-06-07 11:57:17 -06:00
func (a *App) removeWatch(id int) {
newWatches := WatchResults{}
for _, w := range *a.watches {
if w.ID != int64(id) {
newWatches = append(newWatches, w)
}
}
a.watches = &newWatches
}