webimizer

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2021 License: GPL-3.0 Imports: 6 Imported by: 7

README

webimizer

Lightweight HTTP framework module written in Go

Code example

package main

import (
	"fmt"
	"log"
	"net/http"

	app "webimizer.dev/webimizer"
)

func httpNotAllowFunc(rw http.ResponseWriter, r *http.Request) {
	rw.WriteHeader(http.StatusBadRequest)
	fmt.Fprint(rw, "Bad Request")
}

func main() {
	app.DefaultHTTPHeaders = [][]string{
		{"x-content-type-options", "nosniff"},
		{"x-frame-options", "SAMEORIGIN"},
		{"x-xss-protection", "1; mode=block"},
	} // define default headers
	http.Handle("/", app.HttpHandlerStruct{
		Handler: app.HttpHandler(func(rw http.ResponseWriter, r *http.Request) {
			app.Get(rw, r, func(rw http.ResponseWriter, r *http.Request) {
				fmt.Fprint(rw, "Hello from webimizer. HTTP GET method was used.")
			})
			app.Post(rw, r, func(rw http.ResponseWriter, r *http.Request) {
				fmt.Fprint(rw, "Hello from webimizer. HTTP POST method was used.")
			})
		}), // app.HttpHandler call only if method is allowed
		NotAllowHandler: app.HttpNotAllowHandler(httpNotAllowFunc), // app.HtttpNotAllowHandler call if method is not allowed
		AllowedMethods:  []string{"GET","POST"},                           // define allowed methods
	}.Build())
	log.Fatal(http.ListenAndServe(":8080", nil)) // example server listen on port 8080
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultHTTPHeaders [][]string

Define default Http Response headers Example:

    [][]string{
		{"x-content-type-options", "nosniff"},
		{"x-frame-options", "SAMEORIGIN"},
		{"x-xss-protection", "1; mode=block"},
	} // define default headers

Functions

func Connect

func Connect(rw http.ResponseWriter, r *http.Request, handler IfHttpMethodHandler)

Helper func to check r.Method and call handler only if r.Method is CONNECT

func Delete

func Delete(rw http.ResponseWriter, r *http.Request, handler IfHttpMethodHandler)

Helper func to check r.Method and call handler only if r.Method is DELETE

func Get

func Get(rw http.ResponseWriter, r *http.Request, handler IfHttpMethodHandler)

Helper func to check r.Method and call handler only if r.Method is GET

func Head(rw http.ResponseWriter, r *http.Request, handler IfHttpMethodHandler)

Helper func to check r.Method and call handler only if r.Method is HEAD

func IfHttpMethod

func IfHttpMethod(needMethod string, rw http.ResponseWriter, r *http.Request, handler IfHttpMethodHandler)

Helper func to check r.Method and call handler only if needMethod is equal r.Method

func Options

func Options(rw http.ResponseWriter, r *http.Request, handler IfHttpMethodHandler)

Helper func to check r.Method and call handler only if r.Method is OPTIONS

func Patch

func Patch(rw http.ResponseWriter, r *http.Request, handler IfHttpMethodHandler)

Helper func to check r.Method and call handler only if r.Method is PATCH

func Post

func Post(rw http.ResponseWriter, r *http.Request, handler IfHttpMethodHandler)

Helper func to check r.Method and call handler only if r.Method is POST

func Put

func Put(rw http.ResponseWriter, r *http.Request, handler IfHttpMethodHandler)

Helper func to check r.Method and call handler only if r.Method is PUT

func Trace

func Trace(rw http.ResponseWriter, r *http.Request, handler IfHttpMethodHandler)

Helper func to check r.Method and call handler only if r.Method is TRACE

Types

type HttpHandler

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

It is main HttpHandler, which is called only, when Http method is allowed

func NewFileServerHandler added in v1.2.0

func NewFileServerHandler(fsPath string) HttpHandler

Create http Handler for serving files in fsPath directory. If file not found return 404 status and serve error404.html if exist

func (HttpHandler) ServeHTTP

func (fn HttpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

Compressing Http response by using gzipResponseWriter (only if Accept-Encoding request header is set and contains gzip value) and also add DefaultHttpHeaders to Http response

type HttpHandlerStruct

type HttpHandlerStruct struct {
	NotAllowHandler HttpNotAllowHandler
	Handler         HttpHandler
	AllowedMethods  []string
	AllowedOrigins  []string
}

The main struct, where You can define Handler (it is main HttpHandler, which is called only, when Http method is allowed), NotAllowHandler (it is HttpHandler, which is called only if Http method is not allowed) and AllowedMethods ([]string array, which contains allowed HTTP method names) You must call func Build to build HttpHandler.

In version v1.1 added AllowedOrigins field (optional): use if you want to check Origin header

func (HttpHandlerStruct) Build

func (builder HttpHandlerStruct) Build() HttpHandler

Build HttpHandler, which can by used in http.Handle (but not in http.HandleFunc, because only http.Handle call ServeHTTP)

type HttpNotAllowHandler

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

It is HttpHandler, which is called only if Http method is not allowed

type IfHttpMethodHandler

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

Http handler for use in IfHttpMethod func

Jump to

Keyboard shortcuts

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