helper

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2019 License: Apache-2.0 Imports: 11 Imported by: 0

README

http-helper

A simple http helper to support router and argument processing

Sample #1: handler with http.HandlerFunc

package main

import (
    "github.com/rosbit/http-helper"
    "net/http"
    "fmt"
)

func main() {
    r := helper.NewHelper()

    r.Get("/hello", func(w http.ResponseWriter, r *http.Request) {
        c := helper.NewHttpContext(w, r)
        c.String(http.StatusOK, "hello")
    })

    r.Get("/json/:msg", func(w http.ResponseWriter, r *http.Request) {
        c := helper.NewHttpContext(w, r)
        msg := c.Param("msg")
        c.JSON(http.StatusOK, map[string]interface{} {
            "code": http.StatusOK,
            "msg": msg,
        })
    })

    r.Post("/json", func(w http.ResponseWriter, r *http.Request) {
        c := helper.NewHttpContext(w, r)
        var i interface{}
        code, err := c.ReadJSON(&i)
        if err != nil {
            c.Error(code, err.Error())
            return
        }
        c.JSONPretty(http.StatusOK, i, " ")
    })

    r.Get("/jump", func(w http.ResponseWriter, r *http.Request) {
        c := helper.NewHttpContext(w, r)
        url := c.QueryParam("u")
        if url == "" {
            c.Error(http.StatusBadRequest, "argument u expected")
            return
        }
        c.Redirect(http.StatusFound, url)
    })

    r.Post("/form/:name", func(w http.ResponseWriter, r *http.Request) {
        c := helper.NewHttpContext(w, r)
        n := c.Param("name")
        v := c.FormValue(n)
        c.String(http.StatusOK, fmt.Sprintf("value of %s: %s\n", n, v))
    })

    r.Run()
    // or r.Run(":8080")
    // or http.ListenAndServe(":8080", r)
}

Sample #2: handler with argument helper.Context

package main

import (
    "github.com/rosbit/http-helper"
    "net/http"
    "fmt"
)

