2022-07-21 22:43:42 +02:00
|
|
|
package controller
|
|
|
|
|
2022-07-22 17:16:30 +02:00
|
|
|
import (
|
|
|
|
"github.com/go-openapi/loads"
|
2022-10-19 21:21:15 +02:00
|
|
|
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
|
2022-07-22 19:53:39 +02:00
|
|
|
"github.com/openziti-test-kitchen/zrok/controller/store"
|
2022-07-26 21:16:02 +02:00
|
|
|
"github.com/openziti-test-kitchen/zrok/rest_server_zrok"
|
|
|
|
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations"
|
2022-11-30 17:43:00 +01:00
|
|
|
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/account"
|
2022-07-26 21:16:02 +02:00
|
|
|
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/metadata"
|
2022-11-30 20:38:49 +01:00
|
|
|
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/service"
|
2022-07-22 17:16:30 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2022-10-19 19:20:47 +02:00
|
|
|
var cfg *Config
|
2022-07-29 21:28:40 +02:00
|
|
|
var str *store.Store
|
2022-10-13 20:23:52 +02:00
|
|
|
var mtr *metricsAgent
|
2022-10-19 21:21:15 +02:00
|
|
|
var idb influxdb2.Client
|
2022-07-22 19:53:39 +02:00
|
|
|
|
2022-10-19 19:20:47 +02:00
|
|
|
func Run(inCfg *Config) error {
|
|
|
|
cfg = inCfg
|
|
|
|
|
2022-07-26 21:16:02 +02:00
|
|
|
swaggerSpec, err := loads.Embedded(rest_server_zrok.SwaggerJSON, rest_server_zrok.FlatSwaggerJSON)
|
2022-07-22 17:16:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error loading embedded swagger spec")
|
|
|
|
}
|
|
|
|
|
|
|
|
api := operations.NewZrokAPI(swaggerSpec)
|
2022-07-27 20:45:16 +02:00
|
|
|
api.KeyAuth = ZrokAuthenticate
|
2022-11-30 17:43:00 +01:00
|
|
|
api.AccountInviteHandler = newInviteHandler()
|
|
|
|
api.AccountLoginHandler = account.LoginHandlerFunc(loginHandler)
|
|
|
|
api.AccountRegisterHandler = newRegisterHandler()
|
|
|
|
api.AccountVerifyHandler = newVerifyHandler()
|
|
|
|
api.EnvironmentEnableHandler = newEnableHandler()
|
|
|
|
api.EnvironmentDisableHandler = newDisableHandler()
|
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)
|
2022-11-23 18:24:35 +01:00
|
|
|
api.ServiceAccessHandler = newAccessHandler()
|
2022-11-30 20:38:49 +01:00
|
|
|
api.ServiceGetServiceHandler = service.GetServiceHandlerFunc(getServiceHandler)
|
2022-11-18 21:36:55 +01:00
|
|
|
api.ServiceShareHandler = newShareHandler()
|
2022-11-23 19:00:01 +01:00
|
|
|
api.ServiceUnaccessHandler = newUnaccessHandler()
|
2022-11-18 21:36:55 +01:00
|
|
|
api.ServiceUnshareHandler = newUnshareHandler()
|
2022-07-22 17:45:37 +02:00
|
|
|
|
2022-10-19 19:20:47 +02:00
|
|
|
if err := controllerStartup(); err != nil {
|
2022-08-15 20:25:50 +02:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2022-10-19 21:21:15 +02:00
|
|
|
if cfg.Influx != nil {
|
|
|
|
idb = influxdb2.NewClient(cfg.Influx.Url, cfg.Influx.Token)
|
|
|
|
}
|
|
|
|
|
2022-10-13 21:50:12 +02:00
|
|
|
if cfg.Metrics != nil {
|
2022-10-19 19:20:47 +02:00
|
|
|
mtr = newMetricsAgent()
|
2022-10-13 21:50:12 +02:00
|
|
|
go mtr.run()
|
|
|
|
defer func() {
|
|
|
|
mtr.stop()
|
|
|
|
mtr.join()
|
|
|
|
}()
|
|
|
|
}
|
2022-10-13 20:23:52 +02:00
|
|
|
|
2022-07-26 21:16:02 +02:00
|
|
|
server := rest_server_zrok.NewServer(api)
|
2022-07-22 17:16:30 +02:00
|
|
|
defer func() { _ = server.Shutdown() }()
|
2022-08-09 17:18:24 +02:00
|
|
|
server.Host = cfg.Endpoint.Host
|
|
|
|
server.Port = cfg.Endpoint.Port
|
2022-07-22 17:16:30 +02:00
|
|
|
server.ConfigureAPI()
|
|
|
|
if err := server.Serve(); err != nil {
|
|
|
|
return errors.Wrap(err, "api server error")
|
|
|
|
}
|
2022-10-13 20:23:52 +02:00
|
|
|
|
2022-07-21 22:43:42 +02:00
|
|
|
return nil
|
|
|
|
}
|