-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-serve.go
More file actions
75 lines (58 loc) · 2.35 KB
/
Copy pathapi-serve.go
File metadata and controls
75 lines (58 loc) · 2.35 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"embed"
"fmt"
"io/fs"
"net/http"
"github.com/gorilla/mux"
)
const API_PREFIX = "/api"
const API_VERSION = "v1"
const API_PATH = API_PREFIX + "/" + API_VERSION
const clientPath = "client/dist"
//go:embed all:client/dist
var clientSource embed.FS
func Serve(p *WebUIPlugin) {
// API
api := mux.NewRouter()
api.HandleFunc("/auth", CorsHandler(p, HandleAuth(p), http.MethodPost))
api.HandleFunc("/workerGroups", CorsHandler(p, AuthValidator(p, HandleWorkerGroups(p)), http.MethodGet))
api.HandleFunc("/pipelines", CorsHandler(p, AuthValidator(p, HandlePipelines(p)), http.MethodGet))
api.HandleFunc("/pipelines/{id}", CorsHandler(p, AuthValidator(p, HandlePipeline(p)), http.MethodGet))
api.HandleFunc("/pipelines/{id}/logs", CorsHandler(p, AuthValidator(p, HandlePipelineLogs(p)), http.MethodGet))
api.HandleFunc("/actions", CorsHandler(p, AuthValidator(p, HandleActions(p)), http.MethodGet))
api.HandleFunc("/actions/{id}", CorsHandler(p, AuthValidator(p, HandleTriggerAction(p)), http.MethodPost))
api.HandleFunc("/environment", CorsHandler(p, AuthValidator(p, HandleEnvironment(p)), http.MethodGet))
api.HandleFunc("/prompts/{id}", CorsHandler(p, AuthValidator(p, HandleTriggerPrompt(p)), http.MethodPost))
http.HandleFunc(API_PATH+"/", OmitTrailingSlash(http.StripPrefix(API_PATH, api)))
http.HandleFunc(API_PREFIX+"/", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
})
// Client
clientFS, err := fs.Sub(clientSource, clientPath)
if err != nil {
panic(err)
}
http.Handle("/", BasicAuthProvider(p, http.FileServer(FileRewrite(http.FS(clientFS), "index.html"))))
// Serve
hasHTTP := p.HTTPPort != ""
hasHTTPS := p.HTTPSPort != "" && p.TLSCert != "" && p.TLSKey != ""
if !hasHTTP {
ServeHTTPS(p)
return
}
if hasHTTPS {
go ServeHTTPS(p)
}
ServeHTTP(p)
}
func ServeHTTPS(p *WebUIPlugin) {
p.Log.Info(fmt.Sprintf("listening at https://localhost:%s\n", p.HTTPSPort))
err := http.ListenAndServeTLS(fmt.Sprintf(":%s", p.HTTPSPort), p.TLSCert, p.TLSKey, nil)
p.Log.Info(fmt.Sprintf("HTTPS server exited - %s\n", err))
}
func ServeHTTP(p *WebUIPlugin) {
p.Log.Info(fmt.Sprintf("listening at http://localhost:%s\n", p.HTTPPort))
err := http.ListenAndServe(fmt.Sprintf(":%s", p.HTTPPort), nil)
p.Log.Info(fmt.Sprintf("HTTP server exited - %s\n", err))
}