DEV Community

Cover image for Teeny.go server with CLI mode
Guilherme Nascimento
Guilherme Nascimento

Posted on

Teeny.go server with CLI mode

#go

As I said in https://dev.to/brcontainer/teeny-go-a-route-system-for-go-golang-2o10, Teeny.go (or teenygo or only teeny) is a micro-module for create a micro-webserver with support for routes and routes with patterns similar to PHP and JavaScript web-frameworks

For install use

go get -u github.com/inphinit/teeny.go
Enter fullscreen mode Exit fullscreen mode

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

package main

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

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()
}
Enter fullscreen mode Exit fullscreen mode

For build:

go build program.go
Enter fullscreen mode Exit fullscreen mode

Usage example:

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

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

Top comments (0)