Help us shape the future of Go! Take the 2026 Go Developer Survey

restapi

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 4, 2024 License: MPL-2.0 Imports: 33 Imported by: 0

Documentation

Overview

Package restapi Health Tracker App

Track Health Parameters
Schemes:
  http
  https
Host: localhost:8080
BasePath: /
Version: 1.0.0

Consumes:
  - application/json

Produces:
  - application/json

swagger:meta

Index

Constants

This section is empty.

Variables

View Source
var (
	// SwaggerJSON embedded version of the swagger document used at generation time
	SwaggerJSON json.RawMessage
	// FlatSwaggerJSON embedded flattened version of the swagger document used at generation time
	FlatSwaggerJSON json.RawMessage
)

Functions

func AuthGetUser

func AuthGetUser(ctx context.Context, auth *Auth, token string) (schemas.User, error)

func DeleteHandler

func DeleteHandler[Schema SchemaAuth, N int32 | int64 | uint](r *http.Request, db *gorm.DB, id N, principal *schemas.User, successFunc func() middleware.Responder) middleware.Responder

func FromModelFunc

func FromModelFunc[Model interface{}, SchemaPtr Convertable[Model]](schema SchemaPtr, ctx context.Context, db *gorm.DB, model Model) error

func GetEnv

func GetEnv(key string) (string, error)

func GetEnvOrPanic

func GetEnvOrPanic(key string) string

func GetHandler

func GetHandler[Model interface{}, Schema SchemaAuth, N int32 | int64 | uint](
	r *http.Request,
	db *gorm.DB,
	id N,
	principal *schemas.User,
	toModelFunc func(schema *Schema, ctx context.Context, db *gorm.DB) (Model, error),
	successFunc func(model *Model) middleware.Responder,
) middleware.Responder

func ListHandler

func ListHandler[Model interface{}, Schema SchemaAuth, Parent interface{}](
	r *http.Request,
	db *gorm.DB,
	principal *schemas.User,
	getParent func(ctx context.Context, db *gorm.DB) (Parent, int, error),
	listItems func(ctx context.Context, db *gorm.DB, parent *Parent) ([]Schema, error),
	toModelFunc func(schema *Schema, ctx context.Context, db *gorm.DB) (Model, error),
	successFunc func(modelList []*Model) middleware.Responder,
) middleware.Responder

func PostHandler

func PostHandler[Model interface{}, Schema SchemaAuth, N int32 | int64 | uint](
	r *http.Request,
	db *gorm.DB,
	body *Model,
	parentId N,
	principal *schemas.User,
	fromModelFunc func(schema *Schema, ctx context.Context, db *gorm.DB, model Model) error,
	modelAuthFunc func(ctx context.Context, db *gorm.DB, principal *schemas.User, schema *Schema) error,
	setParentIdFunc func(schema *Schema, parentId uint),
	toModelFunc func(schema *Schema, ctx context.Context, db *gorm.DB) (Model, error),
	successFunc func(model *Model) middleware.Responder,
) middleware.Responder

func PutHandler

func PutHandler[Model interface{}, Schema SchemaAuth, N int32 | int64 | uint](
	r *http.Request,
	db *gorm.DB,
	body *Model,
	id N,
	principal *schemas.User,
	fromModelFunc func(schema *Schema, ctx context.Context, db *gorm.DB, model Model) error,
	modelAuthFunc func(ctx context.Context, db *gorm.DB, principal *schemas.User, schema *Schema) error,
	replaceAssociationsFunc func(schema *Schema, ctx context.Context, db *gorm.DB) error,
	toModelFunc func(schema *Schema, ctx context.Context, db *gorm.DB) (Model, error),
	successFunc func(model *Model) middleware.Responder,
) middleware.Responder

func SetParentIdFunc

func SetParentIdFunc[Model interface{}, SchemaPtr Convertable[Model]](schema SchemaPtr, parentId uint)

func ToModelFunc

func ToModelFunc[Model interface{}, SchemaPtr Convertable[Model]](schema SchemaPtr, ctx context.Context, db *gorm.DB) (Model, error)

Types

type Auth

type Auth struct {
	// contains filtered or unexported fields
}

func AuthSetup

func AuthSetup(db *gorm.DB, issuer string, clientId string, clientSecret string, frontendClientId string) (Auth, error)

