cart

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2018 License: MIT Imports: 21 Imported by: 2

README

 ██████╗ █████╗ ██████╗ ████████╗
██╔════╝██╔══██╗██╔══██╗╚══██╔══╝
██║     ███████║██████╔╝   ██║
██║     ██╔══██║██╔══██╗   ██║
╚██████╗██║  ██║██║  ██║   ██║
 ╚═════╝╚═╝  ╚═╝╚═╝  ╚═╝   ╚═╝

Build Status codecov GoDoc

Examples

Using middleware

func main() {
	c.Use("/favicon.ico", cart.Favicon("./public/favicon.ico"))
	c.Use("/", func(context *cart.Context, next cart.Next) {
		fmt.Println("A")
		next()
		fmt.Println("A")

	})
	c.Use("/admin/*file", cart.Static("./public",true))

	c.Use("/admin/*file", func(context *cart.Context, next cart.Next) {
            //go on process if file not found in ./public
            next()
	})
}

Using GET POST ...

func main() {
	c.Route("/a").Route("/b", func(r *cart.Router) {
		r.ANY(func(context *cart.Context, next cart.Next) {
			context.Response.WriteString("ANY")
			next()
		})
		r.GET(func(context *cart.Context) {
			context.Response.WriteString(" /a/b")
		})
	})
	c.Route("/a/c", func(r *cart.Router) {
    		r.ANY(func(context *cart.Context, next cart.Next) {
    			context.Response.WriteString("ANY")
    			next()
    		})
    		r.GET(func(context *cart.Context) {
    			context.Response.WriteString(" /a/c")
    		})
    	})
}

Documentation

Overview

A HTTP web framework written in golang

 ██████╗ █████╗ ██████╗ ████████╗
██╔════╝██╔══██╗██╔══██╗╚══██╔══╝
██║     ███████║██████╔╝   ██║
██║     ██╔══██║██╔══██╗   ██║
╚██████╗██║  ██║██║  ██║   ██║
 ╚═════╝╚═╝  ╚═╝╚═╝  ╚═╝   ╚═╝

Index

Constants

View Source
const (
	DebugMode   string = "debug"
	ReleaseMode string = "release"
)
View Source
const ENV_CART_MODE = "CART_MODE"
View Source
const Version = "v1.0.5"

Variables

View Source
var DefaultErrorWriter io.Writer = os.Stderr
View Source
var DefaultWriter io.Writer = os.Stdout

Functions

func Dir

func Dir(root string, listDirectory bool) http.FileSystem

Dir returns a http.Filesystem that can be used by http.FileServer(). It is used internally in router.Static(). if listDirectory == true, then it works the same as http.Dir() otherwise it returns a filesystem that prevents http.FileServer() to list the directory files.

func IsDebugging

func IsDebugging() bool

IsDebugging returns true if the framework is running in debug mode. Use SetMode(cart.Release) to switch to disable the debug mode.

func SetMode

func SetMode(value string)

Types

type Context

type Context struct {
	Request  *http.Request
	Response ResponseWriter

	Router *Router
	Params Params
	Keys   map[string]interface{}
	// contains filtered or unexported fields
}

func (*Context) AbortRender added in v1.0.1

func (c *Context) AbortRender(code int, request string, err interface{})

func (*Context) AbortWithStatus added in v1.0.1

func (c *Context) AbortWithStatus(code int)

AbortWithStatus calls `Abort()` and writes the headers with the specified status code. For example, a failed attempt to authenticate a request could use: context.AbortWithStatus(401).

func (*Context) ClientIP added in v1.0.1

func (c *Context) ClientIP() string

ClientIP implements a best effort algorithm to return the real client IP, it parses X-Real-IP and X-Forwarded-For in order to work properly with reverse-proxies such us: nginx or haproxy. Use X-Forwarded-For before X-Real-Ip as nginx uses X-Real-Ip with the proxy's IP.

func (*Context) ContentType added in v1.0.1

func (c *Context) ContentType() string

ContentType returns the Content-Type header of the request.

func (*Context) Cookie

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

func (*Context) Data

func (c *Context) Data(code int, contentType string, data []byte)

Data writes some data into the body stream and updates the HTTP code.

func (*Context) ErrorHTML added in v1.0.1

func (c *Context) ErrorHTML(code int, title, content string)

