begin move to go-restful

This commit is contained in:
Mikaël Cluseau
2019-02-01 18:28:08 +11:00
parent 92d3142d96
commit f4f285d0dc
8 changed files with 228 additions and 8 deletions

50
pkg/apiutils/apiutils.go Normal file
View File

@ -0,0 +1,50 @@
package apiutils
import (
"github.com/emicklei/go-restful"
restfulspec "github.com/emicklei/go-restful-openapi"
)
// Prepare does the default API preparation.
func Prepare() {
restful.DefaultRequestContentType(restful.MIME_JSON)
restful.DefaultResponseContentType(restful.MIME_JSON)
restful.DefaultContainer.Router(restful.CurlyRouter{})
}
// Setup does the default API setup
func Setup(addWebServices func()) {
Prepare()
addWebServices()
SetupHealth()
SetupOpenAPI()
SetupCORSWildcard()
}
func SetupHealth() {
// TODO
}
// SetupOpenAPI creates the standard API documentation endpoint at the default location.
func SetupOpenAPI() {
SetupOpenAPIAt("/swagger.json")
}
// SetupOpenAPI creates the standard API documentation endpoint at the defined location.
func SetupOpenAPIAt(apiPath string) {
config := restfulspec.Config{
WebServices: restful.RegisteredWebServices(),
APIPath: apiPath,
}
restful.Add(restfulspec.NewOpenAPIService(config))
}
func SetupCORSWildcard() {
restful.Filter(restful.CrossOriginResourceSharing{
CookiesAllowed: true,
Container: restful.DefaultContainer,
}.Filter)
restful.Filter(restful.OPTIONSFilter())
}