gostart/app.go

43 lines
858 B
Go
Raw Normal View History

2022-11-29 19:55:00 -07:00
package main
import (
"context"
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
ctx context.Context
queries *data.Queries
watches *WatchResults
}
func (a *App) getOwner(r *http.Request) (*tailcfg.Node, error) {
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)
ownerExists, err := a.queries.GetOwner(a.ctx, ownerID)
2022-12-21 19:22:22 -07:00
if err != nil || ownerExists.ID != ownerID {
2022-12-02 20:53:05 -07:00
_, err = a.queries.AddOwner(a.ctx, data.AddOwnerParams{
ID: int64(who.Node.ID),
Name: who.Node.ComputedName,
})
if err != nil {
2022-12-05 15:45:39 -07:00
log.Fatal("adding owner failed: ", 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
}