server

package
v0.0.0-...-5f81c22 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2018 License: BSD-2-Clause Imports: 25 Imported by: 0

Documentation

Overview

fork from martini

fork from martini's router.go

Index

Constants

View Source
const (
	ContentType   = "Content-Type"
	ContentLength = "Content-Length"
	ContentBinary = "application/octet-stream"
	ContentText   = "text/plain; charset=UTF-8"
	ContentJSON   = "application/json; charset=UTF-8"
	ContentHTML   = "text/html; charset=UTF-8"
	ContentXHTML  = "application/xhtml+xml; charset=UTF-8"
	ContentXML    = "text/xml; charset=UTF-8"
)

Variables

This section is empty.

Functions

func AuthBasic

func AuthBasic(user, pass string, r *http.Request) bool

func AuthClient

func AuthClient(allows []string, r *http.Request) (auth bool)

func AuthFailed

func AuthFailed(w http.ResponseWriter)

func AuthSecureURI

func AuthSecureURI(user, pass, key string, r *http.Request) bool

func AuthXAPIKEY

func AuthXAPIKEY(key string, r *http.Request) bool

func GetValues

func GetValues(requestURI string) (values map[string]string, err error)

func Md5

func Md5(key string) string

encode md5

func NewContext

func NewContext(options ...CtxOptions) martini.Handler

NewContext new context

func Run

func Run(addr string, m http.Handler, ssl ...string) error

func StructToString

func StructToString(data interface{}) string

convert struct to string

Types

type Context

type Context struct {
	martini.Context
	http.ResponseWriter
	Req *http.Request
	// contains filtered or unexported fields
}

func (*Context) AuthFailed

func (c *Context) AuthFailed()

AuthFailed write auth failed message to client

func (*Context) Data

func (c *Context) Data(status int, v []byte)

Data render raw data

func (*Context) DelData

func (c *Context) DelData(key string)

DelData delete data

func (*Context) GetData

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

GetData get data

func (*Context) JSON

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

JSON render JSON

func (*Context) NotFound

func (c *Context) NotFound()

NotFound write page not found message to client

func (*Context) Params

func (c *Context) Params() (params Params)

Params get all params from router exmaple

s.Get("/user/:id", getUser)

func getUser(c *Content) {
   params := c.Params()
   id := params["id"]
 // or
   id := params.Get("id")
 // or
   id := c.Params().Get("id")
}

func (*Context) Request

func (c *Context) Request(body interface{}) (req *Request, err error)

Request read client's json data

func (*Context) Response

func (c *Context) Response(code int, body interface{}, message string, v ...interface{})

Response write json to client

func (*Context) SetData

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

SetData set data

func (*Context) StaticFile

func (c *Context) StaticFile(status int, file string)

HTML render

func (*Context) Text

func (c *Context) Text(status int, v string)

Text render Text

func (*Context) XML

func (c *Context) XML(status int, v interface{})

XML render XML

type CtxOptions

type CtxOptions struct {
	AppName string
	Version string
	// Appends the given charset to the Content-Type header. Default is "UTF-8".
	Charset string
	// Outputs human readable JSON
	IndentJSON bool
	// Outputs human readable XML
	IndentXML bool
	// Prefixes the JSON output with the given bytes.
	PrefixJSON []byte
	// Prefixes the XML output with the given bytes.
	PrefixXML []byte
	// Allows changing of output to XHTML instead of HTML. Default is "text/html"
	HTMLContentType string
}
type context struct {
	inject.Injector
	handlers []Handler
	action   Handler
	rw       ResponseWriter
	index    int
}

type Handler

type Handler interface{}

Handler can be any callable function. Martini attempts to inject services into the handler's argument list. Martini will panic if an argument could not be fullfilled via dependency injection.

type Params

type Params map[string]string

Params is a map of name/value pairs for named routes. An instance of martini.Params is available to be injected into any route handler.

func (Params) Get

func (p Params) Get(key string) string

Get get value by key

type Render

type Render render.Render

type RenderOpt

type RenderOpt render.Options

type ReqHeader

type ReqHeader struct {
	Action string
	Time   string
}

request header

type Request

type Request struct {
	Header ReqHeader   // request header
	Body   interface{} // body
}

request

func RequestReader

func RequestReader(input io.ReadCloser, body interface{}) (req *Request, err error)

read request data

func (*Request) String

func (req *Request) String() string

type Response

type Response struct {
	Code    int         // custom status code
	Message string      // message
	Body    interface{} // body
}

response

