wiring in the go-swagger infrastructure

This commit is contained in:
Michael Quigley 2022-07-22 11:16:30 -04:00
parent 87d729791a
commit ad8000c369
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
13 changed files with 97 additions and 75 deletions

View File

@ -23,13 +23,13 @@ func main() {
log.Fatalln(err)
}
api := operations.NewZrokClientAPI(swaggerSpec)
api := operations.NewZrokAPI(swaggerSpec)
server := rest_zrok_server.NewServer(api)
defer server.Shutdown()
parser := flags.NewParser(server, flags.Default)
parser.ShortDescription = "zrok Client"
parser.LongDescription = "Client access service"
parser.ShortDescription = "zrok"
parser.LongDescription = "zrok client access"
server.ConfigureFlags()
for _, optsGroup := range api.CommandLineOptionsGroups {
_, err := parser.AddGroup(optsGroup.ShortDescription, optsGroup.LongDescription, optsGroup.Options)

View File

@ -36,7 +36,7 @@ var controllerCmd = &cobra.Command{
Short: "Start a zrok controller",
Aliases: []string{"ctrl"},
Run: func(_ *cobra.Command, args []string) {
if err := controller.Run(&controller.Config{ApiEndpoint: "0.0.0.0:18888"}); err != nil {
if err := controller.Run(&controller.Config{Host: "0.0.0.0", Port: 10888}); err != nil {
panic(err)
}
},
@ -56,7 +56,7 @@ var proxyCmd = &cobra.Command{
Use: "proxy <configPath>",
Short: "Start a zrok proxy",
Run: func(_ *cobra.Command, args []string) {
if err := proxy.Run(&proxy.Config{IdentityPath: args[0], Address: "0.0.0.0:10081"}); err != nil {
if err := proxy.Run(&proxy.Config{IdentityPath: args[0], Address: "0.0.0.0:10111"}); err != nil {
panic(err)
}
},

View File

@ -1,5 +1,6 @@
package controller
type Config struct {
ApiEndpoint string
Host string
Port int
}

View File

@ -1,5 +1,26 @@
package controller
import (
"github.com/go-openapi/loads"
"github.com/openziti-test-kitchen/zrok/rest_zrok_server"
"github.com/openziti-test-kitchen/zrok/rest_zrok_server/operations"
"github.com/pkg/errors"
)
func Run(cfg *Config) error {
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)
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")
}
return nil
}

View File

@ -41,7 +41,7 @@ func NewGetOK() *GetOK {
/* GetOK describes a response with status code 200, with default header values.
Retrieve the current server version
retrieve the current server version
*/
type GetOK struct {
Payload *rest_model.Version

View File

@ -13,7 +13,7 @@ import (
"github.com/openziti-test-kitchen/zrok/rest_zrok_client/metadata"
)
// Default zrok client HTTP client.
// Default zrok HTTP client.
var Default = NewHTTPClient(nil)
const (
@ -28,14 +28,14 @@ const (
// DefaultSchemes are the default schemes found in Meta (info) section of spec file
var DefaultSchemes = []string{"http"}
// NewHTTPClient creates a new zrok client HTTP client.
func NewHTTPClient(formats strfmt.Registry) *ZrokClient {
// NewHTTPClient creates a new zrok HTTP client.
func NewHTTPClient(formats strfmt.Registry) *Zrok {
return NewHTTPClientWithConfig(formats, nil)
}
// NewHTTPClientWithConfig creates a new zrok client HTTP client,
// NewHTTPClientWithConfig creates a new zrok HTTP client,
// using a customizable transport config.
func NewHTTPClientWithConfig(formats strfmt.Registry, cfg *TransportConfig) *ZrokClient {
func NewHTTPClientWithConfig(formats strfmt.Registry, cfg *TransportConfig) *Zrok {
// ensure nullable parameters have default
if cfg == nil {
cfg = DefaultTransportConfig()
@ -46,14 +46,14 @@ func NewHTTPClientWithConfig(formats strfmt.Registry, cfg *TransportConfig) *Zro
return New(transport, formats)
}
// New creates a new zrok client client
func New(transport runtime.ClientTransport, formats strfmt.Registry) *ZrokClient {
// New creates a new zrok client
func New(transport runtime.ClientTransport, formats strfmt.Registry) *Zrok {
// ensure nullable parameters have default
if formats == nil {
formats = strfmt.Default
}
cli := new(ZrokClient)
cli := new(Zrok)
cli.Transport = transport
cli.Metadata = metadata.New(transport, formats)
return cli
@ -98,15 +98,15 @@ func (cfg *TransportConfig) WithSchemes(schemes []string) *TransportConfig {
return cfg
}
// ZrokClient is a client for zrok client
type ZrokClient struct {
// Zrok is a client for zrok
type Zrok struct {
Metadata metadata.ClientService
Transport runtime.ClientTransport
}
// SetTransport changes the transport on the client and all its subresources
func (c *ZrokClient) SetTransport(transport runtime.ClientTransport) {
func (c *Zrok) SetTransport(transport runtime.ClientTransport) {
c.Transport = transport
c.Metadata.SetTransport(transport)
}

View File

@ -14,13 +14,13 @@ import (
"github.com/openziti-test-kitchen/zrok/rest_zrok_server/operations/metadata"
)
//go:generate swagger generate server --target ../../zrok --name ZrokClient --spec ../specs/zrok.yml --model-package rest_model --server-package rest_zrok_server --principal interface{}
//go:generate swagger generate server --target ../../zrok --name Zrok --spec ../specs/zrok.yml --model-package rest_model --server-package rest_zrok_server --principal interface{}
func configureFlags(api *operations.ZrokClientAPI) {
func configureFlags(api *operations.ZrokAPI) {
// api.CommandLineOptionsGroups = []swag.CommandLineOptionsGroup{ ... }
}
func configureAPI(api *operations.ZrokClientAPI) http.Handler {
func configureAPI(api *operations.ZrokAPI) http.Handler {
// configure the api here
api.ServeError = errors.ServeError

View File

@ -1,8 +1,8 @@
// Code generated by go-swagger; DO NOT EDIT.
// Package rest_zrok_server zrok Client
// Package rest_zrok_server zrok
//
// Client access service
// zrok client access
// Schemes:
// http
// Host: localhost

View File

@ -29,8 +29,8 @@ func init() {
],
"swagger": "2.0",
"info": {
"description": "Client access service",
"title": "zrok Client",
"description": "zrok client access",
"title": "zrok",
"version": "1.0.0"
},
"paths": {
@ -41,7 +41,7 @@ func init() {
],
"responses": {
"200": {
"description": "Retrieve the current server version",
"description": "retrieve the current server version",
"schema": {
"$ref": "#/definitions/version"
}
@ -74,8 +74,8 @@ func init() {
],
"swagger": "2.0",
"info": {
"description": "Client access service",
"title": "zrok Client",
"description": "zrok client access",
"title": "zrok",
"version": "1.0.0"
},
"paths": {
@ -86,7 +86,7 @@ func init() {
],
"responses": {
"200": {
"description": "Retrieve the current server version",
"description": "retrieve the current server version",
"schema": {
"$ref": "#/definitions/version"
}

View File

@ -16,7 +16,7 @@ import (
// GetOKCode is the HTTP code returned for type GetOK
const GetOKCode int = 200
/*GetOK Retrieve the current server version
/*GetOK retrieve the current server version
swagger:response getOK
*/

View File

@ -22,9 +22,9 @@ import (
"github.com/openziti-test-kitchen/zrok/rest_zrok_server/operations/metadata"
)
// NewZrokClientAPI creates a new ZrokClient instance
func NewZrokClientAPI(spec *loads.Document) *ZrokClientAPI {
return &ZrokClientAPI{
// NewZrokAPI creates a new Zrok instance
func NewZrokAPI(spec *loads.Document) *ZrokAPI {
return &ZrokAPI{
handlers: make(map[string]map[string]http.Handler),
formats: strfmt.Default,
defaultConsumes: "application/json",
@ -50,8 +50,8 @@ func NewZrokClientAPI(spec *loads.Document) *ZrokClientAPI {
}
}
/*ZrokClientAPI Client access service */
type ZrokClientAPI struct {
/*ZrokAPI zrok client access */
type ZrokAPI struct {
spec *loads.Document
context *middleware.Context
handlers map[string]map[string]http.Handler
@ -106,52 +106,52 @@ type ZrokClientAPI struct {
}
// UseRedoc for documentation at /docs
func (o *ZrokClientAPI) UseRedoc() {
func (o *ZrokAPI) UseRedoc() {
o.useSwaggerUI = false
}
// UseSwaggerUI for documentation at /docs
func (o *ZrokClientAPI) UseSwaggerUI() {
func (o *ZrokAPI) UseSwaggerUI() {
o.useSwaggerUI = true
}
// SetDefaultProduces sets the default produces media type
func (o *ZrokClientAPI) SetDefaultProduces(mediaType string) {
func (o *ZrokAPI) SetDefaultProduces(mediaType string) {
o.defaultProduces = mediaType
}
// SetDefaultConsumes returns the default consumes media type
func (o *ZrokClientAPI) SetDefaultConsumes(mediaType string) {
func (o *ZrokAPI) SetDefaultConsumes(mediaType string) {
o.defaultConsumes = mediaType
}
// SetSpec sets a spec that will be served for the clients.
func (o *ZrokClientAPI) SetSpec(spec *loads.Document) {
func (o *ZrokAPI) SetSpec(spec *loads.Document) {
o.spec = spec
}
// DefaultProduces returns the default produces media type
func (o *ZrokClientAPI) DefaultProduces() string {
func (o *ZrokAPI) DefaultProduces() string {
return o.defaultProduces
}
// DefaultConsumes returns the default consumes media type
func (o *ZrokClientAPI) DefaultConsumes() string {
func (o *ZrokAPI) DefaultConsumes() string {
return o.defaultConsumes
}
// Formats returns the registered string formats
func (o *ZrokClientAPI) Formats() strfmt.Registry {
func (o *ZrokAPI) Formats() strfmt.Registry {
return o.formats
}
// RegisterFormat registers a custom format validator
func (o *ZrokClientAPI) RegisterFormat(name string, format strfmt.Format, validator strfmt.Validator) {
func (o *ZrokAPI) RegisterFormat(name string, format strfmt.Format, validator strfmt.Validator) {
o.formats.Add(name, format, validator)
}
// Validate validates the registrations in the ZrokClientAPI
func (o *ZrokClientAPI) Validate() error {
// Validate validates the registrations in the ZrokAPI
func (o *ZrokAPI) Validate() error {
var unregistered []string
if o.JSONConsumer == nil {
@ -174,23 +174,23 @@ func (o *ZrokClientAPI) Validate() error {
}
// ServeErrorFor gets a error handler for a given operation id
func (o *ZrokClientAPI) ServeErrorFor(operationID string) func(http.ResponseWriter, *http.Request, error) {
func (o *ZrokAPI) ServeErrorFor(operationID string) func(http.ResponseWriter, *http.Request, error) {
return o.ServeError
}
// AuthenticatorsFor gets the authenticators for the specified security schemes
func (o *ZrokClientAPI) AuthenticatorsFor(schemes map[string]spec.SecurityScheme) map[string]runtime.Authenticator {
func (o *ZrokAPI) AuthenticatorsFor(schemes map[string]spec.SecurityScheme) map[string]runtime.Authenticator {
return nil
}
// Authorizer returns the registered authorizer
func (o *ZrokClientAPI) Authorizer() runtime.Authorizer {
func (o *ZrokAPI) Authorizer() runtime.Authorizer {
return nil
}
// ConsumersFor gets the consumers for the specified media types.
// MIME type parameters are ignored here.
func (o *ZrokClientAPI) ConsumersFor(mediaTypes []string) map[string]runtime.Consumer {
func (o *ZrokAPI) ConsumersFor(mediaTypes []string) map[string]runtime.Consumer {
result := make(map[string]runtime.Consumer, len(mediaTypes))
for _, mt := range mediaTypes {
switch mt {
@ -207,7 +207,7 @@ func (o *ZrokClientAPI) ConsumersFor(mediaTypes []string) map[string]runtime.Con
// ProducersFor gets the producers for the specified media types.
// MIME type parameters are ignored here.
func (o *ZrokClientAPI) ProducersFor(mediaTypes []string) map[string]runtime.Producer {
func (o *ZrokAPI) ProducersFor(mediaTypes []string) map[string]runtime.Producer {
result := make(map[string]runtime.Producer, len(mediaTypes))
for _, mt := range mediaTypes {
switch mt {
@ -223,7 +223,7 @@ func (o *ZrokClientAPI) ProducersFor(mediaTypes []string) map[string]runtime.Pro
}
// HandlerFor gets a http.Handler for the provided operation method and path
func (o *ZrokClientAPI) HandlerFor(method, path string) (http.Handler, bool) {
func (o *ZrokAPI) HandlerFor(method, path string) (http.Handler, bool) {
if o.handlers == nil {
return nil, false
}
@ -238,8 +238,8 @@ func (o *ZrokClientAPI) HandlerFor(method, path string) (http.Handler, bool) {
return h, ok
}
// Context returns the middleware context for the zrok client API
func (o *ZrokClientAPI) Context() *middleware.Context {
// Context returns the middleware context for the zrok API
func (o *ZrokAPI) Context() *middleware.Context {
if o.context == nil {
o.context = middleware.NewRoutableContext(o.spec, o, nil)
}
@ -247,7 +247,7 @@ func (o *ZrokClientAPI) Context() *middleware.Context {
return o.context
}
func (o *ZrokClientAPI) initHandlerCache() {
func (o *ZrokAPI) initHandlerCache() {
o.Context() // don't care about the result, just that the initialization happened
if o.handlers == nil {
o.handlers = make(map[string]map[string]http.Handler)
@ -261,7 +261,7 @@ func (o *ZrokClientAPI) initHandlerCache() {
// Serve creates a http handler to serve the API over HTTP
// can be used directly in http.ListenAndServe(":8000", api.Serve(nil))
func (o *ZrokClientAPI) Serve(builder middleware.Builder) http.Handler {
func (o *ZrokAPI) Serve(builder middleware.Builder) http.Handler {
o.Init()
if o.Middleware != nil {
@ -274,24 +274,24 @@ func (o *ZrokClientAPI) Serve(builder middleware.Builder) http.Handler {
}
// Init allows you to just initialize the handler cache, you can then recompose the middleware as you see fit
func (o *ZrokClientAPI) Init() {
func (o *ZrokAPI) Init() {
if len(o.handlers) == 0 {
o.initHandlerCache()
}
}
// RegisterConsumer allows you to add (or override) a consumer for a media type.
func (o *ZrokClientAPI) RegisterConsumer(mediaType string, consumer runtime.Consumer) {
func (o *ZrokAPI) RegisterConsumer(mediaType string, consumer runtime.Consumer) {
o.customConsumers[mediaType] = consumer
}
// RegisterProducer allows you to add (or override) a producer for a media type.
func (o *ZrokClientAPI) RegisterProducer(mediaType string, producer runtime.Producer) {
func (o *ZrokAPI) RegisterProducer(mediaType string, producer runtime.Producer) {
o.customProducers[mediaType] = producer
}
// AddMiddlewareFor adds a http middleware to existing handler
func (o *ZrokClientAPI) AddMiddlewareFor(method, path string, builder middleware.Builder) {
func (o *ZrokAPI) AddMiddlewareFor(method, path string, builder middleware.Builder) {
um := strings.ToUpper(method)
if path == "/" {
path = ""

View File

@ -42,8 +42,8 @@ func init() {
}
}
// NewServer creates a new api zrok client server but does not configure it
func NewServer(api *operations.ZrokClientAPI) *Server {
// NewServer creates a new api zrok server but does not configure it
func NewServer(api *operations.ZrokAPI) *Server {
s := new(Server)
s.shutdown = make(chan struct{})
@ -66,14 +66,14 @@ func (s *Server) ConfigureFlags() {
}
}
// Server for the zrok client API
// Server for the zrok API
type Server struct {
EnabledListeners []string `long:"scheme" description:"the listeners to enable, this can be repeated and defaults to the schemes in the swagger spec"`
CleanupTimeout time.Duration `long:"cleanup-timeout" description:"grace period for which to wait before killing idle connections" default:"10s"`
GracefulTimeout time.Duration `long:"graceful-timeout" description:"grace period for which to wait before shutting down the server" default:"15s"`
MaxHeaderSize flagext.ByteSize `long:"max-header-size" description:"controls the maximum number of bytes the server will read parsing the request header's keys and values, including the request line. It does not limit the size of the request body." default:"1MiB"`
SocketPath flags.Filename `long:"socket-path" description:"the unix socket to listen on" default:"/var/run/zrok-client.sock"`
SocketPath flags.Filename `long:"socket-path" description:"the unix socket to listen on" default:"/var/run/zrok.sock"`
domainSocketL net.Listener
Host string `long:"host" description:"the IP to listen on" default:"localhost" env:"HOST"`
@ -95,7 +95,7 @@ type Server struct {
TLSWriteTimeout time.Duration `long:"tls-write-timeout" description:"maximum duration before timing out write of the response"`
httpsServerL net.Listener
api *operations.ZrokClientAPI
api *operations.ZrokAPI
handler http.Handler
hasListeners bool
shutdown chan struct{}
@ -125,7 +125,7 @@ func (s *Server) Fatalf(f string, args ...interface{}) {
}
// SetAPI configures the server with the specified API. Needs to be called before Serve
func (s *Server) SetAPI(api *operations.ZrokClientAPI) {
func (s *Server) SetAPI(api *operations.ZrokAPI) {
if api == nil {
s.api = nil
s.handler = nil
@ -186,13 +186,13 @@ func (s *Server) Serve() (err error) {
servers = append(servers, domainSocket)
wg.Add(1)
s.Logf("Serving zrok client at unix://%s", s.SocketPath)
s.Logf("Serving zrok at unix://%s", s.SocketPath)
go func(l net.Listener) {
defer wg.Done()
if err := domainSocket.Serve(l); err != nil && err != http.ErrServerClosed {
s.Fatalf("%v", err)
}
s.Logf("Stopped serving zrok client at unix://%s", s.SocketPath)
s.Logf("Stopped serving zrok at unix://%s", s.SocketPath)
}(s.domainSocketL)
}
@ -216,13 +216,13 @@ func (s *Server) Serve() (err error) {
servers = append(servers, httpServer)
wg.Add(1)
s.Logf("Serving zrok client at http://%s", s.httpServerL.Addr())
s.Logf("Serving zrok at http://%s", s.httpServerL.Addr())
go func(l net.Listener) {
defer wg.Done()
if err := httpServer.Serve(l); err != nil && err != http.ErrServerClosed {
s.Fatalf("%v", err)
}
s.Logf("Stopped serving zrok client at http://%s", l.Addr())
s.Logf("Stopped serving zrok at http://%s", l.Addr())
}(s.httpServerL)
}
@ -309,13 +309,13 @@ func (s *Server) Serve() (err error) {
servers = append(servers, httpsServer)
wg.Add(1)
s.Logf("Serving zrok client at https://%s", s.httpsServerL.Addr())
s.Logf("Serving zrok at https://%s", s.httpsServerL.Addr())
go func(l net.Listener) {
defer wg.Done()
if err := httpsServer.Serve(l); err != nil && err != http.ErrServerClosed {
s.Fatalf("%v", err)
}
s.Logf("Stopped serving zrok client at https://%s", l.Addr())
s.Logf("Stopped serving zrok at https://%s", l.Addr())
}(tls.NewListener(s.httpsServerL, httpsServer.TLSConfig))
}

View File

@ -1,6 +1,6 @@
info:
description: Client access service
title: zrok Client
description: zrok client access
title: zrok
version: 1.0.0
paths:
/:
@ -9,7 +9,7 @@ paths:
- metadata
responses:
200:
description: Retrieve the current server version
description: retrieve the current server version
schema:
$ref: "#/definitions/version"
definitions: