server

package
v0.2.8 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2023 License: MIT Imports: 16 Imported by: 4

Documentation

Overview

Package server provides a simple router based https://github.com/gorilla/mux, with a SpecOpenAPIHandler type return the response (not serialized) and an error, as well as a Middleware type to allow middleware for this new SpecOpenAPIHandler type.

You can see middleware implementation in the github.com/mwm-io/gapi/middleware package.

import (
	"http"
	"log"

	"github.com/mwm-io/gapi/server"
)

r := server.NewMux()

// Add your http handlers.
var h SpecOpenAPIHandler
server.AddHandler(r, http.MethodGet, "/hello", h)

// Add your server options here.
err := server.ServeAndHandleShutdown(r)
if err != nil {
	log.Fatal(err)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddDocHandlers

func AddDocHandlers(r *mux.Router, middlewares ...handler.Middleware) error

AddDocHandlers will add the necessary handlers to serve a rapidoc endpoint: 2 endpoints to serve rapidoc.html and oauth-receiver.html from rapidoc and one endpoint to serve the json openapi definition of your API.

func AddHandler

func AddHandler(router *mux.Router, method, path string, f handler.Handler)

AddHandler register a new handler to the given mux router on a given method and path.

func AddHandlerFactory

func AddHandlerFactory(router *mux.Router, method, path string, f handler.Factory)

AddHandlerFactory register a new handler factory to the given mux router on a given method and path. You can use it if you want a new instance of your handler for each call. It must be use useful if :

  • you use a middleware for handle request params like middleware.BodyDecoder, middleware.PathParameters, etc.
  • you store properties in your handler struct during Serve process

func NewMux

func NewMux() *mux.Router

NewMux returns a new *mux.Router.

func NewServer

func NewServer(r *mux.Router, opts ...Option) *http.Server

NewServer returns a new configured *http.Server, using an existing mux.Router. You can override the default configuration using the available Option.

func ServeAndHandleShutdown

func ServeAndHandleShutdown(r *mux.Router, opts ...Option) error

ServeAndHandleShutdown start a *http.Server with the default configuration (overridden by the given Option) This function lock your program until a signal stopping your program is received. (see WithStopSignals)

func ServeAndHandleTlSShutdown

func ServeAndHandleTlSShutdown(r *mux.Router, certCRT, certKey string, opts ...Option) error

ServeAndHandleTlSShutdown start a *http.Server with TLS and the default configuration (overridden by the given Option) This function lock your program until a signal stopping your program is received. (see WithStopSignals)

func StartProcessAndHandleStopSignals

func StartProcessAndHandleStopSignals(process func() error, shutdown func(ctx context.Context) error, opts ...Option) error

StartProcessAndHandleStopSignals starts the given process and listen for os stop signals to stop it, executing the shutdown function. It can also take Option to customize StopSignals, Context, and StopTimeout.

func UseMiddlewares added in v0.1.0

func UseMiddlewares(middlewares ...handler.Middleware)

UseMiddlewares appends a given list of handler.Middleware to middlewares chain.

Middleware can be used to intercept or otherwise modify requests and/or responses, and are executed in list order.

Types

type CORS

type CORS struct {
	AllowedOrigins []string
	AllowedMethods []string
	AllowedHeaders []string
}

CORS contains the CORS configuration for the http server.

type Option

type Option func(*serverOptions)

Option is an option to modify the default configuration of the http server.

func WithCORS

func WithCORS(cors CORS) Option

WithCORS sets the cors configuration. By default, authorize "*" with methods "GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS" and headers "Content-Type", "Authorization"

func WithContext

func WithContext(ctx context.Context) Option

WithContext specify a parent context for the *http.Server. It will be passed to every request. By default, creates a new context.

func WithPort

func WithPort(port string) Option

WithPort sets the server port on which to listen to. If the port is empty, it will first look the PORT environment variable. If the PORT environment variable is empty, it will take the default value. (8080 for http and 443 for https)

func WithStopSignals

func WithStopSignals(signals ...os.Signal) Option

WithStopSignals specify on which os.Signal we must shut down the server. By default, os.Interrupt, syscall.SIGINT and syscall.SIGTERM

func WithStopTimeout

func WithStopTimeout(d time.Duration) Option

WithStopTimeout set the timeout when the shutting down the server. By default, 3 seconds.

func WithStrictSlash

func WithStrictSlash(strictSlash bool) Option

WithStrictSlash specify if we use the strictSlash configuration or not. When true, if the route path is "/path/", accessing "/path" will perform a redirect to the former and vice versa. In other words, your application will always see the path as specified in the route.

Jump to

Keyboard shortcuts

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