-
Notifications
You must be signed in to change notification settings - Fork 132
/
application.go
137 lines (122 loc) · 4.69 KB
/
application.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//go:generate go run ./generate/main.go
package lookoutv2
import (
"github.com/go-openapi/loads"
"github.com/go-openapi/runtime/middleware"
log "github.com/sirupsen/logrus"
"github.com/armadaproject/armada/internal/common/compress"
"github.com/armadaproject/armada/internal/common/database"
"github.com/armadaproject/armada/internal/common/util"
"github.com/armadaproject/armada/internal/lookoutv2/configuration"
"github.com/armadaproject/armada/internal/lookoutv2/conversions"
"github.com/armadaproject/armada/internal/lookoutv2/gen/restapi"
"github.com/armadaproject/armada/internal/lookoutv2/gen/restapi/operations"
"github.com/armadaproject/armada/internal/lookoutv2/repository"
)
func Serve(configuration configuration.LookoutV2Configuration) error {
// load embedded swagger file
swaggerSpec, err := loads.Analyzed(restapi.SwaggerJSON, "")
if err != nil {
return err
}
db, err := database.OpenPgxPool(configuration.Postgres)
if err != nil {
return err
}
getJobsRepo := repository.NewSqlGetJobsRepository(db)
groupJobsRepo := repository.NewSqlGroupJobsRepository(db)
decompressor := compress.NewThreadSafeZlibDecompressor()
getJobRunErrorRepo := repository.NewSqlGetJobRunErrorRepository(db, decompressor)
getJobSpecRepo := repository.NewSqlGetJobSpecRepository(db, decompressor)
// create new service API
api := operations.NewLookoutAPI(swaggerSpec)
api.GetHealthHandler = operations.GetHealthHandlerFunc(
func(params operations.GetHealthParams) middleware.Responder {
return operations.NewGetHealthOK().WithPayload("Health check passed")
},
)
api.GetJobsHandler = operations.GetJobsHandlerFunc(
func(params operations.GetJobsParams) middleware.Responder {
filters := util.Map(params.GetJobsRequest.Filters, conversions.FromSwaggerFilter)
order := conversions.FromSwaggerOrder(params.GetJobsRequest.Order)
skip := 0
if params.GetJobsRequest.Skip != nil {
skip = int(*params.GetJobsRequest.Skip)
}
result, err := getJobsRepo.GetJobs(
params.HTTPRequest.Context(),
filters,
order,
skip,
int(params.GetJobsRequest.Take))
if err != nil {
return operations.NewGetJobsBadRequest().WithPayload(conversions.ToSwaggerError(err.Error()))
}
return operations.NewGetJobsOK().WithPayload(&operations.GetJobsOKBody{
Count: int64(result.Count),
Jobs: util.Map(result.Jobs, conversions.ToSwaggerJob),
})
},
)
api.GroupJobsHandler = operations.GroupJobsHandlerFunc(
func(params operations.GroupJobsParams) middleware.Responder {
filters := util.Map(params.GroupJobsRequest.Filters, conversions.FromSwaggerFilter)
order := conversions.FromSwaggerOrder(params.GroupJobsRequest.Order)
skip := 0
if params.GroupJobsRequest.Skip != nil {
skip = int(*params.GroupJobsRequest.Skip)
}
result, err := groupJobsRepo.GroupBy(
params.HTTPRequest.Context(),
filters,
order,
conversions.FromSwaggerGroupedField(params.GroupJobsRequest.GroupedField),
params.GroupJobsRequest.Aggregates,
skip,
int(params.GroupJobsRequest.Take))
if err != nil {
return operations.NewGroupJobsBadRequest().WithPayload(conversions.ToSwaggerError(err.Error()))
}
return operations.NewGroupJobsOK().WithPayload(&operations.GroupJobsOKBody{
Count: int64(result.Count),
Groups: util.Map(result.Groups, conversions.ToSwaggerGroup),
})
},
)
api.GetJobRunErrorHandler = operations.GetJobRunErrorHandlerFunc(
func(params operations.GetJobRunErrorParams) middleware.Responder {
result, err := getJobRunErrorRepo.GetJobRunError(params.HTTPRequest.Context(), params.GetJobRunErrorRequest.RunID)
if err != nil {
return operations.NewGetJobRunErrorBadRequest().WithPayload(conversions.ToSwaggerError(err.Error()))
}
return operations.NewGetJobRunErrorOK().WithPayload(&operations.GetJobRunErrorOKBody{
ErrorString: result,
})
},
)
api.GetJobSpecHandler = operations.GetJobSpecHandlerFunc(
func(params operations.GetJobSpecParams) middleware.Responder {
result, err := getJobSpecRepo.GetJobSpec(params.HTTPRequest.Context(), params.GetJobSpecRequest.JobID)
if err != nil {
return operations.NewGetJobSpecBadRequest().WithPayload(conversions.ToSwaggerError(err.Error()))
}
return operations.NewGetJobSpecOK().WithPayload(&operations.GetJobSpecOKBody{
Job: result,
})
},
)
server := restapi.NewServer(api)
defer func() {
shutdownErr := server.Shutdown()
if shutdownErr != nil {
log.WithError(shutdownErr).Error("Failed to shut down server")
}
}()
server.Port = configuration.ApiPort
restapi.SetCorsAllowedOrigins(configuration.CorsAllowedOrigins) // This needs to happen before ConfigureAPI
server.ConfigureAPI()
if err := server.Serve(); err != nil {
return err
}
return err
}