ak

package module
v0.0.0-...-69d5c49 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: 3

README

ak

#####一个开发golang简单web小框架 ###例如

package main

import (
    "io/ioutil"
	"os"
)

import(
	"github.com/hechuangqiang/ak"
)

func main(){
	
	ak.AddRoute("/hello",func(ctx *ak.Context) {
		ctx.WriteJson("哈哈")
	})
	
	ak.AddRoute("/test",func(ctx *ak.Context){
		ctx.Data["name"] = "张三"
		ctx.WriteTpl("text.html")
	})
	
	ak.AddRoute("/user",func(ctx *ak.Context) {
		ctx.Data["users"] = []string{"张三","李四","王五"}
		ctx.WriteTpl("user/user.htm")
	})
	
	ak.AddRoute("/redirct",func(ctx *ak.Context){
		ctx.Redirect("/user")
	})
	
	ak.AddRoute("/panic",func(ctx *ak.Context) {
		panic("ok")
	})
	
	ak.AddRoute("/download",func(ctx *ak.Context){
		f,err := os.Open("hello.go")
		if err != nil {
			ctx.Abort(500,"open file fail")
			return
		}
		defer f.Close()
		buf,_ := ioutil.ReadAll(f)
		ctx.WriteStream("1t.html","application/octet-stream",buf)
	})
	
	ak.Run(":9000")
}

Documentation

Index

Constants

View Source
const (
	GET     = 0x00002000
	POST    = 0x00004000
	PUT     = 0x00008000
	PATCH   = 0x00020000
	DELETE  = 0x00040000
	HEAD    = 0x00080000
	OPTIONS = 0x00200000
	CONNECT = 0x00400000
	TRACE   = 0x00800000
)

Variables

This section is empty.

Functions

func AddFilter

func AddFilter(filter Filter)

给simpleServer添加过滤器

func AddRoute

func AddRoute(methods int, url string, f actionFunc)

给simpleServer添加路由

func AddStaticDir

func AddStaticDir(dir string)

给simpleServer添加静态文件夹

func RunSimpleServer

func RunSimpleServer(addr string)

启动simpleServer服务

func SetTplDelim

func SetTplDelim(leftDelim, rightDelim string)

给simpleServer设置模板标签边界

func SetTplPath

func SetTplPath(tplPath string)

给simpleServer设置模板路径

func StartSession

func StartSession(state bool)

开启session处理

Types

type Context

type Context struct {
	Request        *http.Request
	ResponseWriter http.ResponseWriter
	Params         map[string]string
	Data           map[string]interface{} //返回参数定义

	Session *Session
	// contains filtered or unexported fields
}

请求上下文

func (*Context) Abort

func (ctx *Context) Abort(status int, body string)

错误信息

func (*Context) ArgumentInt

func (ctx *Context) ArgumentInt(name string) (int, error)

获取参数int

func (*Context) ArgumentString

func (ctx *Context) ArgumentString(name string) string

获取参数string

func (*Context) GetArgInt

func (ctx *Context) GetArgInt(param string, def int) int

func (*Context) GetArgStr

func (ctx *Context) GetArgStr(param, def string) string

func (*Context) IsArgExists

func (ctx *Context) IsArgExists(param string) bool

func (*Context) ParseReqBodyJson

func (ctx *Context) ParseReqBodyJson(obj interface{}) error

将请求数据的json格式信息转换成结构

func (*Context) Redirect

func (ctx *Context) Redirect(url_ string)

重定向 3xx

func (*Context) SetStatus

func (ctx *Context) SetStatus(status int)

func (*Context) WriteHtml

func (ctx *Context) WriteHtml(htmlStr string)

返回html

func (*Context) WriteJson

func (ctx *Context) WriteJson(content interface{})

返回json

func (*Context) WriteStream

func (ctx *Context) WriteStream(filename string, contentType string, data []byte)

字符流

func (*Context) WriteTpl

func (ctx *Context) WriteTpl(tplName string)

返回模板

type Filter

type Filter interface {
	Execute(ctx *Context) bool
}

定义过滤器

type Method

type Method int

func (Method) String

func (m Method) String() string

type Server

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

服务

func NewDefaultServer

func NewDefaultServer() *Server

创建默认server

func (*Server) AddFilter

func (s *Server) AddFilter(filter Filter)

添加过滤器

func (*Server) AddRoute

func (s *Server) AddRoute(methods int, url string, f actionFunc)

添加路由

func (*Server) AddStaticDir

func (s *Server) AddStaticDir(staticDir string)

添加静态资源文件夹

func (*Server) Run

func (s *Server) Run(addr string)

启动服务

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request)

继承http.Handler实现ServeHTTP方法

func (*Server) SetTplDelim

func (s *Server) SetTplDelim(leftDelim, rightDelim string)

设置模板标签边界

func (*Server) SetTplPath

func (s *Server) SetTplPath(tplPath string)

设置模板路径

func (*Server) StartSession

func (s *Server) StartSession(state bool)

是否开始session处理

type Session

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

定义session

func (*Session) Del

func (s *Session) Del(k string)

删除session数据

func (*Session) Get

func (s *Session) Get(k string) (interface{}, bool)

获取session数据

func (*Session) Put

func (s *Session) Put(k string, v interface{})

存入session

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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