2024-07-14 13:47:48 -04:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-08-06 11:19:30 -04:00
|
|
|
"context"
|
2024-07-14 13:47:48 -04:00
|
|
|
"net/http"
|
|
|
|
|
2024-07-29 00:29:16 -04:00
|
|
|
"dynatron.me/x/stillbox/pkg/gordio/auth"
|
2024-07-14 13:47:48 -04:00
|
|
|
"dynatron.me/x/stillbox/pkg/gordio/config"
|
2024-07-14 17:39:03 -04:00
|
|
|
"dynatron.me/x/stillbox/pkg/gordio/database"
|
2024-08-04 00:55:28 -04:00
|
|
|
"dynatron.me/x/stillbox/pkg/gordio/nexus"
|
2024-08-01 01:01:08 -04:00
|
|
|
"dynatron.me/x/stillbox/pkg/gordio/sinks"
|
|
|
|
"dynatron.me/x/stillbox/pkg/gordio/sources"
|
2024-07-14 21:26:53 -04:00
|
|
|
"github.com/go-chi/chi/middleware"
|
2024-07-14 13:47:48 -04:00
|
|
|
"github.com/go-chi/chi/v5"
|
2024-08-14 19:12:20 -04:00
|
|
|
"github.com/go-chi/cors"
|
2024-07-14 13:47:48 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2024-08-01 01:01:08 -04:00
|
|
|
auth auth.Authenticator
|
|
|
|
conf *config.Config
|
|
|
|
db *database.DB
|
|
|
|
r *chi.Mux
|
|
|
|
sources sources.Sources
|
|
|
|
sinks sinks.Sinks
|
2024-08-04 00:55:28 -04:00
|
|
|
nex *nexus.Nexus
|
2024-07-14 13:47:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(cfg *config.Config) (*Server, error) {
|
2024-07-14 17:39:03 -04:00
|
|
|
db, err := database.NewClient(cfg.DB)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-07-14 13:47:48 -04:00
|
|
|
r := chi.NewRouter()
|
2024-08-04 09:07:31 -04:00
|
|
|
authenticator := auth.NewAuthenticator(cfg.Auth)
|
2024-07-14 13:47:48 -04:00
|
|
|
srv := &Server{
|
2024-08-01 01:01:08 -04:00
|
|
|
auth: authenticator,
|
|
|
|
conf: cfg,
|
|
|
|
db: db,
|
|
|
|
r: r,
|
2024-08-04 00:55:28 -04:00
|
|
|
nex: nexus.New(),
|
2024-07-14 13:47:48 -04:00
|
|
|
}
|
2024-08-01 01:01:08 -04:00
|
|
|
|
2024-08-04 10:56:46 -04:00
|
|
|
srv.sinks.Register("database", sinks.NewDatabaseSink(srv.db))
|
2024-08-04 08:41:35 -04:00
|
|
|
srv.sinks.Register("nexus", sinks.NewNexusSink(srv.nex))
|
2024-08-01 01:01:08 -04:00
|
|
|
srv.sources.Register("rdio-http", sources.NewRdioHTTP(authenticator, srv))
|
|
|
|
|
2024-07-14 13:47:48 -04:00
|
|
|
r.Use(middleware.RequestID)
|
|
|
|
r.Use(middleware.RealIP)
|
|
|
|
r.Use(middleware.Logger)
|
|
|
|
r.Use(middleware.Recoverer)
|
2024-08-14 19:12:20 -04:00
|
|
|
r.Use(cors.Handler(cors.Options{
|
|
|
|
AllowedOrigins: srv.conf.CORS.AllowedOrigins,
|
|
|
|
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
|
|
|
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token", "Upgrade"},
|
|
|
|
ExposedHeaders: []string{"Link"},
|
2024-08-15 13:28:03 -04:00
|
|
|
AllowCredentials: true,
|
2024-08-14 19:12:20 -04:00
|
|
|
MaxAge: 300, // Maximum value not ignored by any of major browsers
|
|
|
|
}))
|
2024-07-14 13:47:48 -04:00
|
|
|
srv.setupRoutes()
|
|
|
|
|
|
|
|
return srv, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Go() error {
|
2024-07-14 21:26:53 -04:00
|
|
|
defer s.db.Close()
|
2024-08-04 10:56:46 -04:00
|
|
|
|
2024-08-06 11:19:30 -04:00
|
|
|
ctx, cancel := context.WithCancel(database.CtxWithDB(context.Background(), s.db))
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
go s.nex.Go(ctx)
|
2024-07-14 17:39:03 -04:00
|
|
|
|
2024-08-11 14:48:17 -04:00
|
|
|
return http.ListenAndServe(s.conf.Listen, s.r)
|
2024-07-14 13:47:48 -04:00
|
|
|
}
|