zrok/controller/controller.go

67 lines
1.9 KiB
Go
Raw Normal View History

package controller
import (
"github.com/go-openapi/loads"
"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"
"github.com/pkg/errors"
)
2022-07-29 21:28:40 +02:00
var str *store.Store
2022-10-13 20:23:52 +02:00
var mtr *metricsAgent
2022-09-15 21:18:37 +02:00
const version = "v0.2.0"
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
api.IdentityCreateAccountHandler = newCreateAccountHandler(cfg)
2022-08-12 17:03:15 +02:00
api.IdentityEnableHandler = newEnableHandler(cfg)
2022-09-02 16:50:13 +02:00
api.IdentityDisableHandler = newDisableHandler(cfg)
2022-08-02 19:23:31 +02:00
api.IdentityLoginHandler = identity.LoginHandlerFunc(loginHandler)
api.IdentityRegisterHandler = newRegisterHandler()
2022-09-19 22:13:47 +02:00
api.IdentityVerifyHandler = newVerifyHandler(cfg)
2022-08-03 20:58:11 +02:00
api.MetadataOverviewHandler = metadata.OverviewHandlerFunc(overviewHandler)
2022-08-02 19:23:31 +02:00
api.MetadataVersionHandler = metadata.VersionHandlerFunc(versionHandler)
api.TunnelTunnelHandler = newTunnelHandler(cfg)
2022-08-12 17:03:15 +02:00
api.TunnelUntunnelHandler = newUntunnelHandler(cfg)
2022-07-22 17:45:37 +02:00
2022-08-15 20:25:50 +02:00
if err := controllerStartup(cfg); err != nil {
return err
}
2022-07-27 19:38:35 +02:00
if v, err := store.Open(cfg.Store); err == nil {
2022-07-29 21:28:40 +02:00
str = v
2022-07-27 19:38:35 +02:00
} else {
return errors.Wrap(err, "error opening store")
}
if cfg.Metrics != nil {
mtr = newMetricsAgent(cfg.Metrics)
go mtr.run()
defer func() {
mtr.stop()
mtr.join()
}()
}
2022-10-13 20:23:52 +02:00
server := rest_server_zrok.NewServer(api)
defer func() { _ = server.Shutdown() }()
2022-08-09 17:18:24 +02:00
server.Host = cfg.Endpoint.Host
server.Port = cfg.Endpoint.Port
server.ConfigureAPI()
if err := server.Serve(); err != nil {
return errors.Wrap(err, "api server error")
}
2022-10-13 20:23:52 +02:00
return nil
}