Use config in server

This commit is contained in:
Torjus Håkestad 2023-12-05 00:46:04 +01:00
parent e1e96e1e19
commit 8cb0ede570
2 changed files with 57 additions and 10 deletions

45
main.go
View File

@ -2,7 +2,9 @@ package main
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"net/http"
"os" "os"
"git.t-juice.club/torjus/ministream/server" "git.t-juice.club/torjus/ministream/server"
@ -11,6 +13,12 @@ import (
const Version = "v0.1.1" const Version = "v0.1.1"
type ctxKey string
const (
ctxKeyConfig ctxKey = "config"
)
func main() { func main() {
app := cli.App{ app := cli.App{
Name: "ministream", Name: "ministream",
@ -19,12 +27,15 @@ func main() {
{ {
Name: "serve", Name: "serve",
Usage: "Start livestreaming server.", Usage: "Start livestreaming server.",
Action: func(ctx *cli.Context) error { Before: loadConfig,
store := server.NewUserStore() Action: func(c *cli.Context) error {
cfg := configFromCtx(c.Context)
srv := server.NewServer(store) srv := server.NewServer(cfg)
srv.Addr = ":8080" srv.Addr = cfg.HTTPListenAddr
srv.ListenAndServe() if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
return err
}
return nil return nil
}, },
}, },
@ -37,3 +48,27 @@ func main() {
fmt.Println(err) fmt.Println(err)
} }
} }
func loadConfig(c *cli.Context) error {
cfg, err := server.ConfigFromDefault()
if err != nil {
return err
}
c.Context = context.WithValue(c.Context, ctxKeyConfig, cfg)
return nil
}
func configFromCtx(ctx context.Context) *server.Config {
value := ctx.Value(ctxKeyConfig)
if value == nil {
panic("unable to load config")
}
config, ok := value.(*server.Config)
if !ok {
panic("config type assertion failed")
}
return config
}

View File

@ -19,15 +19,15 @@ import (
var static embed.FS var static embed.FS
type Server struct { type Server struct {
users *UserStore
streams *StreamStore streams *StreamStore
config *Config
http.Server http.Server
} }
func NewServer(store *UserStore) *Server { func NewServer(config *Config) *Server {
srv := &Server{ srv := &Server{
users: store,
streams: NewStreamStore(), streams: NewStreamStore(),
config: config,
} }
r := chi.NewRouter() r := chi.NewRouter()
@ -41,6 +41,7 @@ func NewServer(store *UserStore) *Server {
r.Patch("/whip/{streamKey}", srv.PatchHandler) r.Patch("/whip/{streamKey}", srv.PatchHandler)
r.Post("/whip/{streamKey}", srv.PostOfferHandler) r.Post("/whip/{streamKey}", srv.PostOfferHandler)
r.Get("/stats", srv.streams.StatsHandler) r.Get("/stats", srv.streams.StatsHandler)
r.Get("/api/siteinfo", srv.InfoHandler)
srv.Handler = r srv.Handler = r
@ -55,6 +56,17 @@ func corsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(fn) return http.HandlerFunc(fn)
} }
func (s *Server) InfoHandler(w http.ResponseWriter, r *http.Request) {
var infoResponse struct {
SiteName string `json:"siteName"`
}
infoResponse.SiteName = s.config.SiteName
if err := json.NewEncoder(w).Encode(&infoResponse); err != nil {
slog.Warn("Error writing info response")
}
}
func (s *Server) OptionsHandler(w http.ResponseWriter, r *http.Request) { func (s *Server) OptionsHandler(w http.ResponseWriter, r *http.Request) {
slog.Info("Got OPTIONS") slog.Info("Got OPTIONS")
} }