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(method string, uri string, handler func(*Response, *Request, *Params, func(error)))
Command Registers a handler for a given URL
func Delete(uri string, handler func(*Response, *Request, *Params, func(error)))
Delete Registers a handler for a DELETE method URL
func ExposeDebugAPI()
ExposeDebugAPI exposes debug APIs
--------------------------------------------------
Get the list of available server addresses.
The addresses are public addresses.
GET /debug/server/list/type/:serverType
serverType - TCP, UDP or custom server type.
--------------------------------------------------
Get the endpoint of the server by its public address.
The API allows you to choose which server to create a new user on.
GET /debug/endpoint/type/:serverType/user/:uid/address/:addr POST /debug/endpoint/type/:serverType/user/:uid/address/:addr
serverType - TCP, UDP or custom server type.
uid - User ID
addr - Server address and port: format 0.0.0.0:8888
▶︎ Request Body format:
Request body will follow name=value format and separated by an & if there will be more than one name and value set.
[IMPORTANT] Currently user data does NOT automatically URL decode request body data if URL encoded.
▶︎ Example With Form:
foo=foo&bar=1234567&flag=true
The above will be translated to the following as user property data. Each value is stored as a string.
foo = "foo" bar = "123456" flag = "true"
▶︎ Example With JSON (Content-Type:application/json header must be provided):
If Content-Type:application/json header is provided, The API will read the request body as JSON.
{ "foo": "foo", "bar": 123456, "flag": true }
The above JSON request body will be translated to the following as user property data:
All values will be interface{} and they must be type asserted before using them.
foo = "foo" bar = 123456 flag = true
--------------------------------------------------
func ForceEndPointAPIV1Response()
ForceEndPointAPIV1Response forces the response format of /endpoint/type/:type/user/:user to be the same as when the request contains "ResponseVersion:v1" header.
This is only for the purpose of backward compatibility support.
func Get(uri string, handler func(*Response, *Request, *Params, func(error)))
Get Registers a handler for a GET method URL
func GetCCUByNodeAddress(addr string) int
GetCCUByNodeAddress returns CCU of a remote node by its mesh address
func GetRealTimeNodeEndpoint(list []string) string
GetRealTimeNodeEndpoint returns a WebSocket/TC/UDP server endpoint Returns a node endpoint that is not taken and online
func GetRoomNumbersByNodeAddress(addr string) int
GetRoomNumbersByNodeAddress returns the number of rooms on a remote node by its mesh address
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()
Offline Sets mesh network status to offline
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 OnEndPointAPIResponse(cb func(string, string, string, string) (string, interface{}))
OnEndPointAPIResponse assigns a callback on /endpoint/type/:type/user/:user API to add extra data to the response JSON.
func Online()
Online Sets mesh network status to online
func Options(uri string, handler func(*Response, *Request, *Params, func(error)))
Options Registers a handler for an OPTIONS method URL
func Post(uri string, handler func(*Response, *Request, *Params, func(error)))
Post Registers a handler for a POST method URL
func Put(uri string, handler func(*Response, *Request, *Params, func(error)))
Put Registers a handler for a PUT method URL
func SetAllowOrigin(origin string)
SetAllowOrigin sets Access-Control-Allow-Origin response header for CORS
func SetCustomNodeSelector(selector func([]string) string)
SetCustomNodeSelector replaces the default node selector with custom selector for Auth
func Setup(confpath string)
Setup Loads configuration file into memory - pass an empty string to load nothing
func SetupAsAuthServer(path string)
SetupAsAuthServer Sets Up HTTP server as Auth server for TCP/UDP - pass an empty string to load nothing
func ShutdownHTTP(ctx context.Context, done func(error))
ShutdownHTTP Stops HTTP server
func Start(next func(error))
Start Starts HTTP server
Params URL parameter data structure
type Params struct { Values map[string]string }
func (params *Params) GetAsBool(name string) (bool, error)
GetAsBool Returns a URL parameter value as a boolean
func (params *Params) GetAsFloat64(name string) (float64, error)
GetAsFloat64 Returns a URL parameters value as a float64
func (params *Params) GetAsInt(name string) (int, error)
GetAsInt Returns a URL parameter value as an int
func (params *Params) GetAsInt16(name string) (int16, error)
GetAsInt16 Returns a URL parameter value as an int16
func (params *Params) GetAsInt32(name string) (int32, error)
GetAsInt32 Returns a URL parameter value as an int32
func (params *Params) GetAsInt64(name string) (int64, error)
GetAsInt64 Returns a URL parameter value as an int64
func (params *Params) GetAsInt8(name string) (int8, error)
GetAsInt8 Returns a URL parameter value as an int8
func (params *Params) GetAsString(name string) (string, error)
GetAsString a URL parameter value as a string
func (params *Params) GetAsUint16(name string) (uint16, error)
GetAsUint16 Returns a URL parameter value as a uint16
func (params *Params) GetAsUint32(name string) (uint32, error)
GetAsUint32 Returns a URL parameter value as a uint32
func (params *Params) GetAsUint64(name string) (uint64, error)
GetAsUint64 Returns a URL parameter value as a uint64
func (params *Params) GetAsUint8(name string) (uint8, error)
GetAsUint8 Returns a URL parameter value as a uint8
Request Request data structure
type Request struct { // Method represents request method such as GET, POST, etc. Method string // URL represents the request URL. URL string // Queries contains all query strings of the request. Queries map[string][]string // Req represents the request struct instance provided by net/http. Req *http.Request // JSONBody contains unmarshaled JSON encoded data as a map. // This will be nil if Content-Type:application/json header is not provided. JSONBody map[string]interface{} }
func (req *Request) GetPostData(name string) string
GetPostData Returns postMethod body data by name
Response Response data structure
type Response struct { Method string URI string Writer http.ResponseWriter Finished bool sync.RWMutex }
func (res *Response) Respond(data string, status int)
Respond Sends a response packet and HTTP status code
func (res *Response) SendBytes(data []byte, status int)
SendBytes sends a response as binary data
func (res *Response) SetHeader(name, value string)
SetHeader sets a custom response header
ResponseData represents an HTTP response JSON data.
type ResponseData struct { sync.RWMutex // contains filtered or unexported fields }
func NewResponseData() *ResponseData
NewResponseData creates a new HTTP ResponseData.
func (rd *ResponseData) Add(key string, value interface{}) bool
Add adds a key and value to be encoded into JSON. The value data type supported is only primitive data types.
func (rd *ResponseData) JSON() string
JSON encodes added keys and values into JSON.
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 }