-
Notifications
You must be signed in to change notification settings - Fork 85
/
server.go
72 lines (65 loc) · 2.8 KB
/
server.go
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
package publicapi
import (
"net/http"
"github.com/bacalhau-project/bacalhau/pkg/jobstore"
"github.com/bacalhau-project/bacalhau/pkg/model"
"github.com/bacalhau-project/bacalhau/pkg/publicapi"
"github.com/bacalhau-project/bacalhau/pkg/requester"
"github.com/bacalhau-project/bacalhau/pkg/storage"
sync "github.com/bacalhau-project/golang-mutex-tracer"
"github.com/gorilla/websocket"
)
const (
APIPrefix = "requester/"
ApprovalRoute = "approve"
VerifyRoute = "verify"
)
type RequesterAPIServerParams struct {
APIServer *publicapi.APIServer
Requester requester.Endpoint
DebugInfoProviders []model.DebugInfoProvider
JobStore jobstore.Store
StorageProviders storage.StorageProvider
}
type RequesterAPIServer struct {
apiServer *publicapi.APIServer
requester requester.Endpoint
debugInfoProviders []model.DebugInfoProvider
jobStore jobstore.Store
storageProviders storage.StorageProvider
// jobId or "" (for all events) -> connections for that subscription
websockets map[string][]*websocket.Conn
websocketsMutex sync.RWMutex
}
func NewRequesterAPIServer(params RequesterAPIServerParams) *RequesterAPIServer {
return &RequesterAPIServer{
apiServer: params.APIServer,
requester: params.Requester,
debugInfoProviders: params.DebugInfoProviders,
jobStore: params.JobStore,
storageProviders: params.StorageProviders,
websockets: make(map[string][]*websocket.Conn),
}
}
func (s *RequesterAPIServer) RegisterAllHandlers() error {
handlerConfigs := []publicapi.HandlerConfig{
{Path: "/" + APIPrefix + "list", Handler: http.HandlerFunc(s.list)},
{Path: "/" + APIPrefix + "states", Handler: http.HandlerFunc(s.states)},
{Path: "/" + APIPrefix + "results", Handler: http.HandlerFunc(s.results)},
{Path: "/" + APIPrefix + "events", Handler: http.HandlerFunc(s.events)},
{Path: "/" + APIPrefix + "submit", Handler: http.HandlerFunc(s.submit)},
{Path: "/" + APIPrefix + ApprovalRoute, Handler: http.HandlerFunc(s.approve)},
{Path: "/" + APIPrefix + VerifyRoute, Handler: http.HandlerFunc(s.verify)},
{Path: "/" + APIPrefix + "cancel", Handler: http.HandlerFunc(s.cancel)},
{Path: "/" + APIPrefix + "websocket/events", Handler: http.HandlerFunc(s.websocketJobEvents), Raw: true},
{Path: "/" + APIPrefix + "logs", Handler: http.HandlerFunc(s.logs), Raw: true},
{Path: "/" + APIPrefix + "debug", Handler: http.HandlerFunc(s.debug)},
}
// register URIs at root prefix for backward compatibility before migrating to API versioning
// we should remove these eventually, or have throttling limits shared across versions
err := s.apiServer.RegisterHandlers(publicapi.LegacyAPIPrefix, handlerConfigs...)
if err != nil {
return err
}
return s.apiServer.RegisterHandlers(publicapi.V1APIPrefix, handlerConfigs...)
}