...

Package http

import "github.com/Diarkis/diarkis/server/http"
Overview
Index

Overview ▾

Package http ▷

HTTP - Client entry point and router

Routes clients to UDP/RUDP, TCP, and/or, WebSocket servers.

Configurations

HTTP configurations are explained below.

{
  "address": "127.0.0.1",
  "port": "7000",
  "enableEncryption": true,
  "useFixedPort": false,
}

▶︎ Configuration Values

address - Default is "127.0.0.1"

Address for HTTP server to bind with.

port - Default is "7000"

Port for HTTP server to bind with. Starts with the given port and scans to find an open port to bind.

enableEncryption - Default is true

Default is true, but if you set this to false, TCP, UDP/RUDP must set this to false also.

IMPORTANT - If you are using Docker, you need to "use" external IP address of Docker.

useFixedPort - Default is false

If true, HTTP server will bind to a fixed address and port instead of searching available port.

Index ▾

Constants
func Command(method string, uri string, handler func(*Response, *Request, *Params, func(error)))
func Delete(uri string, handler func(*Response, *Request, *Params, func(error)))
func Get(uri string, handler func(*Response, *Request, *Params, func(error)))
func GetRealTimeNodeEndpoint(list []string) string
func HandleStaticFiles(uriRoot string, fsPath string)
func Head(uri string, handler func(*Response, *Request, *Params, func(error)))
func Offline()
func OnAuthRequest(handler func(res *Response, req *Request, params *Params, next func(error)))
func Online()
func Options(uri string, handler func(*Response, *Request, *Params, func(error)))
func Post(uri string, handler func(*Response, *Request, *Params, func(error)))
func Put(uri string, handler func(*Response, *Request, *Params, func(error)))
func SetAllowOrigin(origin string)
func SetCustomNodeSelector(selector func([]string) string)
func Setup(confpath string)
func SetupAsAuthServer(path string)
func ShutdownHTTP(next func(error))
func Start(next func(error))
type Params
    func (params *Params) GetAsBool(name string) (bool, error)
    func (params *Params) GetAsFloat64(name string) (float64, error)
    func (params *Params) GetAsInt(name string) (int, error)
    func (params *Params) GetAsInt16(name string) (int16, error)
    func (params *Params) GetAsInt32(name string) (int32, error)
    func (params *Params) GetAsInt64(name string) (int64, error)
    func (params *Params) GetAsInt8(name string) (int8, error)
    func (params *Params) GetAsString(name string) (string, error)
    func (params *Params) GetAsUint16(name string) (uint16, error)
    func (params *Params) GetAsUint32(name string) (uint32, error)
    func (params *Params) GetAsUint64(name string) (uint64, error)
    func (params *Params) GetAsUint8(name string) (uint8, error)
type Request
    func (req *Request) GetPostData(name string) string
type Response
    func (res *Response) Respond(data string, status int)
    func (res *Response) SendBytes(data []byte, status int)
    func (res *Response) SetHeader(name, value string)
type Route

Constants

Bad HTTP status code 400

const Bad = 400

Err HTTP status code 500

const Err = 500

NoService HTTP status code 503

const NoService = 503

NotFound HTTP status code 404

const NotFound = 404

Ok HTTP status code 200

const Ok = 200

Type Mesh network server type for HTTP server

const Type = "HTTP"

func Command

func Command(method string, uri string, handler func(*Response, *Request, *Params, func(error)))

Command Registers a handler for a given URL

func Delete

func Delete(uri string, handler func(*Response, *Request, *Params, func(error)))

Delete Registers a handler for a DELETE method URL

func Get

func Get(uri string, handler func(*Response, *Request, *Params, func(error)))

Get Registers a handler for a GET method URL

func GetRealTimeNodeEndpoint

func GetRealTimeNodeEndpoint(list []string) string

GetRealTimeNodeEndpoint returns a WebSocket/TC/UDP server endpoint Returns a node endpoint that is not taken and online

func HandleStaticFiles

func HandleStaticFiles(uriRoot string, fsPath string)

HandleStaticFiles handle static files

func Head(uri string, handler func(*Response, *Request, *Params, func(error)))

Head Registers a handler for a HEAD method URL

func Offline

func Offline()

Offline Sets mesh network status to offline

func OnAuthRequest

func OnAuthRequest(handler func(res *Response, req *Request, params *Params, next func(error)))

