summaryrefslogtreecommitdiffstats
path: root/web/web.go
blob: dc9b5e0c49794e3c7576847d421e2232a1dad620 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package web

import (
	"embed"
	"html/template"
	"net/http"

	"git.leonardobishop.net/instancer/pkg/auth"
	"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, oidcAuthProvider *auth.OIDCAuthProvider) *http.ServeMux {
	tmpl, err := template.ParseFS(views, "views/*.html")
	if err != nil {
		panic(err)
	}

	mux := http.NewServeMux()
	store := session.NewMemoryStore()
	mustAuthenticate := middleware.MustAuthenticate(tmpl, store, oidcAuthProvider)

	mux.HandleFunc("GET /auth", handler.GetAuth(tmpl, oidcAuthProvider))
	mux.HandleFunc("POST /auth", handler.PostAuth(tmpl, store, oidcAuthProvider))
	mux.HandleFunc("GET /auth/callback", handler.GetAuthCallback(tmpl, store, oidcAuthProvider))
	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
}