forked from UBAutograding/leviathan
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.go
More file actions
73 lines (64 loc) · 2.05 KB
/
api.go
File metadata and controls
73 lines (64 loc) · 2.05 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
package api
import (
"connectrpc.com/connect"
"fmt"
v1 "github.com/makeopensource/leviathan/api/v1"
"github.com/makeopensource/leviathan/common"
dkclient "github.com/makeopensource/leviathan/generated/docker_rpc/v1/v1connect"
jobClient "github.com/makeopensource/leviathan/generated/jobs/v1/v1connect"
labClient "github.com/makeopensource/leviathan/generated/labs/v1/v1connect"
"github.com/makeopensource/leviathan/service"
"github.com/rs/zerolog/log"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"net/http"
)
func StartGrpcServer() {
mux := setupEndpoints()
log.Info().Msg("Leviathan initialized successfully")
srvAddr := fmt.Sprintf(":%s", common.ServerPort.GetStr())
log.Info().Msgf("starting server on %s", srvAddr)
err := http.ListenAndServe(
srvAddr,
// Use h2c so we can serve HTTP/2 without TLS.
h2c.NewHandler(mux, &http2.Server{}),
)
if err != nil {
log.Fatal().Err(err).Msgf("Failed to start server on %s", srvAddr)
return
}
}
func setupEndpoints() *http.ServeMux {
docker, job, lab := service.InitServices()
interceptor := connect.WithInterceptors()
if common.ApiKey.GetStr() != "" {
log.Info().Msg("ApiKey is set, endpoints now require authentication")
interceptor = connect.WithInterceptors(&authInterceptor{common.ApiKey.GetStr()})
}
v1Endpoints := []func() (string, http.Handler){
// jobs endpoints
func() (string, http.Handler) {
jobSrv := v1.NewJobServer(job)
return jobClient.NewJobServiceHandler(jobSrv, interceptor)
},
// docker endpoints
func() (string, http.Handler) {
dkSrv := &v1.DockerServer{Service: docker}
return dkclient.NewDockerServiceHandler(dkSrv, interceptor)
},
func() (string, http.Handler) {
labSrv := v1.LabServer{Srv: lab}
return labClient.NewLabServiceHandler(labSrv, interceptor)
},
func() (string, http.Handler) {
fileHandler := v1.NewFileManagerHandler("/files.v1")
return fileHandler.BasePath + "/", fileHandler
},
}
mux := http.NewServeMux()
for _, svc := range v1Endpoints {
path, handler := svc()
mux.Handle(path, handler)
}
return mux
}