summaryrefslogtreecommitdiffstats
path: root/web/web.go
blob: eaf03e8c7e181f8a542c73175579c6314674964c (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
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
}