supernova

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2017 License: GPL-3.0 Imports: 15 Imported by: 1

README

Supernova

GoDoc Go Report Card Build Status

An express like router for fasthttp

Provides a lot of the same methods and functionality as Expressjs

Example

package main

import (
	"fmt"

	"github.com/MordFustang21/supernova"
)

func main() {
	// Get new instance of server
	s := supernova.New()

	//Middleware Example
	s.Use(func(req *supernova.Request, next func()) {
		req.Response.Header.Set("Powered-By", "supernova")
		next()
	})

	//Route Examples
	s.Post("/test/taco/:apple", func(req *supernova.Request) {

		// Get query parameters
		limit := req.QueryParam("limit")

		type test struct {
			Apple string
		}

		// Read JSON into struct from body
		var testS test
		err := req.ReadJSON(&testS)
		if err != nil {
			fmt.Println("Error:", err)
		}

		req.Send("Received data " + limit)
	})

	// Example Get route with route params
	s.Get("/test/:taco/:apple", func(req *supernova.Request) {
		tacoType := req.RouteParam("taco")
		req.Send(tacoType)
	})

	// Resticted routes are used to restict methods other than GET,PUT,POST,DELETE
	s.Restricted("OPTIONS", "/test/stuff", func(req *supernova.Request) {
		req.Send("OPTIONS Request received")
	})

	// Example post returning error
	s.Post("/register", func(req *supernova.Request) {
		if len(req.Request.Body()) == 0 {
			// response code, error message, and any struct you want put into the errors array
			req.Error(500, "Body is empty")
		}
	})

	err := s.ListenAndServe(":8080")
	if err != nil {
		println(err.Error())
	}
}

Documentation

Overview

Package supernova is a fasthttp router that implements a radix tree for fast lookups

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewGracefulListener

func NewGracefulListener(ln net.Listener, maxWaitTime time.Duration) net.Listener

NewGracefulListener wraps the given listener into 'graceful shutdown' listener.

Types

type CachedObj

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

CachedObj represents a static asset

type CachedStatic

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

CachedStatic holds all cached static assets in memory

type GracefulListener

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

GracefulListener is used as custom listener to watch connections

func (*GracefulListener) Accept

func (ln *GracefulListener) Accept() (net.Conn, error)

Accept waits for connection increments count and returns to the listener.

func (*GracefulListener) Addr

func (ln *GracefulListener) Addr() net.Addr

Addr returns the listener's network address.

func (*GracefulListener) Close

func (ln *GracefulListener) Close() error

Close closes the inner listener and waits until all the pending open connections are closed before returning.

type JSONError

type JSONError struct {
	Errors  []interface{} `json:"errors"`
	Code    int           `json:"code"`
	Message string        `json:"message"`
}

JSONError resembles the RESTful standard for an error response

type JSONErrors

type JSONErrors struct {
	Error JSONError `json:"error"`
}

JSONErrors holds the JSONError response

type Middleware

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

Middleware holds all middleware functions

type Node

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

Node holds a single route with accompanying children routes

type Request

type Request struct {
	*fasthttp.RequestCtx

	BaseUrl string

	// Writer is used to write to response body
	Writer io.Writer
	Ctx    context.Context
	// contains filtered or unexported fields
}

Request resembles an incoming request

func NewRequest

func NewRequest(ctx *fasthttp.RequestCtx) *Request

NewRequest creates a new Request pointer for an incoming request

func (*Request) Error

func (r *Request) Error(statusCode int, msg string, errors ...interface{}) (int, error)

Error allows an easy method to set the RESTful standard error response

func (*Request) GetMethod

func (r *Request) GetMethod() string

GetMethod provides a simple way to return the request method type as a string

func (*Request) JSON

func (r *Request) JSON(code int, obj interface{}) (int, error)

JSON marshals the given interface object and writes the JSON response.

func (*Request) QueryParam

func (r *Request) QueryParam(key string) string

QueryParam checks for and returns param or "" if doesn't exist

func (*Request) ReadJSON

func (r *Request) ReadJSON(i interface{}) error

ReadJSON unmarshals request body into the struct provided

func (*Request) RouteParam

func (r *Request) RouteParam(key string) string

RouteParam checks for and returns param or "" if doesn't exist

func (*Request) Send

func (r *Request) Send(data interface{}) (int, error)

Send writes the data to the response body

type Route

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

Route is the construct of a single route pattern

type Server

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

Server represents the router and all associated data

func New

func New() *Server

New returns new supernova router

func (*Server) All

func (sn *Server) All(route string, routeFunc func(*Request))

All adds route for all http methods

func (*Server) Close

func (sn *Server) Close() error

Close closes existing listener

func (*Server) Delete

func (sn *Server) Delete(route string, routeFunc func(*Request))

Delete adds only DELETE method to route

func (*Server) EnableDebug

func (sn *Server) EnableDebug(debug bool)

EnableDebug toggles output for incoming requests

func (*Server) Get

func (sn *Server) Get(route string, routeFunc func(*Request))

Get adds only GET method to route

func (*Server) ListenAndServe

func (sn *Server) ListenAndServe(addr string) error

ListenAndServe starts the server

func (*Server) ListenAndServeTLS

func (sn *Server) ListenAndServeTLS(addr, certFile, keyFile string) error

ListenAndServeTLS starts server with ssl

func (*Server) Post

func (sn *Server) Post(route string, routeFunc func(*Request))

Post adds only POST method to route

func (*Server) Put

func (sn *Server) Put(route string, routeFunc func(*Request))

Put adds only PUT method to route

func (*Server) Restricted

func (sn *Server) Restricted(method, route string, routeFunc func(*Request))

Restricted adds route that is restricted by method

func (*Server) Serve

func (sn *Server) Serve(ln net.Listener) error

Serve serves incoming connections from the given listener.

func (*Server) SetShutDownHandler

func (sn *Server) SetShutDownHandler(shutdownFunc func())

SetShutDownHandler implements function called when SIGTERM signal is received

func (*Server) Use

func (sn *Server) Use(f func(*Request, func()))

Use adds a new function to the middleware stack

Jump to

Keyboard shortcuts

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