SimpleTutorialHosting/internal/server/server.go
Mitchell Thompson 56635fc145 first commit
2024-10-27 07:12:56 -04:00

64 lines
1.4 KiB
Go

package server
import (
"SimpleTutorialHosting/internal/auth"
"SimpleTutorialHosting/internal/config"
"SimpleTutorialHosting/internal/handlers"
"SimpleTutorialHosting/internal/storage"
"context"
"fmt"
"net/http"
"strconv"
)
type Server struct {
server *http.Server
}
func New(cfg *config.Config, store *storage.S3Client) (*Server, error) {
h, err := handlers.New(store)
if err != nil {
return nil, fmt.Errorf("failed to create handlers: %w", err)
}
var authenticator auth.Authenticator
switch cfg.Auth.Mode {
case "dev":
authenticator, err = auth.NewDevAuthenticator(cfg.Auth.Dev)
case "saml":
authenticator, err = auth.NewSAMLAuthenticator(cfg.Auth.SAML)
default:
return nil, fmt.Errorf("invalid auth mode: %s", cfg.Auth.Mode)
}
if err != nil {
return nil, fmt.Errorf("failed to create authenticator: %w", err)
}
mux := http.NewServeMux()
mux.HandleFunc("/", h.HandleIndex)
mux.HandleFunc("/stream", h.HandleStreamVideo)
if cfg.Auth.Mode == "saml" {
samlAuth := authenticator.(*auth.SAMLAuthenticator)
mux.Handle("/saml/", samlAuth.GetMiddleware())
}
srv := &http.Server{
Addr: ":" + strconv.Itoa(cfg.Port),
Handler: mux,
}
return &Server{
server: srv,
}, nil
}
func (s *Server) Start() error {
return s.server.ListenAndServe()
}
func (s *Server) Shutdown(ctx context.Context) error {
return s.server.Shutdown(ctx)
}