arah

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

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

Go to latest
Published: Mar 3, 2023 License: MIT Imports: 7 Imported by: 0

README

Arah by 🦊

Library for managing host and route HTTP based on Echo.



Content

  1. Quickplay
  2. Host
  3. Route
  4. Start HTTP



Quickplay

Back to top


You need to setup hosts and routes, then start the HTTP server, for example.

package main

import (
    "net/http"

    "github.com/iamaredfoxx/arah"
    "github.com/labstack/echo/v4"
)

func main() {
    arah.Register(

        // register host
        arah.HostHandler{
            Route: []arah.RouteInterface{
                exampleRoute{}, // initiate the route
            },
        },

    )

    // serve HTTP
    arah.Bind(
        arah.Start{
            Port: "8080",
        },
    )
}

// This is route
type exampleRoute struct {}

func (exampleRoute) Create(rp arah.RoutePathInterface) {
    rp.Get("/example", func (c echo.Context) error {
        return c.String(http.StatusOK, "example")
    })
}



Host

Back to top


Don't you think you need different host for different section too 😉




Hostname

Back to top


You can specify the hostname by filling in the hostname option.

Default host is required, to do so you can fill the hostname config with "" (empty string)

arah.HostHandler{
    Hostname: "book.example.com"
}



Config

Back to top


More configuration for the host that you can do.

type HostConfiguration struct {
	Debug            bool
	HideBanner       bool
	DisableHTTP2     bool
	ReadTimeout      int64
	WriteTimeout     int64
	Listener         net.Listener
	Validator        echo.Validator
	Binder           echo.Binder
	Renderer         echo.Renderer
	IPExtractor      echo.IPExtractor
	Middleware       []echo.MiddlewareFunc
	HTTPErrorHandler func(err error, c echo.Context)
}

for more detail check out https://echo.labstack.com/guide/customization



List Route

Back to top


Register all your routes in here, like so.

arah.HostHandler{
    Route: []arah.RouteInterface{
        exampleRoute{},
    }
}



Route

Back to top


This is all you can do for now.

Group(group string, f func(rg RoutePathInterface), m ...echo.MiddlewareFunc)

Get(prefix string, f echo.HandlerFunc, m ...echo.MiddlewareFunc) *routePath

Post(prefix string, f echo.HandlerFunc, m ...echo.MiddlewareFunc) *routePath

Put(prefix string, f echo.HandlerFunc, m ...echo.MiddlewareFunc) *routePath

Patch(prefix string, f echo.HandlerFunc, m ...echo.MiddlewareFunc) *routePath

Delete(prefix string, f echo.HandlerFunc, m ...echo.MiddlewareFunc) *routePath

Any(prefix string, f echo.HandlerFunc, m ...echo.MiddlewareFunc) []*routePath

Use(middleware ...echo.MiddlewareFunc)

Use all of that like this

type exampleRoute struct {}

func (exampleRoute) Create(rp arah.RoutePathInterface) {

    rp.Use(func(next HandlerFunc) HandlerFunc{
        // should be return next
    })

    rp.Group("/example", f func(rg RoutePathInterface) {
        rg.Get("/" func (c echo.Context) error {
            // should be return Echo response
        })
    })

    rp.Get("/example", func (c echo.Context) error {
        // should be return Echo response
    })

    rp.Post("/example", func (c echo.Context) error {
        // should be return Echo response
    })

    rp.Put("/example", func (c echo.Context) error {
        // should be return Echo response
    })

    rp.Patch("/example", func (c echo.Context) error {
        // should be return Echo response
    })

    rp.Delete("/example", func (c echo.Context) error {
        // should be return Echo response
    })

    rp.Any("/example", func (c echo.Context) error {
        // should be return Echo response
    })

}



Start HTTP

Back to top


To start HTTP you need different method, for example

arah.Bind(
    arah.Start{
        Port: "8080",
    },
)

All start struct you can use

// Start HTTP
type Start struct {
	Port string
}

// Start HTTP with TLS
type StartTLS struct {
	Port              string
	CertFile, KeyFile interface{}
}


// Start HTTP with auto creating TLS
type StartAutoTLS struct {
	Port string
}


// Start HTTP 2
type StartH2CServer struct {
	Port  string
	HTTP2 http2.Server
}


// Start HTTP with custom configuration HTTP Server
type StartServer struct {
	Server http.Server
}

// If echo has custom Listener, run this one
type StartListener struct {
}

For more detail check out https://echo.labstack.com/guide/http_server

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorDefaultHostNotFound = errors.New("default host not found")
	ErrorHostNotFound        = errors.New("host not found")
	ErrorHostAlreadyDefined  = errors.New("host already defined")
	ErrorRouteNotFound       = errors.New("route not found")
)

Functions

func Bind

func Bind(s StartInterface) error

Bind Default Host into http and serve it

func Host

func Host(hostname string) (*host, error)

Return Host with specified name

func IsDefaultHostNotFound

func IsDefaultHostNotFound(err error) bool

func IsHostAlreadyDefined

func IsHostAlreadyDefined(err error) bool

func IsHostNotFound

func IsHostNotFound(err error) bool

func IsRouteNotFound

func IsRouteNotFound(err error) bool

func Name

func Name(routeName string, params ...interface{}) (string, error)

Return Route Path with specified name

func Register

func Register(hostHandler ...HostHandler) error

Register the Host Handler

Types

type HostConfiguration

type HostConfiguration struct {
	Debug            bool
	HideBanner       bool
	DisableHTTP2     bool
	ReadTimeout      int64
	WriteTimeout     int64
	Listener         net.Listener
	Validator        echo.Validator
	Binder           echo.Binder
	Renderer         echo.Renderer
	IPExtractor      echo.IPExtractor
	Middleware       []echo.MiddlewareFunc
	HTTPErrorHandler func(err error, c echo.Context)
}

Host configuration

type HostHandler

type HostHandler struct {
	Hostname string
	Config   *HostConfiguration
	Route    []RouteInterface
}

The host handler

type RouteInterface

type RouteInterface interface {
	Create(RoutePathInterface)
}

Route Implementation

type RoutePathInterface

type RoutePathInterface interface {
	Group(group string, f func(rg RoutePathInterface), m ...echo.MiddlewareFunc)
	Get(prefix string, f echo.HandlerFunc, m ...echo.MiddlewareFunc) *routePath
	Post(prefix string, f echo.HandlerFunc, m ...echo.MiddlewareFunc) *routePath
	Put(prefix string, f echo.HandlerFunc, m ...echo.MiddlewareFunc) *routePath
	Patch(prefix string, f echo.HandlerFunc, m ...echo.MiddlewareFunc) *routePath
	Delete(prefix string, f echo.HandlerFunc, m ...echo.MiddlewareFunc) *routePath
	Any(prefix string, f echo.HandlerFunc, m ...echo.MiddlewareFunc) []*routePath
}

Route Path Implementation

type Start

type Start struct {
	Port string
}

Start HTTP

func (Start) Start

func (s Start) Start(e *echo.Echo) error

type StartAutoTLS

type StartAutoTLS struct {
	Port string
}

Start HTTP with auto creating TLS

func (StartAutoTLS) Start

func (s StartAutoTLS) Start(e *echo.Echo) error

type StartH2CServer

type StartH2CServer struct {
	Port  string
	HTTP2 http2.Server
}

Start HTTP 2

func (StartH2CServer) Start

func (s StartH2CServer) Start(e *echo.Echo) error

type StartInterface

type StartInterface interface {
	Start(e *echo.Echo) error
}

Start Implementation

type StartListener

type StartListener struct {
}

If echo has custom Listener, run this one

func (StartListener) Start

func (s StartListener) Start(e *echo.Echo) error

type StartServer

type StartServer struct {
	Server http.Server
}

Start HTTP with custom configuration HTTP Server

func (StartServer) Start

func (s StartServer) Start(e *echo.Echo) error

type StartTLS

type StartTLS struct {
	Port              string
	CertFile, KeyFile interface{}
}

Start HTTP with TLS

func (StartTLS) Start

func (s StartTLS) Start(e *echo.Echo) error

Jump to

Keyboard shortcuts

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