This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.go
145 lines (123 loc) · 5.11 KB
/
api.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
138
139
140
141
142
143
144
145
package skeleton
import (
"fmt"
"github.com/DRK-Blutspende-BaWueHe/skeleton/config"
"github.com/DRK-Blutspende-BaWueHe/skeleton/consolelog/service"
"github.com/DRK-Blutspende-BaWueHe/skeleton/middleware"
"github.com/DRK-Blutspende-BaWueHe/skeleton/server"
"net/http/pprof"
"sync"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog"
)
type api struct {
config *config.Configuration
engine *gin.Engine
analysisService AnalysisService
instrumentService InstrumentService
consoleLogService service.ConsoleLogService
createAnalysisRequestMutex sync.Mutex
consoleLogSSEServer *server.ConsoleLogSSEServer
}
func (api *api) Run() error {
if api.config.EnableTLS {
return api.engine.RunTLS(fmt.Sprintf("bloodlab:%d", api.config.APIPort), api.config.BloodlabCertPath, api.config.BloodlabKeyPath)
}
return api.engine.Run(fmt.Sprintf(":%d", api.config.APIPort))
}
func NewAPI(config *config.Configuration,
authManager AuthManager,
analysisService AnalysisService,
instrumentService InstrumentService,
consoleLogService service.ConsoleLogService,
consoleLogSSEServer *server.ConsoleLogSSEServer) GinApi {
return newAPI(gin.New(), config, authManager, analysisService, instrumentService, consoleLogService, consoleLogSSEServer)
}
func NewAPIForTesting(engine *gin.Engine,
config *config.Configuration,
authManager AuthManager,
analysisService AnalysisService,
instrumentService InstrumentService,
consoleLogService service.ConsoleLogService,
consoleLogSSEServer *server.ConsoleLogSSEServer) GinApi {
return newAPI(engine, config, authManager, analysisService, instrumentService, consoleLogService, consoleLogSSEServer)
}
func newAPI(engine *gin.Engine, config *config.Configuration, authManager AuthManager,
analysisService AnalysisService, instrumentService InstrumentService, consoleLogService service.ConsoleLogService,
consoleLogSSEServer *server.ConsoleLogSSEServer) GinApi {
if config.LogLevel <= zerolog.DebugLevel {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
engine.Use(gin.Recovery())
engine.Use(gin.Logger())
api := api{
config: config,
engine: engine,
analysisService: analysisService,
instrumentService: instrumentService,
consoleLogService: consoleLogService,
consoleLogSSEServer: consoleLogSSEServer,
}
corsMiddleWare := middleware.CreateCorsMiddleware(config)
engine.Use(corsMiddleWare)
root := engine.Group("")
root.GET("/health", api.GetHealth)
v1Group := root.Group("v1")
if api.config.Authorization {
authMiddleWare := middleware.CheckAuth(authManager)
v1Group.Use(authMiddleWare)
}
instrumentsGroup := v1Group.Group("/instruments")
{
instrumentsGroup.GET("", api.GetInstruments)
instrumentsGroup.POST("", api.CreateInstrument)
instrumentsGroup.GET("/:instrumentId", api.GetInstrumentByID)
instrumentsGroup.GET("/:instrumentId/requests", api.GetAnalysisRequestsInfo)
instrumentsGroup.GET("/:instrumentId/results", api.GetAnalysisResultsInfo)
instrumentsGroup.GET("/:instrumentId/list/transmissions", api.GetAnalysisBatches)
instrumentsGroup.PUT("/:instrumentId", api.UpdateInstrument)
instrumentsGroup.DELETE("/:instrumentId", api.DeleteInstrument)
instrumentsGroup.POST("/result/:resultID/retransmit", api.RetransmitResult)
instrumentsGroup.POST("/result/retransmit/batches", api.RetransmitResultBatches)
instrumentsGroup.GET("/protocol/:protocolId/encodings", api.GetEncodings)
instrumentsGroup.GET("/:instrumentId/console/handshake", middleware.SSEHeadersMiddleware(), api.consoleLogSSEServer.ServeHTTP())
messagesGroup := instrumentsGroup.Group("/:instrumentId/messages")
{
messagesGroup.GET("", api.GetMessages)
}
}
protocolVersions := v1Group.Group("/protocol-versions")
{
protocolVersions.GET("", api.GetSupportedProtocols)
protocolVersions.GET("/:protocolVersionId/abilities", api.GetProtocolAbilities)
protocolVersions.GET("/:protocolVersionId/manufacturer-tests", api.GetManufacturerTests)
}
analysisRequests := v1Group.Group("analysis-requests")
{
analysisRequests.POST("/batch", api.CreateAnalysisRequestBatch)
analysisRequests.DELETE("/batch", api.RevokeAnalysisRequestBatch)
analysisRequests.POST("/batch/reexamine", api.ReexamineAnalysisRequestBatch)
}
v1Group.POST("/analytes/usage", api.CheckAnalytesUsage)
// Development-option enables debugger, this can have side-effects
if api.config.Development {
debug := root.Group("/debug/pprof")
{
debug.GET("/", gin.WrapF(pprof.Index))
debug.GET("/cmdline", gin.WrapF(pprof.Cmdline))
debug.GET("/profile", gin.WrapF(pprof.Profile))
debug.GET("/symbol", gin.WrapF(pprof.Symbol))
debug.GET("/trace", gin.WrapF(pprof.Trace))
debug.GET("/allocs", gin.WrapH(pprof.Handler("allocs")))
debug.GET("/block", gin.WrapH(pprof.Handler("block")))
debug.GET("/goroutine", gin.WrapH(pprof.Handler("goroutine")))
debug.GET("/heap", gin.WrapH(pprof.Handler("heap")))
debug.GET("/mutex", gin.WrapH(pprof.Handler("mutex")))
debug.GET("/threadcreate", gin.WrapH(pprof.Handler("threadcreate")))
debug.POST("/symbol", gin.WrapF(pprof.Symbol))
}
}
return &api
}