zrok/controller/controller.go

145 lines
4.8 KiB
Go
Raw Normal View History

package controller
import (
"context"
"github.com/openziti/zrok/controller/config"
"github.com/openziti/zrok/controller/limits"
2023-03-15 21:14:06 +01:00
"github.com/openziti/zrok/controller/metrics"
"github.com/sirupsen/logrus"
2023-06-16 17:52:42 +02:00
"log"
"net/http"
_ "net/http/pprof"
"github.com/go-openapi/loads"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
"github.com/openziti/zrok/controller/store"
"github.com/openziti/zrok/rest_server_zrok"
"github.com/openziti/zrok/rest_server_zrok/operations"
"github.com/openziti/zrok/rest_server_zrok/operations/account"
"github.com/openziti/zrok/rest_server_zrok/operations/metadata"
"github.com/pkg/errors"
)
var (
2023-07-18 20:03:43 +02:00
cfg *config.Config
str *store.Store
idb influxdb2.Client
limitsAgent *limits.Agent
)
func Run(inCfg *config.Config) error {
cfg = inCfg
2023-06-16 17:52:42 +02:00
if cfg.Admin != nil && cfg.Admin.ProfileEndpoint != "" {
go func() {
log.Println(http.ListenAndServe(cfg.Admin.ProfileEndpoint, nil))
}()
}
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 = newZrokAuthenticator(cfg).authenticate
api.AccountInviteHandler = newInviteHandler(cfg)
2022-11-30 17:43:00 +01:00
api.AccountLoginHandler = account.LoginHandlerFunc(loginHandler)
api.AccountRegisterHandler = newRegisterHandler(cfg)
api.AccountResetPasswordHandler = newResetPasswordHandler(cfg)
api.AccountResetPasswordRequestHandler = newResetPasswordRequestHandler()
2022-11-30 17:43:00 +01:00
api.AccountVerifyHandler = newVerifyHandler()
2022-12-01 22:14:42 +01:00
api.AdminCreateFrontendHandler = newCreateFrontendHandler()
api.AdminCreateIdentityHandler = newCreateIdentityHandler()
api.AdminDeleteFrontendHandler = newDeleteFrontendHandler()
2023-01-10 19:26:22 +01:00
api.AdminInviteTokenGenerateHandler = newInviteTokenGenerateHandler()
2022-12-02 15:30:06 +01:00
api.AdminListFrontendsHandler = newListFrontendsHandler()
2022-12-02 16:46:53 +01:00
api.AdminUpdateFrontendHandler = newUpdateFrontendHandler()
api.EnvironmentEnableHandler = newEnableHandler()
2022-11-30 17:43:00 +01:00
api.EnvironmentDisableHandler = newDisableHandler()
api.MetadataGetAccountDetailHandler = newAccountDetailHandler()
api.MetadataConfigurationHandler = newConfigurationHandler(cfg)
2023-05-09 20:16:01 +02:00
if cfg.Metrics != nil && cfg.Metrics.Influx != nil {
api.MetadataGetAccountMetricsHandler = newGetAccountMetricsHandler(cfg.Metrics.Influx)
api.MetadataGetEnvironmentMetricsHandler = newGetEnvironmentMetricsHandler(cfg.Metrics.Influx)
api.MetadataGetShareMetricsHandler = newGetShareMetricsHandler(cfg.Metrics.Influx)
2023-05-09 20:16:01 +02:00
}
api.MetadataGetEnvironmentDetailHandler = newEnvironmentDetailHandler()
2023-05-17 19:53:18 +02:00
api.MetadataGetFrontendDetailHandler = newGetFrontendDetailHandler()
api.MetadataGetShareDetailHandler = newShareDetailHandler()
2023-05-16 19:45:43 +02:00
api.MetadataOverviewHandler = newOverviewHandler()
2022-08-02 19:23:31 +02:00
api.MetadataVersionHandler = metadata.VersionHandlerFunc(versionHandler)
api.ShareAccessHandler = newAccessHandler()
2023-07-18 20:03:43 +02:00
api.ShareShareHandler = newShareHandler()
api.ShareUnaccessHandler = newUnaccessHandler()
api.ShareUnshareHandler = newUnshareHandler()
api.ShareUpdateShareHandler = newUpdateShareHandler()
2022-07-22 17:45:37 +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")
}
if cfg.Metrics != nil && cfg.Metrics.Influx != nil {
idb = influxdb2.NewClient(cfg.Metrics.Influx.Url, cfg.Metrics.Influx.Token)
} else {
logrus.Warn("skipping influx client; no configuration")
}
if cfg.Metrics != nil && cfg.Metrics.Agent != nil && cfg.Metrics.Influx != nil {
2023-03-15 21:14:06 +01:00
ma, err := metrics.NewAgent(cfg.Metrics.Agent, str, cfg.Metrics.Influx)
if err != nil {
return errors.Wrap(err, "error creating metrics agent")
}
if err := ma.Start(); err != nil {
return errors.Wrap(err, "error starting metrics agent")
}
defer func() { ma.Stop() }()
if cfg.Limits != nil && cfg.Limits.Enforcing {
limitsAgent, err = limits.NewAgent(cfg.Limits, cfg.Metrics.Influx, cfg.Ziti, cfg.Email, str)
if err != nil {
return errors.Wrap(err, "error creating limits agent")
}
ma.AddUsageSink(limitsAgent)
limitsAgent.Start()
defer func() { limitsAgent.Stop() }()
}
}
ctx, cancel := context.WithCancel(context.Background())
defer func() {
cancel()
}()
if cfg.Maintenance != nil {
if cfg.Maintenance.Registration != nil {
go newRegistrationMaintenanceAgent(ctx, cfg.Maintenance.Registration).run()
}
if cfg.Maintenance.ResetPassword != nil {
go newMaintenanceResetPasswordAgent(ctx, cfg.Maintenance.ResetPassword).run()
}
}
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
rest_server_zrok.HealthCheck = HealthCheckHTTP
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
}
func Store() *store.Store {
return str
}