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-07-22 17:45:37 +02:00
|
|
|
"github.com/go-openapi/runtime/middleware"
|
2022-07-22 19:53:39 +02:00
|
|
|
"github.com/openziti-test-kitchen/zrok/controller/store"
|
2022-07-22 17:45:37 +02:00
|
|
|
"github.com/openziti-test-kitchen/zrok/rest_model"
|
2022-07-22 17:16:30 +02:00
|
|
|
"github.com/openziti-test-kitchen/zrok/rest_zrok_server"
|
|
|
|
"github.com/openziti-test-kitchen/zrok/rest_zrok_server/operations"
|
2022-07-22 17:45:37 +02:00
|
|
|
"github.com/openziti-test-kitchen/zrok/rest_zrok_server/operations/metadata"
|
2022-07-22 17:16:30 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2022-07-22 19:53:39 +02:00
|
|
|
var str *store.Store
|
|
|
|
|
2022-07-21 22:43:42 +02:00
|
|
|
func Run(cfg *Config) error {
|
2022-07-22 19:53:39 +02:00
|
|
|
if v, err := store.Open(cfg.Store); err == nil {
|
|
|
|
str = v
|
|
|
|
} else {
|
|
|
|
return errors.Wrap(err, "error opening store")
|
|
|
|
}
|
|
|
|
|
2022-07-22 17:16:30 +02:00
|
|
|
swaggerSpec, err := loads.Embedded(rest_zrok_server.SwaggerJSON, rest_zrok_server.FlatSwaggerJSON)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error loading embedded swagger spec")
|
|
|
|
}
|
|
|
|
|
|
|
|
api := operations.NewZrokAPI(swaggerSpec)
|
2022-07-25 17:51:23 +02:00
|
|
|
api.MetadataVersionHandler = metadata.VersionHandlerFunc(func(_ metadata.VersionParams) middleware.Responder {
|
2022-07-22 19:53:39 +02:00
|
|
|
return metadata.NewGetOK().WithPayload(&rest_model.Version{Version: "v0.0.0; sk3tch"})
|
2022-07-22 17:45:37 +02:00
|
|
|
})
|
|
|
|
|
2022-07-22 17:16:30 +02:00
|
|
|
server := rest_zrok_server.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")
|
|
|
|
}
|
2022-07-21 22:43:42 +02:00
|
|
|
return nil
|
|
|
|
}
|