TWTServer

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2021 License: Unlicense Imports: 11 Imported by: 0

Documentation

Overview

A simple router to help my plugin application gateway experiment

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetQueryParams

func GetQueryParams(r *http.Request) map[string]interface{}

build args["query"] for given subroute

ex: route /cake
request: /cake?flavor=chocolate
args["query"] = { "flavor" : "chocolate" }

request: /cake?flavor=chocolate&flavor=vanilla
args["query"] = { "flavor" : ["chocolate", "vanilla"] }

func GetRequestBody

func GetRequestBody(r *http.Request) map[string]interface{}

build args["body"] for given subroute

ex: route /cake
request: (post) /cake
post body: { "flavor" : "chocolate" }
args["body"] = { "flavor" : "chocolate" }
+ similar nesting for args["query"] based on payload, which is why both are map[string]interface

func PathFromRequest

func PathFromRequest(r *http.Request) []string

func PathFromString

func PathFromString(url string, del string) []string

Types

type BasicPlugin added in v0.2.0

type BasicPlugin struct {
	Name    string
	FileDir string
	Router  SubRouter
}

a basic / common implementation for the interface

func (*BasicPlugin) FilePath added in v0.3.0

func (p *BasicPlugin) FilePath(url string, fileRoute string) string

func (*BasicPlugin) HandleWeb added in v0.2.0

func (p *BasicPlugin) HandleWeb(w http.ResponseWriter, r *http.Request, path []string)

func (*BasicPlugin) ReceiveInfo added in v0.2.0

func (p *BasicPlugin) ReceiveInfo(name string, fileDir string)

type STPlugin

type STPlugin interface {
	/*
		void function that writes a response to the client. In most cases this can be handled by SubRouter.Handle.

		If your website is www.site.com, your plugin is "misc" and you register a route of "/hello",
		when you visit www.site.com/misc/hello your plugin "misc" will be passed a path of ["hello"].
	*/
	HandleWeb(http.ResponseWriter, *http.Request, []string)

	/*
		void function that will receive config information from the gateway, like what name the application is being served as
		or what directory the files exist in.
	*/
	ReceiveInfo(name string, fileDir string)
}

Interface for plugins to implement. Every plugin must export a variable that implements STPlugin. The default plugin name will be "TWTPlugin" but can be renamed with the config "plugin-variable".

type ServerThing

type ServerThing struct {
	// handler functions from sub routed applications
	WebRouter map[string]func(http.ResponseWriter, *http.Request, []string)
	ProgArgs  struct {
		Build          bool              `json:"build"`
		Port           int               `json:"port"`
		PluginVariable string            `json:"plugin-variable"`
		Apps           map[string]string `json:"apps"`
	}
}

Type to receive all of the functions here

func (*ServerThing) ArgsAndConfig

func (st *ServerThing) ArgsAndConfig()

build args from args & config file

func (*ServerThing) BuildModules

func (st *ServerThing) BuildModules() []string

go through apps key of config and attempt to build projects into so files. current design is that a project intended for this gateway will be

/path/to/source/

src/ all golang files and packages to be built
files/ files that the project may serve

func (*ServerThing) DeleteBuiltModules

func (st *ServerThing) DeleteBuiltModules()

clear ./modules folder, intended to be used before building

func (*ServerThing) DeleteLinkedFiles

func (st *ServerThing) DeleteLinkedFiles()

clear ./files folder

func (*ServerThing) DoPluginStuff

func (st *ServerThing) DoPluginStuff()

determine whether we're loading or building modules, then do it!

func (*ServerThing) GetExistingModules

func (st *ServerThing) GetExistingModules() []string

check ./modules folder for existing modules instead of building them

func (*ServerThing) Handle

func (st *ServerThing) Handle(w http.ResponseWriter, r *http.Request)

Handle - main server handler

func (*ServerThing) LoadModules

func (st *ServerThing) LoadModules(names []string)

load .so files in ./modules dir, optionally filtered by a config file or program arg

func (*ServerThing) Start

func (st *ServerThing) Start()

type SubRoute

type SubRoute struct {
	Path      []string    // ex: ["misc", "file", "thefile.png"]
	Method    string      // ex: "GET" or "POST" or "CUSTOM" or "*"
	Handler   interface{} // handler for this route
	GlobIndex int         // cached index of glob
	IsDefault bool        // any router or subroute registered as *
}

router routes :)

func (*SubRoute) GetRouteParams

func (route *SubRoute) GetRouteParams(path []string) map[string]string

build args["route"] for given subroute.

ex: route - /cake/:flavor
request: /cake/chocolate
args["route"] = { "flavor": "chocolate" }

func (*SubRoute) MatchPath

func (route *SubRoute) MatchPath(path []string, method string) bool

used in the main router's Handle function to decide which route to execute, if any

type SubRouter

type SubRouter struct {
	Routes []SubRoute
}

a collection of routes for the given application

func (*SubRouter) AddSubRoutes added in v0.5.0

func (router *SubRouter) AddSubRoutes(prefix string, routes []SubRoute)

prefix a collection of SubRoutes

func (*SubRouter) Handle

func (router *SubRouter) Handle(w http.ResponseWriter, r *http.Request, path []string)

default handler for application plugin's HandleWeb

func (*SubRouter) Register

func (router *SubRouter) Register(uri string, method string, handler interface{})

add route to router

Jump to

Keyboard shortcuts

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