symple

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2025 License: MIT Imports: 18 Imported by: 0

README

Symple v0.1.0

Motivation

TODO

Install

go get -u github.com/ClMarlier/symple

Features

Usage

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

        "github.com/ClMarlier/symple"
)

func main() {
        // ErrFuncDefault is used to handle client response in case of an error
        // occuring anywhere in the chain of middleware or the handler itself.
        // You can provide your own implementation
        rs := symple.NewRouter(symple.ErrFuncDefault)

        mux, err := rs.Router(
                rs.WithZeroLog(os.Stdout),
                rs.WithRecoverer(true),
                rs.WithRoute("GET /hello", hello),
                rs.WithRouter(
                        rs.WithPrefix("/error"),
                        rs.WithRoute("GET /simple", simpleError),
                        rs.WithRoute("GET /panic/{n}", panicError),
                ),
        )
        if err != nil {
                log.Fatal(err)
        }
        log.Fatal(http.ListenAndServe(":8000", mux))
}

func hello(w http.ResponseWriter, r *http.Request) error {
        _, err := w.Write([]byte("Hello World"))
        return err
}

func simpleError(w http.ResponseWriter, _ *http.Request) error {
        return fmt.Errorf("%w custom description of the error", symple.ErrUnauthorized)
}

// to get a panic simply call with n=0
func panicError(w http.ResponseWriter, r *http.Request) error {
        pathNumber := r.PathValue("n")
        n, err := strconv.ParseInt(pathNumber, 10, 32)
        if err != nil {
                symple.ErrorResponse(w, err, http.StatusUnprocessableEntity)
        }
        res := 666 / n
        _, err = w.Write([]byte(fmt.Sprintf("666/%d = %d", n, res)))
        return err
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnauthorized        = errors.New("unauthorized")
	ErrNotFound            = errors.New("ressource not found")
	ErrUnsuportedMedia     = errors.New("unsupported media type")
	ErrUnprocessableEntity = errors.New("unprocessable entiy")
	ErrInternalServer      = errors.New("internal server error")
)

Functions

func CacheFileSystem

func CacheFileSystem(path string) (http.FileSystem, error)

func ErrFuncDefault

func ErrFuncDefault(w http.ResponseWriter, r *http.Request, err error)

func ErrorResponse

func ErrorResponse(w http.ResponseWriter, error error, statusCode int)

Error is used to build the http error response with the provided http status code and make theese values available to the middlewares.

func NewRouter

func NewRouter(handler ErrorHandlerFunc) *routerState

Types

type ContentType

type ContentType string
const (
	ContentTypeTextPlain   ContentType = "text/plain"
	ContentTypeTextHtml    ContentType = "text/html"
	ContentTypeJson        ContentType = "application/json"
	ContentTypeXml         ContentType = "application/xml"
	ContentTypeFormEncoded ContentType = "application/x-www-form-urlencoded"
	ContentTypeFormData    ContentType = "multipart/form-data"
)

type ErrorHandlerFunc

type ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error)

type HandlerFunc

type HandlerFunc func(w http.ResponseWriter, r *http.Request) error

type Middleware

type Middleware func(HandlerFunc) HandlerFunc

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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