type Convertable

type Convertable[T interface{}] interface {
	ToModel(ctx context.Context, db *gorm.DB) (T, error)
	FromModel(ctx context.Context, db *gorm.DB, model T) error
	SetParentId(id uint)
}

type SchemaAuth

type SchemaAuth interface {
	GetOwnerID(ctx context.Context, db *gorm.DB) uint
}

type Server

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 `` /* 231-byte string literal not displayed */

	SocketPath flags.Filename `long:"socket-path" description:"the unix socket to listen on" default:"/var/run/hta.sock"`

	Host         string        `long:"host" description:"the IP to listen on" default:"localhost" env:"HOST"`
	Port         int           `long:"port" description:"the port to listen on for insecure connections, defaults to a random value" env:"PORT"`
	ListenLimit  int           `long:"listen-limit" description:"limit the number of outstanding requests"`
	KeepAlive    time.Duration `` /* 169-byte string literal not displayed */
	ReadTimeout  time.Duration `long:"read-timeout" description:"maximum duration before timing out read of the request" default:"30s"`
	WriteTimeout time.Duration `long:"write-timeout" description:"maximum duration before timing out write of the response" default:"60s"`

	TLSHost           string         `long:"tls-host" description:"the IP to listen on for tls, when not specified it's the same as --host" env:"TLS_HOST"`
	TLSPort           int            `long:"tls-port" description:"the port to listen on for secure connections, defaults to a random value" env:"TLS_PORT"`
	TLSCertificate    flags.Filename `long:"tls-certificate" description:"the certificate to use for secure connections" env:"TLS_CERTIFICATE"`
	TLSCertificateKey flags.Filename `long:"tls-key" description:"the private key to use for secure connections" env:"TLS_PRIVATE_KEY"`
	TLSCACertificate  flags.Filename `long:"tls-ca" description:"the certificate authority file to be used with mutual tls auth" env:"TLS_CA_CERTIFICATE"`
	TLSListenLimit    int            `long:"tls-listen-limit" description:"limit the number of outstanding requests"`
	TLSKeepAlive      time.Duration  `` /* 160-byte string literal not displayed */
	TLSReadTimeout    time.Duration  `long:"tls-read-timeout" description:"maximum duration before timing out read of the request"`
	TLSWriteTimeout   time.Duration  `long:"tls-write-timeout" description:"maximum duration before timing out write of the response"`
	// contains filtered or unexported fields
}

Server for the hta API

func NewServer

func NewServer(api *operations.HtaAPI) *Server

NewServer creates a new api hta server but does not configure it

func (*Server) ConfigureAPI

func (s *Server) ConfigureAPI()

ConfigureAPI configures the API and handlers.

func (*Server) ConfigureFlags

func (s *Server) ConfigureFlags()

ConfigureFlags configures the additional flags defined by the handlers. Needs to be called before the parser.Parse

func (*Server) Fatalf

func (s *Server) Fatalf(f string, args ...interface{})

Fatalf logs message either via defined user logger or via system one if no user logger is defined. Exits with non-zero status after printing

func (*Server) GetHandler

func (s *Server) GetHandler() http.Handler

GetHandler returns a handler useful for testing

func (*Server) HTTPListener

func (s *Server) HTTPListener() (net.Listener, error)

HTTPListener returns the http listener

func (*Server) Listen

func (s *Server) Listen() error

Listen creates the listeners for the server

func (*Server) Logf

func (s *Server) Logf(f string, args ...interface{})

Logf logs message either via defined user logger or via system one if no user logger is defined.

func (*Server) Serve

func (s *Server) Serve() (err error)

Serve the api

func (*Server) SetAPI

func (s *Server) SetAPI(api *operations.HtaAPI)

SetAPI configures the server with the specified API. Needs to be called before Serve

func (*Server) SetHandler

func (s *Server) SetHandler(handler http.Handler)

SetHandler allows for setting a http handler on this server

func (*Server) Shutdown

func (s *Server) Shutdown() error

Shutdown server and clean up resources

func (*Server) TLSListener

func (s *Server) TLSListener() (net.Listener, error)

TLSListener returns the https listener

func (*Server) UnixListener

func (s *Server) UnixListener() (net.Listener, error)

UnixListener returns the domain socket listener

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL