teeny

package module
v0.0.0-...-532a02a Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2021 License: MIT Imports: 9 Imported by: 0

README

About Teeny.go

The main objective of this project is to be light, simple, easy to learn, to serve other projects that need a route system to use together with other libraries and mainly to explore the native resources from language and engine (Go).

Configure your project

For install use:

go get -u github.com/inphinit/teeny.go

Create a file any name with extesion .go, example: test.go

For use local server use like this:

package main

import (
    "fmt"
    "net/http"
    "github.com/inphinit/teeny.go"
)

func main() {
    app := teeny.Serve("localhost", 7000)

    app.SetPublic("/home/user/Documents/")

    app.Action("GET", "/", func (response http.ResponseWriter, request *http.Request) {
        fmt.Fprint(response, "Homepage")
    })

    app.Action("GET", "/about", func (response http.ResponseWriter, request *http.Request) {
        fmt.Fprint(response, "About page")
    })

    app.Exec()
}

For a simples test execution (test.go is a example):

go run test.go

For build execute:

go build

Using TLS:

For use TLS in local server with certificate files

package main

import (
    "fmt"
    "net/http"
    "github.com/inphinit/teeny.go"
)

func main() {
    app := teeny.Serve("localhost", 7000)

    app.SetTLS(true)

    app.SetCertificate("/home/foo/cert.pem")

    app.SetKey("/home/foo/key.pem")

    ...

    app.Exec()
}

Using Fast-CGI:

For use teeny with Apache or Ngnix, enable Fast-CGI:

package main

import (
    "fmt"
    "net/http"
    "github.com/inphinit/teeny.go"
)

func main() {
    app := teeny.Serve("localhost", 7000)

    app.SetFcgi(true)

    ...

    app.Exec()
}

Handling errors

func main() {
    app := teeny.Serve("localhost", 7000)

    ...
    
    var codes = []int {403, 404, 405, 500}

    app.HandlerCodes(codes, func (response http.ResponseWriter, request *http.Request, code int) {
        fmt.Fprintf(response, "Error %d", code)
    })

    app.Exec()
}

Different handlers:

func main() {
    app := teeny.Serve("localhost", 7000)

    ...

    app.HandlerCodes([]int {404, 405}, func (response http.ResponseWriter, request *http.Request, code int) {
        fmt.Fprintf(response, "Router error: %d", code)
    })

    app.HandlerCodes([]int {403, 500}, func (response http.ResponseWriter, request *http.Request, code int) {
        fmt.Fprintf(response, "Error from ISAPI: %d", code)
    })

    app.Exec()
}

Static files

Set absolute path

func main() {
    app := teeny.Serve("localhost", 7000)

    app.SetPublic("/home/foo/bar")

    ...

Methods for config teeny

Method Description
app := teeny.Serve(host string, port int) configure routes host and port
app.SetDebug(enable bool) Define if debug is on (true) or off (false), by default is false
app.SetFcgi(enable bool) Enable Fast-CGI
app.SetTLS(enable bool) Enable TLS for server (not Fast-CGI)
app.SetCertificate(certFile string) Set certificate, use with app.SetTLS(true)
app.SetKey(keyFile string) Set certificate, use with app.SetTLS(true)
app.SetPublic(path string) Define absolute path for use static files
app.Action(method string, path string, func TeenyCallback) Define a route (from HTTP path in URL) for execute a function, arrow function or anonymous function
app.Params(method string, path string, func TeenyPatternCallback),
app.HandlerCodes(codes []int, func TeenyStatusCallback) Catch http errors (like ErrorDocument or error_page) from ISPAI or if try access a route not defined (emits 404 Not Found) or if try access a defined route with not defined http method (emits 405 Method Not Allowed)
app.SetPattern(name string, regex string) Create a pattern for use in route params
app.Exec() Starts server
app.CliMode() Starts server with support for configure with commands

Patterns supported by param routes (app.Params())

You can create your own patterns to use with the routes in "teeny", but there are also ready-to-use patterns:

Pattern Regex used Description
alnum [\da-zA-Z]+ Matches routes with param using alpha-numeric in route
alpha [a-zA-Z]+ Matches routes with param using A to Z letters in route
decimal \d+\.\d+ Matches routes with param using decimal format (like 1.2, 3.5, 100.50) in route
num \d+ Matches routes with param using numeric format in route
noslash [^\/]+ Matches routes with param using any character except slashs (\/ or /) in route
nospace \S+ Matches routes with param using any character except spaces, tabs or NUL in route
uuid [0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12} Matches routes with param using uuid format in route
version \d+\.\d+(\.\d+(-[\da-zA-Z]+(\.[\da-zA-Z]+)*(\+[\da-zA-Z]+(\.[\da-zA-Z]+)*)?)?)? Matches routes with param using semver.org format in route

CLI mode

For create a application with support for command line arguments you can create manualy using os.Args or using app.CliMode(), example:

func main() {
    //Default host and port
    app := teeny.Serve("localhost", 7000)

    app.Action("GET", "/", func (response http.ResponseWriter, request *http.Request) {
        fmt.Fprint(response, "Homepage")
    })

    app.Action("GET", "/about", func (response http.ResponseWriter, request *http.Request) {
        fmt.Fprint(response, "About page")
    })

    app.HandlerCodes([]int {403, 404, 405}, func (response http.ResponseWriter, request *http.Request, code int) {
        fmt.Fprintf(response, "Error: %d", code)
    })

    app.CliMode()
}

Usage example:

program.exe --debug --host 0.0.0.0 --port 8080 --public "/home/foo/bar/assets"

CLI mode arguments

Argument Example Description
--tls program --tls Enable TLS mode in your program (use with --cert and --key if it is not pre-configured)
--tls program --no-tls Disable TLS (if in the initial configuration of the script it was enabled)
--debug program --debug Enable debug mode in your program
--debug program --no-debug Disable debug (if in the initial configuration of the script it was enabled)
--fcgi program --fcgi Enable Fast-CGI mode in your program
--fcgi program --no-fcgi Disable Fast-CGI (if in the initial configuration of the script it was enabled)
--cert program --cert /home/foo/cert.pem Define certificate file (use with --tls if it is not pre-configured)
--key program --key /home/foo/key.pem Define key file (use with --tls if it is not pre-configured)
--public program --public /home/foo/assets Define folder for access static files
--host program --host 0.0.0.0 Define host address
--port program --port 9000 Define port addres

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TeenyCallback

type TeenyCallback func(http.ResponseWriter, *http.Request)

type TeenyPatternCallback

type TeenyPatternCallback func(http.ResponseWriter, *http.Request, map[string]string)

type TeenyServe

type TeenyServe struct {
	// contains filtered or unexported fields
}

func Serve

func Serve(host string, port int) TeenyServe

func (*TeenyServe) Action

func (e *TeenyServe) Action(method string, path string, callback TeenyCallback)

func (*TeenyServe) CliMode

func (e *TeenyServe) CliMode()

func (*TeenyServe) Exec

func (e *TeenyServe) Exec()

func (*TeenyServe) HandlerCodes

func (e *TeenyServe) HandlerCodes(codes []int, callback TeenyStatusCallback)

func (*TeenyServe) Params

func (e *TeenyServe) Params(method string, path string, callback TeenyPatternCallback)

func (*TeenyServe) SetCertificate

func (e *TeenyServe) SetCertificate(certFile string)

func (*TeenyServe) SetDebug

func (e *TeenyServe) SetDebug(enable bool)

func (*TeenyServe) SetFcgi

func (e *TeenyServe) SetFcgi(enable bool)

func (*TeenyServe) SetHost

func (e *TeenyServe) SetHost(host string)

func (*TeenyServe) SetKey

func (e *TeenyServe) SetKey(keyFile string)

func (*TeenyServe) SetPattern

func (e *TeenyServe) SetPattern(pattern string, regex string)

func (*TeenyServe) SetPort

func (e *TeenyServe) SetPort(port int)

func (*TeenyServe) SetPublic

func (e *TeenyServe) SetPublic(path string)

func (*TeenyServe) SetTLS

func (e *TeenyServe) SetTLS(enable bool)

type TeenyStatusCallback

type TeenyStatusCallback func(http.ResponseWriter, *http.Request, int)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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