atreugo

package module
v3.1.2 Latest Latest
Warning

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

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

README

Atreugo

Build Status Coverage Status Go Report Card GoDoc

Micro-framework to make simple the use of routing and middlewares in 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

Overview

Package atreugo is a micro-framework to make simple the use of routing and middlewares with all optimizations of fasthttp

This micro-framework is build on top erikdubbelboer's fasthttp fork.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FileResponse added in v3.1.0

func FileResponse(ctx *fasthttp.RequestCtx, fileName, filePath, mimeType string) error

FileResponse return a streaming response with file data.

func HTTPResponse

func HTTPResponse(ctx *fasthttp.RequestCtx, body []byte, statusCode ...int) error

HTTPResponse return response with body in html format

func JSONResponse

func JSONResponse(ctx *fasthttp.RequestCtx, body interface{}, statusCode ...int) error

JSONResponse return response with body in json format

func RawResponse added in v3.1.0

func RawResponse(ctx *fasthttp.RequestCtx, body []byte, statusCode ...int) error

RawResponse returns response without encoding the body.

func RedirectResponse

func RedirectResponse(ctx *fasthttp.RequestCtx, url string, statusCode int) error

RedirectResponse redirect request to an especific url

func TextResponse added in v3.1.0

func TextResponse(ctx *fasthttp.RequestCtx, body []byte, statusCode ...int) error

TextResponse return response with body in text format

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{}

JSON is a map whose key is a string and whose value an interface

type Middleware

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

Middleware must process all incoming requests before defined views.

type View

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