doob

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2017 License: Apache-2.0 Imports: 22 Imported by: 0

README

Doob

-_-

Doob is a rest and a simple router handler

init invoke AddHandlerFunc(forwardUrl,methodStr,func) such as

  • add static folder
Doob.AddStaicPrefix("/static")
  • add forwardUrl func , use && split urls
  router := Doob.DefaultRouter()

  router.AddHandlerFunc("/Doob/origin/{who}/{do} && /Doob/origin1/{who}/{do}", origin, Doob.GET, Doob.POST, Doob.PUT, Doob.DELETE)
  • use Get,Post,Put,Delete,Options,Head method
  // get '/Doob' at the beginning base router
  doobPrefixRouter := Doob.GetRouter("Doob")

  // use `{}` distinction pathVariable
  doobPrefixRouter.Get("/{name}/{value}", func)
  // {} 中支持添加正则表达式,用`:`分割参数名和正则表达式
  doobPrefixRouter.Post("/{name:[0-9]+}/{value}", func)
  doobPrefixRouter.Put("/{name}/{value}", func)
  doobPrefixRouter.Delete("/{name}/{value}", func)
  doobPrefixRouter.Options("/{name}/{value}", func)
  doobPrefixRouter.Head("/{name}/{value}", func)
  • if your not has HEAD or OPTIONS method , doob is add default HEAD and OPTIOND method
  HEAD is return this url get method headers
  OPTIONS is return this url support methods
  • support func classify
  // 兼容原始http方法类
  func origin(w http.ResponseWriter, r *http.Request) {}

  // 根据doob 里的context 进行获取参数或者返回
  func ctx(ctx *Doob.Context) interface{} {}

  // 根据url参数自动注入参数
  // 返回值为string时为返回静态文件
  // 返回不为string时默认将解析该对象,并返回给请求用户
  func di(name, value string) interface{} {}

  // 返回 string 是 type, interface{} 是你需要处理的数据
  // 你的返回值将流向注册的返回值处理器进行匹配并处理
  // 当只返回string时,把interface当做nil
  //
  // 当只返回数据时,此时returnDealDefaultType 为 auto
  // 此时将根据 請求的header Accept参数进行判断type类型
  // 你可以通过SetReturnDealDefaultType(t string)设置默认的type类型
  func returnHtml() (string, interface{}) {}
  • next use
  Doob.Start(8888)

run you application

clone this project , run demo

  cd /sample
  go run demo.go

Documentation

Overview

在用户和实际逻辑之间做一个中转 封装相关方法帮用户更好的使用

Index

Constants

View Source
const (
	DOOB    = "doob"
	VERSION = " 0.1.4 dev"
)

Variables

This section is empty.

Functions

func AddBFilter added in v0.2.0

func AddBFilter(fs ...middleware.BeforeFilter)

func AddLFilter added in v0.2.0

func AddLFilter(fs ...middleware.LaterFilter)

func AddMiddlerware added in v0.2.0

func AddMiddlerware(fs ...middleware.Middleware)

func AddStaticPrefix

func AddStaticPrefix(prefixs ...string)

func Listen

func Listen(port int) error

func Start

func Start(port int)

start doob server

Types

type Context

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

对 ResponseWriter 和 request 封装的上下文

func (*Context) AddHeader added in v0.2.0

func (this *Context) AddHeader(name, value string)

添加返回header

func (*Context) BodyJson

func (this *Context) BodyJson() string

获取请求的 body 字符串

func (*Context) Forward added in v0.1.3

func (this *Context) Forward(forwardUrl string)

Forward one request

@panic DoobError

func (*Context) Ip added in v0.2.0

func (this *Context) Ip() string

func (*Context) Param

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

获取 参数名为 name 的参数值

func (*Context) ParamInt

func (this *Context) ParamInt(name string) int

获取参数名为 name 的参数值并转化为int类型 当转化失败时返回 0

func (*Context) PathParam added in v0.1.3

func (this *Context) PathParam(name string) string

func (*Context) PostParam added in v0.1.3

func (this *Context) PostParam(name string) string

func (*Context) Redirect

func (this *Context) Redirect(redirectUrl string, code int)

func (*Context) Seesion added in v0.2.0

func (this *Context) Seesion() session.Session

func (*Context) SetHttpStatus

func (this *Context) SetHttpStatus(num int)

设置 http 返回状态码

func (*Context) URI added in v0.2.0

func (this *Context) URI() string

func (*Context) WriteBytes

func (this *Context) WriteBytes(bytes []byte)

往 responseWriter 中写入内容

func (*Context) WriteJson

func (this *Context) WriteJson(jsonStruct interface{})

接受一个实体并转化为 json bytes 往 responseWriter 中写入 改 json bytes 并添加 header Application/json

type Doob added in v0.2.0

type Doob struct {
	Root *router.Node
	// contains filtered or unexported fields
}

Doob 实现 http Handle 接口

func (*Doob) ServeHTTP added in v0.2.0

func (d *Doob) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP 实现 http Handle 接口

type HttpMethod

type HttpMethod string

type Router

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

封装node,对外提供简单方法

func DefaultRouter

func DefaultRouter() Router

func GetRouter

func GetRouter(prefix string) Router

func (Router) AddHandlerFunc

func (r Router) AddHandlerFunc(allUrl string, handler interface{}, methods ...HttpMethod)

func (Router) Delete

func (r Router) Delete(allUrl string, handler interface{})

func (Router) Get

func (r Router) Get(allUrl string, handler interface{})

func (Router) Head

func (r Router) Head(allUrl string, handler interface{})

func (Router) Options

func (r Router) Options(allUrl string, handler interface{})

func (Router) Post

func (r Router) Post(allUrl string, handler interface{})

func (Router) Put

func (r Router) Put(allUrl string, handler interface{})

Directories

Path Synopsis
http
const
http related const store
http related const store
return_deal
处理用户注册的 handle func 的返回值 列如
处理用户注册的 handle func 的返回值 列如
在用户注册 handle func 的时候对函数进行分析 并分已不同的类别,在执行时根据类别进行不同的处理
在用户注册 handle func 的时候对函数进行分析 并分已不同的类别,在执行时根据类别进行不同的处理

Jump to

Keyboard shortcuts

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