webkit

package module
v0.0.0-...-e97808e Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2020 License: MIT Imports: 17 Imported by: 0

README

go-webkit

Build codecov Go Report Card

A webkit for Go with simple APIs to use. It solves common problems of a web server so engineers can focus on business logic.

Features

  • Graceful shutdown

Quick Start

Installation
go get github.com/bongnv/go-webkit
Example
package main

import (
	"context"
	"log"

	"github.com/bongnv/go-webkit"
)

func main() {
	app := webkit.New()
	app.GET("/hello-world", func(ctx context.Context, req webkit.Request) (interface{}, error) {
		return "OK", nil
	})
	log.Println(app.Run())
}

Usages

Options

An Option customizes an Application and these are available Option:

WithLogger

WithLogger allows to specify a custom implementation of the logger.

  logger := log.New(os.Stderr, "", log.LstdFlags)
  app := webkit.New(WithLogger(logger))
Route Options

A RouteOption customizes a route. It can be used to add middlewares like Recovery().

For convenience, a RouteOption can be a Option for the Application. In this case, the RouteOption will be applied to all routes.

WithRecovery

WithRecovery recovers from panics and returns error with 500 status code to clients.

    app.GET("/hello-world", helloWorld, WithRecovery())
WithDecoder

WithDecoder specifies a custom logic for decoding the request to a request DTO.

   app.GET("/hello-world", helloWorld, WithDecoder(customDecoder))
WithCORS

WithCORS enables the support for Cross-Origin Resource Sharing. Ref: https://developer.mozilla.org/en/docs/Web/HTTP/Access_control_CORS.

  app := webkit.New(WithCORS(webkit.DefaultCORSConfig))
  // or
  app.GET("/hello-world", helloWorld, WithCORS(webkit.DefaultCORSConfig))

Documentation

Index

Constants

View Source
const (
	HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials"
	HeaderAccessControlAllowHeaders     = "Access-Control-Allow-Headers"
	HeaderAccessControlAllowMethods     = "Access-Control-Allow-Methods"
	HeaderAccessControlAllowOrigin      = "Access-Control-Allow-Origin"
	HeaderAccessControlMaxAge           = "Access-Control-Max-Age"
	HeaderAccessControlRequestHeaders   = "Access-Control-Request-Headers"
	HeaderAccessControlRequestMethod    = "Access-Control-Request-Method"
	HeaderContentType                   = "Content-Type"
	HeaderOrigin                        = "Origin"
	HeaderVary                          = "Vary"
)

Headers

Variables

View Source
var DefaultCORSConfig = CORSConfig{
	AllowOrigins: []string{"*"},
	AllowMethods: []string{http.MethodGet, http.MethodHead, http.MethodPut, http.MethodPatch, http.MethodPost, http.MethodDelete},
}

DefaultCORSConfig is the default configuration for the WithCORS middleware.

Functions

This section is empty.

Types

type Application

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

Application is a web application.

func Default

func Default() *Application

Default returns an Application with a default set of configurations.

func New

func New(opts ...Option) *Application

New creates a new application.

func (*Application) DELETE

func (app *Application) DELETE(path string, h Handler, opts ...RouteOption)

DELETE registers a new DELETE route for a path with handler.

func (*Application) GET

func (app *Application) GET(path string, h Handler, opts ...RouteOption)

GET registers a new GET route for a path with handler.

func (*Application) PATCH

func (app *Application) PATCH(path string, h Handler, opts ...RouteOption)

PATCH registers a new PATCH route for a path with handler.

func (*Application) POST

func (app *Application) POST(path string, h Handler, opts ...RouteOption)

POST registers a new POST route for a path with handler.

func (*Application) PUT

func (app *Application) PUT(path string, h Handler, opts ...RouteOption)

PUT registers a new PUT route for a path with handler.

func (*Application) Run

func (app *Application) Run() error

Run starts an HTTP server.

type CORSConfig

type CORSConfig struct {
	AllowOrigins     []string
	AllowMethods     []string
	AllowHeaders     []string
	AllowCredentials bool
	MaxAge           int
}

CORSConfig defines the config for WithCORS middleware.

type Decoder

type Decoder interface {
	// Decode decodes a request to a struct. req.PostForm is called in advanced.
	Decode(obj interface{}, req *http.Request) error
}

Decoder defines a request decoder.

type Encoder

type Encoder interface {
	// Encode encodes obj and writes to http.ResponseWriter.
	Encode(w http.ResponseWriter, obj interface{}) error
}

Encoder define a request decoder.

type Handler

type Handler func(ctx context.Context, req Request) (interface{}, error)

Handler defines a function to serve HTTP requests.

type Logger

type Logger interface {
	// Println prints out logs like fmt.Println.
	Println(...interface{})
}

Logger defines a Logger.

type Middleware

type Middleware func(Handler) Handler

Middleware defines a middleware to provide additional logic.

func WithCORS

func WithCORS(cfg CORSConfig) Middleware

WithCORS returns a middleware to support Cross-Origin Resource Sharing.

func (Middleware) Apply

func (m Middleware) Apply(app *Application)

Apply implements Option.

func (Middleware) ApplyRoute

func (m Middleware) ApplyRoute(r *route)

ApplyRoute implements RouteOption.

type Option

type Option interface {
	Apply(app *Application)
}

Option defines an application Option.

type OptionFn

type OptionFn func(app *Application)

OptionFn defines a function that implements Option

func WithLogger

func WithLogger(l Logger) OptionFn

WithLogger specifies a custom Logger for tha application.

func (OptionFn) Apply

func (opt OptionFn) Apply(app *Application)

Apply implements Option.

type Request

type Request interface {
	// HTTPRequest returns the http.Request.
	HTTPRequest() *http.Request
	// Decode decodes the request to an object.
	Decode(obj interface{}) error
	// ResponseHeader returns the header map that will be sent.
	ResponseHeader() http.Header
}

Request defines a HTTP request.

type RouteOption

type RouteOption interface {
	ApplyRoute(r *route)
}

RouteOption defines an option to customize a route.

type RouteOptionFn

type RouteOptionFn func(r *route)

RouteOptionFn defines a function implementation of RouteOption.

func WithDecoder

func WithDecoder(d Decoder) RouteOptionFn

WithDecoder specifies the decoder which will be used.

func WithRecovery

func WithRecovery() RouteOptionFn

WithRecovery returns a middleware which recovers from panics.

func (RouteOptionFn) Apply

func (fn RouteOptionFn) Apply(app *Application)

Apply implements Option.

func (RouteOptionFn) ApplyRoute

func (fn RouteOptionFn) ApplyRoute(r *route)

ApplyRoute implements RouteOption.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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