shttp

package module
v0.0.0-...-53ffc05 Latest Latest
Warning

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

Go to latest
Published: May 13, 2019 License: MIT Imports: 5 Imported by: 0

README

Simple HTTP Server Library

Go Report Card

Usage

package main

import (
	"errors"
	"log"
	"net/http"

	"github.com/bearchit/shttp"
)

func main() {
	r := shttp.New()
	r.ErrorHandler = func(c *shttp.Context, err error) {
		c.String(http.StatusInternalServerError, err.Error())
	}

	r.Use(func(next shttp.HandlerFunc) shttp.HandlerFunc {
		return func(c *shttp.Context) error {
			log.Println(c.Request.RequestURI)
			return next(c)
		}
	})

	r.GET("/", func(c *shttp.Context) error {
		return c.String(http.StatusOK, "Hello")
	})

	r.GET("/error", func(c *shttp.Context) error {
		return errors.New("error from root")
	})

	sub := r.Sub("/sub")
	sub.ErrorHandler = func(c *shttp.Context, err error) {
		c.JSON(http.StatusInternalServerError, map[string]interface{}{
			"error": err.Error(),
		})
	}

	sub.GET("/", func(c *shttp.Context) error {
		return c.JSON(http.StatusOK, map[string]interface{}{
			"message": "Hello from sub",
		})
	})

	sub.GET("/error", func(c *shttp.Context) error {
		return errors.New("error from sub")
	})

	log.Fatal(http.ListenAndServe(":8080", r))
}

Documentation

Index

Constants

View Source
const (
	MimePlainText = "plain/text"
	MimeJson      = "application/json; charset=utf8"
	MimeHTML      = "text/html"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	Request    *http.Request
	Response   http.ResponseWriter
	PathParams httprouter.Params
	Values     map[string]interface{}
}

func (Context) Get

func (c Context) Get(key string) (interface{}, bool)

func (Context) HTML

func (c Context) HTML(status int, tpl template.Template, data interface{}) error

func (Context) JSON

func (c Context) JSON(status int, v interface{}) error

func (Context) MustGet

func (c Context) MustGet(key string) interface{}

func (Context) NoContent

func (c Context) NoContent(status int) error

func (*Context) Set

func (c *Context) Set(key string, v interface{})

func (Context) String

func (c Context) String(status int, v string) error

type ErrorHandler

type ErrorHandler func(c *Context, err error)

type HandlerFunc

type HandlerFunc func(c *Context) error

type Middleware

type Middleware func(HandlerFunc) HandlerFunc

type Router

type Router struct {
	ErrorHandler ErrorHandler
	// contains filtered or unexported fields
}

func New

func New() *Router

func (Router) DELETE

func (e Router) DELETE(pattern string, handler HandlerFunc)

func (Router) GET

func (e Router) GET(pattern string, handler HandlerFunc)

func (Router) OPTIONS

func (e Router) OPTIONS(pattern string, handler HandlerFunc)

func (Router) PATCH

func (e Router) PATCH(pattern string, handler HandlerFunc)

func (Router) POST

func (e Router) POST(pattern string, handler HandlerFunc)

func (Router) PUT

func (e Router) PUT(pattern string, handler HandlerFunc)

func (Router) ServeHTTP

func (e Router) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (Router) Static

func (e Router) Static(pattern string, root string)

func (Router) Sub

func (e Router) Sub(prefix string) *Router

func (*Router) Use

func (e *Router) Use(middleware ...Middleware)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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