error

func (*Context) File

func (c *Context) File(filepath string)

File writes the specified file into the body stream in a efficient way.

func (*Context) Get added in v1.0.1

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

Get returns the value for the given key, ie: (value, true). If the value does not exists it returns (nil, false)

func (*Context) GetBool added in v1.0.1

func (c *Context) GetBool(key string) (b bool)

GetBool returns the value associated with the key as a boolean.

func (*Context) GetDuration added in v1.0.1

func (c *Context) GetDuration(key string) (d time.Duration)

GetDuration returns the value associated with the key as a duration.

func (*Context) GetFloat64 added in v1.0.1

func (c *Context) GetFloat64(key string) (f64 float64)

GetFloat64 returns the value associated with the key as a float64.

func (*Context) GetHeader

func (c *Context) GetHeader(key string) string

GetHeader returns value from request headers

func (*Context) GetInt added in v1.0.1

func (c *Context) GetInt(key string) (i int)

GetInt returns the value associated with the key as an integer.

func (*Context) GetInt64 added in v1.0.1

func (c *Context) GetInt64(key string) (i64 int64)

GetInt64 returns the value associated with the key as an integer.

func (*Context) GetString added in v1.0.1

func (c *Context) GetString(key string) (s string)

GetString returns the value associated with the key as a string.

func (*Context) GetStringMap added in v1.0.1

func (c *Context) GetStringMap(key string) (sm map[string]interface{})

GetStringMap returns the value associated with the key as a map of interfaces.

func (*Context) GetStringMapString added in v1.0.1

func (c *Context) GetStringMapString(key string) (sms map[string]string)

GetStringMapString returns the value associated with the key as a map of strings.

func (*Context) GetStringMapStringSlice added in v1.0.1

func (c *Context) GetStringMapStringSlice(key string) (smss map[string][]string)

GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings.

func (*Context) GetStringSlice added in v1.0.1

func (c *Context) GetStringSlice(key string) (ss []string)

GetStringSlice returns the value associated with the key as a slice of strings.

func (*Context) GetTime added in v1.0.1

func (c *Context) GetTime(key string) (t time.Time)

GetTime returns the value associated with the key as time.

func (*Context) HTML

func (c *Context) HTML(code int, name string, obj interface{})

HTML renders the HTTP template specified by its file name. It also updates the HTTP code and sets the Content-Type as "text/html". See http://golang.org/doc/articles/wiki/

func (*Context) HTMLString added in v1.0.2

func (c *Context) HTMLString(name string, obj interface{}) string

render Template to String

func (*Context) Header

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

Header is a intelligent shortcut for c.Writer.Header().Set(key, value)

func (*Context) IndentedJSON

func (c *Context) IndentedJSON(code int, obj interface{})

IndentedJSON serializes the given struct as pretty JSON (indented + endlines) into the response body. It also sets the Content-Type as "application/json". WARNING: we recommend to use this only for development purposes since printing pretty JSON is more CPU and bandwidth consuming. Use Context.JSON() instead.

func (*Context) JSON

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

JSON serializes the given struct as JSON into the response body. It also sets the Content-Type as "application/json".

func (*Context) LayoutHTML added in v1.0.2

func (c *Context) LayoutHTML(code int, layout, name string, obj interface{})

render layout html

func (*Context) MustGet added in v1.0.1

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

MustGet returns the value for the given key if it exists, otherwise it panics.

func (*Context) Param added in v1.0.4

func (c *Context) Param(key string) (string, bool)

func (*Context) Redirect

func (c *Context) Redirect(code int, location string)

Redirect returns a HTTP redirect to the specific location.

func (*Context) Render

func (c *Context) Render(code int, r render.Render)

func (*Context) Set added in v1.0.1

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

Set is used to store a new key/value pair exclusively for this context. It also lazy initializes c.Keys if it was not used previously.

func (*Context) SetCookie

func (c *Context) SetCookie(
	name string,
	value string,
	maxAge int,
	path string,
	domain string,
	secure bool,
	httpOnly bool,
)

func (*Context) Status

func (c *Context) Status(code int)

func (*Context) Stream

func (c *Context) Stream(step func(w io.Writer) bool)

func (*Context) String

func (c *Context) String(code int, format string, values ...interface{})

String writes the given string into the response body.

func (*Context) XML

func (c *Context) XML(code int, obj interface{})

XML serializes the given struct as XML into the response body. It also sets the Content-Type as "application/xml".

type Engine

type Engine struct {
	Router

	NotFound HandlerFinal

	FuncMap  template.FuncMap
	Template *template.Template

	ForwardedByClientIP bool
	AppEngine           bool
	// contains filtered or unexported fields
}

func Default added in v1.0.1

func Default() *Engine

func New

func New() *Engine

func (*Engine) LoadHTMLGlob added in v1.0.1

func (engine *Engine) LoadHTMLGlob(pattern string)

func (*Engine) Run

func (e *Engine) Run(addr ...string) (server *http.Server, err error)

Run the server

func (*Engine) RunTLS added in v1.0.1

func (e *Engine) RunTLS(addr string, certFile string, keyFile string) (server *http.Server, err error)

RunTLS

func (*Engine) ServeHTTP

func (e *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*Engine) Server added in v1.0.5

func (e *Engine) Server(addr ...string) (server *http.Server)

func (*Engine) ServerKeepAlive added in v1.0.6

func (e *Engine) ServerKeepAlive(addr ...string) (server *http.Server)

func (*Engine) SetFuncMap added in v1.0.1

func (engine *Engine) SetFuncMap(funcMap template.FuncMap)

func (*Engine) SetHTMLTemplate added in v1.0.1

func (engine *Engine) SetHTMLTemplate(templ *template.Template)

type H

type H map[string]interface{}

func (H) MarshalXML

func (h H) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML allows type H to be used with xml.Marshal

type Handler

type Handler func(*Context, Next)

normal handler

func Favicon

func Favicon(relativePath string) Handler

func File added in v1.0.3

func File(relativePath string) Handler

func Logger

func Logger() Handler

func LoggerWithWriter

func LoggerWithWriter(out io.Writer) Handler

func Recovery added in v1.0.1

func Recovery() Handler

Recovery returns a middleware that recovers from any panics and writes a 500 if there was one.

func RecoveryRender added in v1.0.1

func RecoveryRender(out io.Writer) Handler

func RecoveryWithWriter added in v1.0.1

func RecoveryWithWriter(out io.Writer) Handler

func Static

func Static(relativePath string, listDirectory bool) Handler

type HandlerCompose

type HandlerCompose func(*Context, Next) Next

type HandlerFinal

type HandlerFinal func(*Context)

type HandlerRoute

type HandlerRoute func(*Router)

type Next

type Next func()

type Param

type Param struct {
	Key   string
	Value string
}

type Params

type Params []Param

func (Params) Get

func (ps Params) Get(name string) (string, bool)

type ResponseWriter

type ResponseWriter interface {
	http.ResponseWriter
	http.Hijacker
	http.Flusher
	http.CloseNotifier
	WriteString(string) (int, error)
	WriteHeaderNow()
	Status() int
	Size() int
}

type Router

type Router struct {
	Engine *Engine
	Path   string
	// contains filtered or unexported fields
}

func (*Router) ANY

func (r *Router) ANY(handler Handler) *Router

func (*Router) CONNECT

func (r *Router) CONNECT(handler HandlerFinal) *Router

func (*Router) DELETE

func (r *Router) DELETE(handler HandlerFinal) *Router

func (*Router) GET

func (r *Router) GET(handler HandlerFinal) *Router

func (*Router) HEAD

func (r *Router) HEAD(handler HandlerFinal) *Router

func (*Router) Handle

func (r *Router) Handle(httpMethod string, handler HandlerFinal) *Router

func (*Router) OPTIONS

func (r *Router) OPTIONS(handler HandlerFinal) *Router

func (*Router) PATCH

func (r *Router) PATCH(handler HandlerFinal) *Router

func (*Router) POST

func (r *Router) POST(handler HandlerFinal) *Router

func (*Router) PUT

func (r *Router) PUT(handler HandlerFinal) *Router

func (*Router) Route

func (r *Router) Route(relativePath string, handles ...HandlerRoute) *Router

func (*Router) TRACE

func (r *Router) TRACE(handler HandlerFinal) *Router

func (*Router) Use

func (r *Router) Use(relativePath string, handles ...Handler) *Router

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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