zrok/controller/controller.go

51 lines
1.7 KiB
Go
Raw Normal View History

package controller
import (
"github.com/go-openapi/loads"
2022-07-22 17:45:37 +02:00
"github.com/go-openapi/runtime/middleware"
"github.com/openziti-test-kitchen/zrok/controller/store"
"github.com/openziti-test-kitchen/zrok/rest_server_zrok"
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations"
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/identity"
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/metadata"
2022-07-26 22:21:49 +02:00
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/tunnel"
"github.com/pkg/errors"
)
var Str *store.Store
func Run(cfg *Config) error {
swaggerSpec, err := loads.Embedded(rest_server_zrok.SwaggerJSON, rest_server_zrok.FlatSwaggerJSON)
if err != nil {
return errors.Wrap(err, "error loading embedded swagger spec")
}
api := operations.NewZrokAPI(swaggerSpec)
api.KeyAuth = ZrokAuthenticate
2022-07-25 19:17:52 +02:00
api.MetadataVersionHandler = metadata.VersionHandlerFunc(versionHandler)
api.IdentityCreateAccountHandler = identity.CreateAccountHandlerFunc(createAccountHandler)
2022-07-25 23:05:44 +02:00
api.IdentityEnableHandler = identity.EnableHandlerFunc(enableHandler)
2022-07-26 22:21:49 +02:00
api.TunnelTunnelHandler = tunnel.TunnelHandlerFunc(tunnelHandler)
2022-07-27 18:17:37 +02:00
api.TunnelUntunnelHandler = tunnel.UntunnelHandlerFunc(untunnelHandler)
2022-07-22 17:45:37 +02:00
2022-07-27 19:38:35 +02:00
if v, err := store.Open(cfg.Store); err == nil {
Str = v
2022-07-27 19:38:35 +02:00
} else {
return errors.Wrap(err, "error opening store")
}
server := rest_server_zrok.NewServer(api)
defer func() { _ = server.Shutdown() }()
server.Host = cfg.Host
server.Port = cfg.Port
server.ConfigureAPI()
if err := server.Serve(); err != nil {
return errors.Wrap(err, "api server error")
}
return nil
}
2022-07-25 19:17:52 +02:00
func versionHandler(_ metadata.VersionParams) middleware.Responder {
return metadata.NewVersionOK().WithPayload("v0.0.2")
2022-07-25 19:17:52 +02:00
}