package web
import (
"embed"
"html/template"
"net/http"
"git.leonardobishop.net/instancer/pkg/deployer"
"git.leonardobishop.net/instancer/pkg/registry"
"git.leonardobishop.net/instancer/pkg/session"
"git.leonardobishop.net/instancer/web/handler"
"git.leonardobishop.net/instancer/web/middleware"
)
//go:embed views
var views embed.FS
func NewMux(registryClient *registry.RegistryClient, dockerDeployer *deployer.DockerDeployer) *http.ServeMux {
tmpl, err := template.ParseFS(views, "views/*.html")
if err != nil {
panic(err)
}
mux := http.NewServeMux()
store := session.NewMemoryStore()
mustAuthenticate := middleware.MustAuthenticate(store)
mux.HandleFunc("GET /auth", handler.GetAuth(tmpl))
mux.HandleFunc("POST /auth", handler.PostAuth(tmpl, store))
mux.HandleFunc("GET /logout", handler.GetLogout(store))
mux.HandleFunc("GET /", mustAuthenticate(handler.GetIndex(tmpl, registryClient)))
mux.HandleFunc("POST /deploy", mustAuthenticate(handler.PostDeploy(tmpl, dockerDeployer)))
mux.HandleFunc("GET /deploy/stream", mustAuthenticate(handler.DeploySSE(tmpl, registryClient, dockerDeployer)))
mux.HandleFunc("GET /instances", mustAuthenticate(handler.GetInstances(tmpl, dockerDeployer)))
mux.HandleFunc("POST /instances/{deployKey}/stop", mustAuthenticate(handler.PostStopInstance(tmpl, dockerDeployer)))
return mux
}