wt

package module
v0.0.0-...-9052308 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2022 License: MIT Imports: 29 Imported by: 0

README

WebTools (wt)

Collection of useful set of WebTools (wt) for building Go HTTP server applications. For example:

package main

import (
	"flag"
	"log"
	"net/http"

	"github.com/gorilla/mux"
	wt "github.com/vkuznet/wt"
)

func main() {
	var verbose int
	flag.IntVar(&verbose, "verbose", 0, "verbosity level")
	flag.Parse()
    // create new server configuration
	config := wt.NewServerConfig()
    // set appropriate flags, see more in wt.ServerConfiguration
	config.Templates = "static/tmpl" // location of static template area
    config.Base = "/"                // our server base path
	config.Verbose = 1               // verbosity level

    // start the server
	wt.StartServer(config, srvRouter(config))
}

// AuthFunc provides authentication for incoming HTTP request
func AuthFunc(h http.Header) error {
	log.Println("authFunc")
	return nil
}

// AuthFunc provides validation for incoming HTTP request
func ValidateFunc(h http.Header) error {
	log.Println("validateFunc")
	return nil
}

// create our HTTP router
func srvRouter(serverConfig wt.ServerConfiguration) *mux.Router {
	base := serverConfig.Base
	router := mux.NewRouter()
	router.StrictSlash(true) // to allow /route and /route/ end-points
	router.HandleFunc(wt.BasePath(base, "/"), wt.HomeHandler).Methods("GET")

	// this is for displaying the QR code on /qr end point
	// and static area which holds user's images
	log.Println("server static area", serverConfig.StaticDir)
	fileServer := http.StripPrefix("/static/", http.FileServer(http.Dir(serverConfig.StaticDir)))
	router.PathPrefix(wt.BasePath(base, "/css/{file:[0-9a-zA-Z-\\.]+}")).Handler(fileServer)

    // define different middleware layers
	router.Use(wt.LoggingMiddleware)
	router.Use(wt.AuthMiddleware(AuthFunc))
	router.Use(wt.ValidateMiddleware(ValidateFunc))
	router.Use(wt.LimitMiddleware)
	router.Use(wt.CorsMiddleware)

	return router
}

The wt provides different middleware layers, and flexible configuration to setup different servers, HTTP, HTTPs, HTTPs with let's encrypt, etc.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BottomHTML string

BottomHTML represents bottom html content

View Source
var PrintLogRecord bool

PrintLogRecord controls if we'll print to stdout log record in JSON data format. This is useful in k8s setup when we can redirect logs to dedidcated file and at the same time print log record to stdout for scraping by monitoring/k8s systems

View Source
var TmplDir string

TmplDir defines location of templates

View Source
var TopHTML string

TopHTML represents top html content

View Source
var UTC bool

UTC flag represents UTC time zone for log messages

Functions

func AuthMiddleware

func AuthMiddleware(authFunc MiddlewareFunction) mux.MiddlewareFunc

AuthMiddleware provide auth/authz action for incoming HTTP requests. User should initialize it with MiddlewareFunction which accepts http Header and return the error

func BasePath

func BasePath(base, api string) string

BasePath provides proper base path for a given api

func CorsMiddleware

func CorsMiddleware(next http.Handler) http.Handler

Corsmiddleware provides CORS

func FaviconHandler

func FaviconHandler(w http.ResponseWriter, r *http.Request)

FaviconHandler provides favicon icon file

func GetServer

func GetServer(serverConfig ServerConfiguration) *http.Server

GetServer returns http.Server object for different configurations

func HomeHandler

func HomeHandler(w http.ResponseWriter, r *http.Request)

HomeHandler handles home page requests

func InitLimiter

func InitLimiter(period string)

InitLimiter initializes Limiter middleware pointer

func LetsEncryptServer

func LetsEncryptServer(hosts ...string) *http.Server

LetsEncryptServer provides HTTPs server with Let's encrypt for given domain names (hosts)

func LimitMiddleware

func LimitMiddleware(next http.Handler) http.Handler

LimitMiddleware limits incoming requests

func LogRequest

func LogRequest(w http.ResponseWriter, r *http.Request, start time.Time, cauth string, status *int, tstamp int64, bytesOut int64)

helper function to log every single user request, here we pass pointer to status code as it may change through the handler while we use defer logRequest

func LoggingMiddleware

func LoggingMiddleware(h http.Handler) http.Handler

LoggingMiddleware provides logging middleware for HTTP requests https://arunvelsriram.dev/simple-golang-http-logging-middleware

func ResponseMsg

func ResponseMsg(w http.ResponseWriter, r *http.Request, msg, api string, code int)

ResponseMsg helper function to provide response to end-user ResponseMsg(w, r, fmt.Sprintf("%v", err), "VaultDeleteHandler", http.StatusBadRequest)

func SignUpHandler

func SignUpHandler(w http.ResponseWriter, r *http.Request)

SignUpHandler handles sign-up page requests

func StartServer

func StartServer(serverConfig ServerConfiguration, handler http.Handler)

StartServer starts web server with given http handler

func TlsServer

func TlsServer(serverCrt, serverKey, rootCAs string, port, verbose int) *http.Server

TlsServer returns TLS enabled HTTP server

func ValidateMiddleware

func ValidateMiddleware(validateFunc MiddlewareFunction) mux.MiddlewareFunc

ValidateMiddleware provides validation action for incoming HTTP requests. User should initialize it with MiddlewareFunction which accepts http Header and return the error

Types

type HTTPRecord

type HTTPRecord map[string]string

HTTPRecord represents HTTP response record

type LogRecord

type LogRecord struct {
	Method         string  `json:"method"`           // http.Request HTTP method
	Host           string  `json:"hostname"`         // hostname of the server
	URI            string  `json:"uri"`              // http.RequestURI
	API            string  `json:"api"`              // http service API being used
	System         string  `json:"system"`           // cmsweb service name
	ClientIP       string  `json:"clientip"`         // client IP address
	BytesSend      int64   `json:"bytes_send"`       // number of bytes send with HTTP request
	BytesReceived  int64   `json:"bytes_received"`   // number of bytes received with HTTP request
	Proto          string  `json:"proto"`            // http.Request protocol
	Status         int64   `json:"status"`           // http.Request status code
	ContentLength  int64   `json:"content_length"`   // http.Request content-length
	AuthProto      string  `json:"auth_proto"`       // authentication protocol
	AuthCert       string  `json:"auth_cert"`        // auth certificate, user DN
	LoginName      string  `json:"login_name"`       // login name, user DN
	Auth           string  `json:"auth"`             // auth method
	Cipher         string  `json:"cipher"`           // TLS cipher name
	Referer        string  `json:"referer"`          // http referer
	UserAgent      string  `json:"user_agent"`       // http user-agent field
	XForwardedHost string  `json:"x_forwarded_host"` // http.Request X-Forwarded-Host
	XForwardedFor  string  `json:"x_forwarded_for"`  // http.Request X-Forwarded-For
	RemoteAddr     string  `json:"remote_addr"`      // http.Request remote address
	ResponseStatus string  `json:"response_status"`  // http.Response status
	ResponseTime   float64 `json:"response_time"`    // http response time
	RequestTime    float64 `json:"request_time"`     // http request time
	Timestamp      int64   `json:"timestamp"`        // record timestamp
	RecTimestamp   int64   `json:"rec_timestamp"`    // timestamp for backward compatibility with apache
	RecDate        string  `json:"rec_date"`         // timestamp for backward compatibility with apache
}

LogRecord represents HTTP log record

type LogWriter

type LogWriter struct {
}

LogWriter represents our logger

func (LogWriter) Write

func (writer LogWriter) Write(data []byte) (int, error)

Write method of our log writer

type MiddlewareFunction

type MiddlewareFunction func(h http.Header) error

MiddlewareFunction defines common function to be used by mux middleware For examaple, we can provide Authentication or Validator functions to corresponding middleware

type RotateLogWriter

type RotateLogWriter struct {
	RotateLogs *rotatelogs.RotateLogs
}

RotateLogWriter represents rorate log writer

func (RotateLogWriter) Write

func (w RotateLogWriter) Write(data []byte) (int, error)

Write method of our rotate log writer

type ServerConfiguration

type ServerConfiguration struct {
	Port          int      `json:"port"`         // server port number
	Base          string   `json:"base"`         // base URL
	Verbose       int      `json:"verbose"`      // verbose output
	ServerCrt     string   `json:"serverCrt"`    // path to server crt file
	ServerKey     string   `json:"serverKey"`    // path to server key file
	RootCA        string   `json:"rootCA"`       // RootCA file
	CSRFKey       string   `json:"csrfKey"`      // CSRF 32-byte-long-auth-key
	Production    bool     `json:"production"`   // production server or not
	LimiterPeriod string   `json:"rate"`         // limiter rate value
	LogFile       string   `json:"log_file"`     // server log file
	LetsEncrypt   bool     `json:"lets_encrypt"` // start LetsEncrypt HTTPs server
	DomainNames   []string `json:"domain_names"` // list of domain names to use
	StaticDir     string   `json:"static"`       // location of static files
	Templates     string   `json:"templates"`    // location of server templates
}

ServerConfiguration stores server configuration parameters

func NewServerConfig

func NewServerConfig() ServerConfiguration

NewServerConfig creates new ServerConfigruation with some default parameters

func ParseServerConfig

func ParseServerConfig(configFile string) (ServerConfiguration, error)

ParseServerConfig parses server configuration

type ServerRouter

type ServerRouter *http.Handler

ServerRouter represents pointer to http Handler

type Templates

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

Templates structure holds our HTML template

func (Templates) Tmpl

func (t Templates) Tmpl(tdir, tfile string, tmplData map[string]interface{}) string

Tmpl method for Templates structure

type TmplRecord

type TmplRecord map[string]interface{}

TmplRecord represent template record

Jump to

Keyboard shortcuts

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