func ResponseWriter

func ResponseWriter(code int, message string, body interface{}) (res *Response)

initial response data code: status code message: response messages body: response body

func (*Response) String

func (res *Response) String() string

type ReturnHandler

type ReturnHandler func(martini.Context, []reflect.Value)

ReturnHandler is a service that Martini provides that is called when a route handler returns something. The ReturnHandler is responsible for writing to the ResponseWriter based on the values that are passed into this function.

type Route

type Route interface {
	// URLWith returns a rendering of the Route's url with the given string params.
	URLWith([]string) string
	// Name sets a name for the route.
	Name(string)
	// GetName returns the name of the route.
	GetName() string
	// Pattern returns the pattern of the route.
	Pattern() string
	// Method returns the method of the route.
	Method() string
}

Route is an interface representing a Route in Martini's routing layer.

type RouteMatch

type RouteMatch int
const (
	NoMatch RouteMatch = iota
	StarMatch
	OverloadMatch
	ExactMatch
)

func (RouteMatch) BetterThan

func (r RouteMatch) BetterThan(o RouteMatch) bool

Higher number = better match

type Router

type Router interface {
	Routes

	EnableSecure()

	// Group adds a group where related routes can be added.
	Group(string, func(Router), ...Handler)
	// Get adds a route for a HTTP GET request to the specified matching pattern.
	Get(string, ...Handler) Route
	// Patch adds a route for a HTTP PATCH request to the specified matching pattern.
	Patch(string, ...Handler) Route
	// Post adds a route for a HTTP POST request to the specified matching pattern.
	Post(string, ...Handler) Route
	// Put adds a route for a HTTP PUT request to the specified matching pattern.
	Put(string, ...Handler) Route
	// Delete adds a route for a HTTP DELETE request to the specified matching pattern.
	Delete(string, ...Handler) Route
	// Options adds a route for a HTTP OPTIONS request to the specified matching pattern.
	Options(string, ...Handler) Route
	// Head adds a route for a HTTP HEAD request to the specified matching pattern.
	Head(string, ...Handler) Route
	// Connect adds a route for a HTTP CONNECT request to the specified matching pattern.
	Connect(string, ...Handler) Route
	// Head adds a route for a HTTP TRACE request to the specified matching pattern.
	Trace(string, ...Handler) Route
	// Any adds a route for any HTTP method request to the specified matching pattern.
	Any(string, ...Handler) Route
	// AddRoute adds a route for a given HTTP method request to the specified matching pattern.
	AddRoute(string, string, ...Handler) Route

	// NotFound sets the handlers that are called when a no route matches a request. Throws a basic 404 by default.
	NotFound(...Handler)

	// Handle is the entry point for routing. This is used as a martini.Handler
	Handle(http.ResponseWriter, *http.Request, martini.Context)
}

Router is Martini's de-facto routing interface. Supports HTTP verbs, stacked handlers, and dependency injection.

func NewRouter

func NewRouter() Router

NewRouter creates a new Router instance. If you aren't using ClassicMartini, then you can add Routes as a service with:

m := martini.New()
r := martini.NewRouter()
m.MapTo(r, (*martini.Routes)(nil))

If you are using ClassicMartini, then this is done for you.

type Routes

type Routes interface {
	// URLFor returns a rendered URL for the given route. Optional params can be passed to fulfill named parameters in the route.
	URLFor(name string, params ...interface{}) string
	// MethodsFor returns an array of methods available for the path
	MethodsFor(path string) []string
	// All returns an array with all the routes in the router.
	All() []Route
}

Routes is a helper service for Martini's routing layer.

type Server

type Server struct {
	*martini.Martini
	Router
}

func NewServer

func NewServer(app, version string) *Server

func (*Server) AuthBasic

func (s *Server) AuthBasic(user, pass string)

func (*Server) AuthClient

func (s *Server) AuthClient(allows []string)

func (*Server) AuthSecureURI

func (s *Server) AuthSecureURI(user, pass, key string)

func (*Server) AuthXAPIKEY

func (s *Server) AuthXAPIKEY(key string)

func (*Server) NotFount

func (s *Server) NotFount()

func (*Server) Run

func (s *Server) Run(addr string, ssl ...string) error

func (*Server) Static

func (s *Server) Static(path, uri string)

Static server

func (*Server) UseRender

func (s *Server) UseRender()

//

func (*Server) UseSession

func (s *Server) UseSession(key string)

type Session

type Session sessions.Session

sessions

Jump to

Keyboard shortcuts

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