Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
1.6 kB · 76 lines
Go
at spaces
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677package main
import ( "context" "log/slog" "net/http" "time"
"tangled.org/bnewbold.net/cobalt/cmd/labelmaker/store"
"github.com/bluesky-social/indigo/atproto/atcrypto" "github.com/bluesky-social/indigo/atproto/labeling" "github.com/bluesky-social/indigo/atproto/syntax" "gorm.io/gorm")
type Server struct { Logger *slog.Logger Store *store.Store SigningKey atcrypto.PrivateKey ServiceDID syntax.DID AdminPassword string AuthorizedDIDs []syntax.DID Broker *Broker
httpServer *http.Server}
func NewServer(db *gorm.DB, signingKey atcrypto.PrivateKey, serviceDID syntax.DID, authDIDs []syntax.DID, adminPass string) (*Server, error) {
srv := Server{ Logger: slog.Default(), Store: store.NewStore(db), SigningKey: signingKey, ServiceDID: serviceDID, AuthorizedDIDs: authDIDs, Broker: NewBroker(), AdminPassword: adminPass, }
if err := srv.Store.MigrateDatabase(); err != nil { return nil, err } return &srv, nil}
func (srv *Server) CreateLabels(ctx context.Context, labels []labeling.Label) error {
rows, err := srv.Store.UpsertLabels(ctx, labels) if err != nil { return err }
for _, row := range rows { msg, err := rowToMessage(row) if err != nil { return err } // TODO: timeout? srv.Broker.broadcast <- *msg } return nil}
func (srv *Server) RunBroker() { srv.Broker.Run()}
func (srv *Server) Shutdown() { // graceful shutdown period ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel()
srv.Broker.Shutdown() srv.httpServer.Shutdown(ctx)}