forked from rai-project/dlframework
/
http.go
68 lines (54 loc) · 2.77 KB
/
http.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
package http
import (
"net/http"
"github.com/go-openapi/runtime"
"github.com/c3sr/dlframework/httpapi/restapi/operations"
"github.com/c3sr/dlframework/httpapi/models"
"github.com/c3sr/dlframework/httpapi/restapi/operations/authentication"
"github.com/c3sr/dlframework/httpapi/restapi/operations/predict"
"github.com/c3sr/dlframework/httpapi/restapi/operations/registry"
"github.com/volatiletech/authboss/v3/remember"
)
func ConfigureAPI(api *operations.DlframeworkAPI) http.Handler {
api.ServeError = ServeError
api.Logger = log.Debugf
api.JSONConsumer = runtime.JSONConsumer()
api.JSONProducer = runtime.JSONProducer()
api.BasicAuthAuth = func(user string, pass string) (*models.User, error) {
return &models.User{
Username: user,
Password: pass,
}, nil
}
api.AuthenticationLoginHandler = authentication.LoginHandlerFunc(LoginHandler)
api.AuthenticationSignupHandler = authentication.SignupHandlerFunc(SignupHandler)
api.AuthenticationUserInfoHandler = authentication.UserInfoHandlerFunc(UserInfoHandler)
api.AuthenticationLogoutHandler = authentication.LogoutHandlerFunc(LogoutHandler)
api.RegistryFrameworkAgentsHandler = registry.FrameworkAgentsHandlerFunc(RegistryFrameworkAgentsHandler)
api.RegistryFrameworkManifestsHandler = registry.FrameworkManifestsHandlerFunc(RegistryFrameworkManifestsHandler)
api.RegistryModelAgentsHandler = registry.ModelAgentsHandlerFunc(RegistryModelAgentsHandler)
api.RegistryModelManifestsHandler = registry.ModelManifestsHandlerFunc(RegistryModelManifestsHandler)
predictHandler := &PredictHandler{}
api.PredictOpenHandler = predict.OpenHandlerFunc(predictHandler.Open)
api.PredictCloseHandler = predict.CloseHandlerFunc(predictHandler.Close)
api.PredictResetHandler = predict.ResetHandlerFunc(predictHandler.Reset)
api.PredictImagesHandler = predict.ImagesHandlerFunc(predictHandler.Images)
api.PredictURLsHandler = predict.URLsHandlerFunc(predictHandler.URLs)
api.PredictDatasetHandler = predict.DatasetHandlerFunc(predictHandler.Dataset)
api.ServerShutdown = func() {}
setupAuthboss()
return setupGlobalMiddleware(api.Serve(setupMiddlewares))
}
// The middleware configuration happens before anything, this middleware also applies to serving the swagger.json document.
// So this is a good place to plug in a panic handling middleware, logging and metrics
func setupGlobalMiddleware(handler http.Handler) http.Handler {
handler = ab.LoadClientStateMiddleware(handler)
handler = remember.Middleware(ab)(handler)
handler = dataInjector(handler)
return handler
}
// The middleware configuration is for the handler executors. These do not apply to the swagger.json document.
// The middleware executes after routing but before authentication, binding and validation
func setupMiddlewares(handler http.Handler) http.Handler {
return handler
}