func main() {
    r := helper.NewHelper()

    r.GET("/hello", func(c *helper.Context) {
        c.String(http.StatusOK, "hello")
    })

    r.GET("/json/:msg", func(c *helper.Context) {
        msg := c.Param("msg")
        c.JSON(http.StatusOK, map[string]interface{} {
            "code": http.StatusOK,
            "msg": msg,
        })
    })

    r.POST("/json", func(c *helper.Context) {
        var i interface{}
        code, err := c.ReadJSON(&i)
        if err != nil {
            c.Error(code, err.Error())
            return
        }
        c.JSONPretty(http.StatusOK, i, " ")
    })

    r.GET("/jump", func(c *helper.Context) {
        url := c.QueryParam("u")
        if url == "" {
            c.Error(http.StatusBadRequest, "argument u expected")
            return
        }
        c.Redirect(http.StatusFound, url)
    })

    r.POST("/form/:name", func(c *helper.Context) {
        n := c.Param("name")
        v := c.FormValue(n)
        c.String(http.StatusOK, fmt.Sprintf("value of %s: %s\n", n, v))
    })

    r.Run()
    // or r.Run(":8080")
    // or http.ListenAndServe(":8080", r)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NotFoundHandler

func NotFoundHandler(c *Context) (err error)

Types

type Context

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

func NewHttpContext

func NewHttpContext(w http.ResponseWriter, r *http.Request) *Context

func (*Context) AddHeader

func (c *Context) AddHeader(key, value string)

func (*Context) Attachment

func (c *Context) Attachment(file, name string) error

func (*Context) Blob

func (c *Context) Blob(code int, contentType string, b []byte) (err error)

func (*Context) Cookie

func (c *Context) Cookie(name string) (*http.Cookie, error)

func (*Context) Cookies

func (c *Context) Cookies() []*http.Cookie

func (*Context) Error

func (c *Context) Error(code int, msg string) (err error)

func (*Context) File

func (c *Context) File(file string) (err error)

func (*Context) FormFile

func (c *Context) FormFile(name string) (*multipart.FileHeader, error)

func (*Context) FormParams

func (c *Context) FormParams() (url.Values, error)

func (*Context) FormValue

func (c *Context) FormValue(name string) string

func (*Context) Header

func (c *Context) Header(name string) string

func (*Context) Inline

func (c *Context) Inline(file, name string) error

func (*Context) JSON

func (c *Context) JSON(code int, i interface{}) error

func (*Context) JSONBlob

func (c *Context) JSONBlob(code int, b []byte) (err error)

func (*Context) JSONPretty

func (c *Context) JSONPretty(code int, i interface{}, indent string) (err error)

func (*Context) MultipartForm

func (c *Context) MultipartForm() (*multipart.Form, error)

func (*Context) Param

func (c *Context) Param(name string) string

func (*Context) QueryParam

func (c *Context) QueryParam(name string) string

func (*Context) QueryParams

func (c *Context) QueryParams() url.Values

func (*Context) QueryString

func (c *Context) QueryString() string

func (*Context) ReadJSON

func (c *Context) ReadJSON(res interface{}) (code int, err error)

func (*Context) Redirect

func (c *Context) Redirect(code int, url string) error

func (*Context) Request

func (c *Context) Request() *http.Request

func (*Context) Response

func (c *Context) Response() http.ResponseWriter

func (*Context) SetCookie

func (c *Context) SetCookie(cookie *http.Cookie)

func (*Context) SetHeader

func (c *Context) SetHeader(key, value string)

func (*Context) Stream

func (c *Context) Stream(code int, contentType string, r io.Reader) (err error)

func (*Context) String

func (c *Context) String(code int, s string) error

type ContextHandlerFunc

type ContextHandlerFunc func(c *Context)

type HttpHelper

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

func NewHelper

func NewHelper() *HttpHelper

func (*HttpHelper) CONNECT

func (hr *HttpHelper) CONNECT(path string, h func(c *Context)) error

func (*HttpHelper) Connect

func (hr *HttpHelper) Connect(path string, h func(http.ResponseWriter, *http.Request)) error

func (*HttpHelper) DELETE

func (hr *HttpHelper) DELETE(path string, h func(c *Context)) error

func (*HttpHelper) Delete

func (hr *HttpHelper) Delete(path string, h func(http.ResponseWriter, *http.Request)) error

func (*HttpHelper) GET

func (hr *HttpHelper) GET(pattern string, h func(c *Context)) error

func (*HttpHelper) Get

func (hr *HttpHelper) Get(pattern string, h func(http.ResponseWriter, *http.Request)) error

func (*HttpHelper) HEAD

func (hr *HttpHelper) HEAD(path string, h func(c *Context)) error

func (*HttpHelper) Head

func (hr *HttpHelper) Head(path string, h func(http.ResponseWriter, *http.Request)) error

func (*HttpHelper) NotFoundHandler

func (hr *HttpHelper) NotFoundHandler(h http.Handler)

func (*HttpHelper) OPTIONS

func (hr *HttpHelper) OPTIONS(path string, h func(c *Context)) error

func (*HttpHelper) Options

func (hr *HttpHelper) Options(path string, h func(http.ResponseWriter, *http.Request)) error

func (*HttpHelper) PATCH

func (hr *HttpHelper) PATCH(path string, h func(c *Context)) error

func (*HttpHelper) POST

func (hr *HttpHelper) POST(path string, h func(c *Context)) error

func (*HttpHelper) PUT

func (hr *HttpHelper) PUT(path string, h func(c *Context)) error

func (*HttpHelper) Patch

func (hr *HttpHelper) Patch(path string, h func(http.ResponseWriter, *http.Request)) error

func (*HttpHelper) Post

func (hr *HttpHelper) Post(path string, h func(http.ResponseWriter, *http.Request)) error

func (*HttpHelper) Put

func (hr *HttpHelper) Put(path string, h func(http.ResponseWriter, *http.Request)) error

func (*HttpHelper) Run

func (h *HttpHelper) Run(addr ...string)

func (*HttpHelper) ServeHTTP

func (h *HttpHelper) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*HttpHelper) TRACE

func (hr *HttpHelper) TRACE(path string, h func(c *Context)) error

func (*HttpHelper) Trace

func (hr *HttpHelper) Trace(path string, h func(http.ResponseWriter, *http.Request)) error

func (*HttpHelper) Use

func (h *HttpHelper) Use(handler negroni.Handler)

func (*HttpHelper) UseHandler

func (h *HttpHelper) UseHandler(handler http.Handler)

func (*HttpHelper) UseHandlerFunc

func (h *HttpHelper) UseHandlerFunc(handlerFunc http.HandlerFunc)

Jump to

Keyboard shortcuts

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