atreugo

package module
v4.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2018 License: GPL-3.0 Imports: 14 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.

The project use dep manager 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
  • GracefulShutdown (bool): Start server with graceful shutdown

Note:

*atreugo.RequestCtx is equal than *fasthttp.RequestCtx, but adding extra funtionality, so you can use the same functions of *fasthttp.RequestCtx. Don't worry 😄

Example:

package main

import (
	"errors"

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

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

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

	fnMiddlewareTwo := func(ctx *atreugo.RequestCtx) (int, error) {
		// Disable this middleware if you don't want to see this error
		return fasthttp.StatusBadRequest, errors.New("Error example")
	}

	server.UseMiddleware(fnMiddlewareOne, fnMiddlewareTwo)

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

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

	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 valyala's fasthttp fork.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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) SetLogOutput

func (s *Atreugo) SetLogOutput(output io.Writer)

SetLogOutput set log output of server

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
	GracefulShutdown 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 *RequestCtx) (int, error)

Middleware must process all incoming requests before defined views.

type RequestCtx

type RequestCtx struct {
	*fasthttp.RequestCtx
}

RequestCtx context wrapper for fasthttp.RequestCtx to adds extra funtionality

func (*RequestCtx) FileResponse

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

FileResponse return a streaming response with file data.

func (*RequestCtx) HTTPResponse

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

HTTPResponse return response with body in html format

func (*RequestCtx) JSONResponse

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

JSONResponse return response with body in json format

func (*RequestCtx) RawResponse

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

RawResponse returns response without encoding the body.

func (*RequestCtx) RedirectResponse

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

RedirectResponse redirect request to an especific url

func (*RequestCtx) TextResponse

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

TextResponse return response with body in text format

type View

type View func(ctx *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