atreugo

package module
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2018 License: GPL-3.0 Imports: 13 Imported by: 0

README

Atreugo

Go Report Card GoDoc

Micro-framework to make simple the use of routing and middlewares of fasthttp.

Is based on erikdubbelboer's fasthttp fork that it more active than valyala's fasthttp

The project use dep manager dependencies.

Go dependencies:

Atreugo configuration:

  • Host (string)
  • Port (int)
  • LogLevel (string): See levels
  • Compress (bool): Compress response body
  • TLSEnable (bool): Enable HTTPS
  • CertKey (string): Path of cert.key file
  • CertFile (string): Path of cert.pem file
  • GracefulEnable (bool): Start server with graceful shutdown

Example:

package main

import (
	"errors"

	"github.com/erikdubbelboer/fasthttp"
	"github.com/savsgio/atreugo"
)

func main() {
	// Configuration for Atreugo server
	config := &atreugo.Config{
		Host: "0.0.0.0",
		Port: 8000,
	}

	// New instance of atreugo server with your config
	server := atreugo.New(config)

	// Middlewares
	fnMiddlewareOne := func(ctx *fasthttp.RequestCtx) (int, error) {
		return fasthttp.StatusOK, nil
	}

	fnMiddlewareTwo := func(ctx *fasthttp.RequestCtx) (int, error) {
		return fasthttp.StatusBadRequest, errors.New("Error message")
	}

	// Register middlewares
	server.UseMiddleware(fnMiddlewareOne, fnMiddlewareTwo)


	// Views
	server.Path("GET", "/", func(ctx *fasthttp.RequestCtx) error {
		return atreugo.HttpResponse(ctx, []byte("<h1>Atreugo Micro-Framework</h1>"))
	})

	server.Path("GET", "/jsonPage", func(ctx *fasthttp.RequestCtx) error {
		return atreugo.JsonResponse(ctx, atreugo.Json{"Atreugo": true})
	})

	// Start server
	err := server.ListenAndServe()
	if err != nil {
		panic(err)
	}
}

Useful third-party libraries

Contributing

Feel free to contribute it or fork me... 😉

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func B2S

func B2S(b []byte) string

B2S convert bytes array to string without memory allocation

func HttpResponse

func HttpResponse(ctx *fasthttp.RequestCtx, response []byte, statusCode ...int) error

func JsonResponse

func JsonResponse(ctx *fasthttp.RequestCtx, response interface{}, statusCode ...int) error

Types

type Atreugo

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

Atreugo struct for make up a server

func New

func New(cfg *Config) *Atreugo

New create a new instance of Atreugo Server

func (*Atreugo) ListenAndServe

func (s *Atreugo) ListenAndServe() error

ListenAndServe start Atreugo server according to the configuration

func (*Atreugo) Path

func (s *Atreugo) Path(httpMethod string, url string, viewFn View)

Path add the views to serve

func (*Atreugo) Static

func (s *Atreugo) Static(rootStaticDirPath string)

Static add view for static files

func (*Atreugo) UseMiddleware

func (s *Atreugo) UseMiddleware(fns ...Middleware)

UseMiddleware register middleware functions that viewHandler will use

type Config

type Config struct {
	Host           string
	Port           int
	LogLevel       string
	Compress       bool
	TLSEnable      bool
	CertKey        string
	CertFile       string
	GracefulEnable bool
}

Config config for Atreugo

type Json

type Json map[string]interface{}

type Middleware added in v2.0.1

type Middleware func(ctx *fasthttp.RequestCtx) (int, error)

Middleware must process all incoming requests before defined views.

type View added in v2.0.1

type View func(ctx *fasthttp.RequestCtx) error

View must process incoming requests.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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