Live video on the AT Protocol

api: implement https redirect

See merge request aquareum-tv/aquareum!20

+52 -11
+45 -7
pkg/api/api.go
··· 5 5 "encoding/json" 6 6 "fmt" 7 7 "io" 8 + "net" 8 9 "net/http" 9 10 "time" 10 11 ··· 25 26 return mux, nil 26 27 } 27 28 29 + func RedirectHandler(ctx context.Context, cli config.CLI, mod model.Model) (http.Handler, error) { 30 + _, tlsPort, err := net.SplitHostPort(cli.HttpsAddr) 31 + if err != nil { 32 + return nil, err 33 + } 34 + handleRedirect := func(w http.ResponseWriter, req *http.Request) { 35 + host, _, _ := net.SplitHostPort(req.Host) 36 + u := req.URL 37 + if tlsPort == "443" { 38 + u.Host = host 39 + } else { 40 + u.Host = net.JoinHostPort(host, tlsPort) 41 + } 42 + u.Scheme = "https" 43 + http.Redirect(w, req, u.String(), http.StatusTemporaryRedirect) 44 + } 45 + mux := http.NewServeMux() 46 + mux.HandleFunc("/", handleRedirect) 47 + return mux, nil 48 + } 49 + 28 50 type NotificationPayload struct { 29 51 Token string `json:"token"` 30 52 } ··· 55 77 } 56 78 57 79 func ServeHTTP(ctx context.Context, cli config.CLI, mod model.Model) error { 58 - return ServerWithShutdown(ctx, cli, mod, func(s *http.Server) error { 80 + handler, err := Handler(ctx, mod) 81 + if err != nil { 82 + return err 83 + } 84 + return ServerWithShutdown(ctx, handler, cli, mod, func(s *http.Server) error { 59 85 s.Addr = cli.HttpAddr 60 86 log.Log(ctx, "http server starting", "addr", s.Addr) 61 87 return s.ListenAndServe() 62 88 }) 63 89 } 64 90 91 + func ServeHTTPRedirect(ctx context.Context, cli config.CLI, mod model.Model) error { 92 + handler, err := RedirectHandler(ctx, cli, mod) 93 + if err != nil { 94 + return err 95 + } 96 + return ServerWithShutdown(ctx, handler, cli, mod, func(s *http.Server) error { 97 + s.Addr = cli.HttpAddr 98 + log.Log(ctx, "http tls redirecct server starting", "addr", s.Addr) 99 + return s.ListenAndServe() 100 + }) 101 + } 102 + 65 103 func ServeHTTPS(ctx context.Context, cli config.CLI, mod model.Model) error { 66 - return ServerWithShutdown(ctx, cli, mod, func(s *http.Server) error { 104 + handler, err := Handler(ctx, mod) 105 + if err != nil { 106 + return err 107 + } 108 + return ServerWithShutdown(ctx, handler, cli, mod, func(s *http.Server) error { 67 109 s.Addr = cli.HttpsAddr 68 110 log.Log(ctx, "https server starting", 69 111 "addr", s.Addr, ··· 74 116 }) 75 117 } 76 118 77 - func ServerWithShutdown(ctx context.Context, cli config.CLI, mod model.Model, serve func(*http.Server) error) error { 78 - handler, err := Handler(ctx, mod) 79 - if err != nil { 80 - return err 81 - } 119 + func ServerWithShutdown(ctx context.Context, handler http.Handler, cli config.CLI, mod model.Model, serve func(*http.Server) error) error { 82 120 ctx, cancel := context.WithCancel(ctx) 83 121 server := http.Server{Handler: handler} 84 122 var serveErr error
+7 -4
pkg/cmd/aquareum.go
··· 70 70 return handleSignals(ctx) 71 71 }) 72 72 73 - group.Go(func() error { 74 - return api.ServeHTTP(ctx, cli, mod) 75 - }) 76 - 77 73 if !cli.Insecure { 78 74 group.Go(func() error { 79 75 return api.ServeHTTPS(ctx, cli, mod) 76 + }) 77 + group.Go(func() error { 78 + return api.ServeHTTPRedirect(ctx, cli, mod) 79 + }) 80 + } else { 81 + group.Go(func() error { 82 + return api.ServeHTTP(ctx, cli, mod) 80 83 }) 81 84 } 82 85