2024-07-14 13:47:48 -04:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 08:41:35 -04:00
|
|
|
srv.sinks.Register("database", sinks.NewDatabaseSink())
|
|
|
|
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)
|
|
|
|
srv.setupRoutes()
|
|
|
|
|
|
|
|
return srv, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Go() error {
|
2024-07-14 21:26:53 -04:00
|
|
|
defer s.db.Close()
|
2024-07-14 17:39:03 -04:00
|
|
|
|
2024-07-14 13:47:48 -04:00
|
|
|
http.ListenAndServe(s.conf.Listen, s.r)
|
|
|
|
return nil
|
|
|
|
}
|