OnAuthRequest hooks a request handler function to auth request. If invoked before SetupAsAuthServer(), the hook will be called BEFORE the actual auth handling. If invoked after SetupAsAuthServer(), the hook will be called AFTER.

func Online

func Online()

Online Sets mesh network status to online

func Options

func Options(uri string, handler func(*Response, *Request, *Params, func(error)))

Options Registers a handler for an OPTIONS method URL

func Post

func Post(uri string, handler func(*Response, *Request, *Params, func(error)))

Post Registers a handler for a POST method URL

func Put

func Put(uri string, handler func(*Response, *Request, *Params, func(error)))

Put Registers a handler for a PUT method URL

func SetAllowOrigin

func SetAllowOrigin(origin string)

SetAllowOrigin sets Access-Control-Allow-Origin response header for CORS

func SetCustomNodeSelector

func SetCustomNodeSelector(selector func([]string) string)

SetCustomNodeSelector replaces the default node selector with custom selector for Auth

func Setup

func Setup(confpath string)

Setup Loads configuration file into memory - pass an empty string to load nothing

func SetupAsAuthServer

func SetupAsAuthServer(path string)

SetupAsAuthServer Sets Up HTTP server as Auth server for TCP/UDP - pass an empty string to load nothing

func ShutdownHTTP

func ShutdownHTTP(next func(error))

ShutdownHTTP Stops HTTP server

func Start

func Start(next func(error))

Start Starts HTTP server

type Params

Params URL parameter data structure

type Params struct {
    Values map[string]string
}

func (*Params) GetAsBool

func (params *Params) GetAsBool(name string) (bool, error)

GetAsBool Returns a URL parameter value as a boolean

func (*Params) GetAsFloat64

func (params *Params) GetAsFloat64(name string) (float64, error)

GetAsFloat64 Returns a URL parameters value as a float64

func (*Params) GetAsInt

func (params *Params) GetAsInt(name string) (int, error)

GetAsInt Returns a URL parameter value as an int

func (*Params) GetAsInt16

func (params *Params) GetAsInt16(name string) (int16, error)

GetAsInt16 Returns a URL parameter value as an int16

func (*Params) GetAsInt32

func (params *Params) GetAsInt32(name string) (int32, error)

GetAsInt32 Returns a URL parameter value as an int32

func (*Params) GetAsInt64

func (params *Params) GetAsInt64(name string) (int64, error)

GetAsInt64 Returns a URL parameter value as an int64

func (*Params) GetAsInt8

func (params *Params) GetAsInt8(name string) (int8, error)

GetAsInt8 Returns a URL parameter value as an int8

func (*Params) GetAsString

func (params *Params) GetAsString(name string) (string, error)

GetAsString a URL parameter value as a string

func (*Params) GetAsUint16

func (params *Params) GetAsUint16(name string) (uint16, error)

GetAsUint16 Returns a URL parameter value as a uint16

func (*Params) GetAsUint32

func (params *Params) GetAsUint32(name string) (uint32, error)

GetAsUint32 Returns a URL parameter value as a uint32

func (*Params) GetAsUint64

func (params *Params) GetAsUint64(name string) (uint64, error)

GetAsUint64 Returns a URL parameter value as a uint64

func (*Params) GetAsUint8

func (params *Params) GetAsUint8(name string) (uint8, error)

GetAsUint8 Returns a URL parameter value as a uint8

type Request

Request Request data structure

type Request struct {
    Method  string
    URL     string
    Queries map[string][]string
    Req     *http.Request
}

func (*Request) GetPostData

func (req *Request) GetPostData(name string) string

GetPostData Returns postMethod body data by name

type Response

Response Response data structure

type Response struct {
    Method   string
    URI      string
    Writer   http.ResponseWriter
    Finished bool
}

func (*Response) Respond

func (res *Response) Respond(data string, status int)

Respond Sends a response packet and HTTP status code

func (*Response) SendBytes

func (res *Response) SendBytes(data []byte, status int)

SendBytes sends a response as binary data

func (*Response) SetHeader

func (res *Response) SetHeader(name, value string)

SetHeader sets a custom response header

type Route

Route URL route data structure

type Route struct {
    Route      string
    Finder     *regexp.Regexp
    Extractor  *regexp.Regexp
    Handlers   []func(*Response, *Request, *Params, func(error))
    ParamNames []string
}