flute

package module
v0.0.0-...-512f450 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2013 License: MIT Imports: 10 Imported by: 0

README

flute

一个基于mux的restful框架, 选择最好的第三方包进行整合,尽量不重复制造轮子

项目依赖的第三方包

  • github.com/gorilla/mux
  • github.com/bitly/go-simplejson
  • github.com/robfig/config

例子说明

package main

import (
  "flute"
)

type Users struct {
  flute.Controller
}

func (u *Users) Index() {
  u.RenderText(200, "mj")
}

func (u *Users) Show() {
  u.RenderText(200, "mj"+u.GetVars("id"))
}

func main() {
  flute.HandleFunc("/", "GET", func(c *flute.Context) {
    c.RenderText(200, "hello word")
  })
  var users Users
  flute.Resources("users", &users)
  flute.Start(":1234")
}

十分像rails的一些东西, 内部实现很多闭包,支持restful路由, 基于mux的小封装,更大的自由

学习资源

关于日志那一块请可以看: https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/12.1.md
立面有很好的说明, log.go 基本就是直接拿过来用的那种, 3q @astaxie

项目构建说明

惯例优于配置还是在的,所有配置相关的文件请放在config目录, example有例子可以看

接下来的东西

  • 封装好mgo驱动,提供更加方便的方法
  • 增加插件功能,比如before和after,以便做框架验证
  • 增加文档

适用范围

  • 不适合与web开发, 这个框架没有想过集成什么template或者我去实现
  • 适合写web api, 所有功能都将围绕web api来开发

我怎么会想到笛子这个单词了~~

Documentation

Overview

flute - 一个简单的golang web api 开发框架

Overview 开发这套框架主要是因为在开发api中,我找不到一个比较好的框架,ruby的框架主要还是太多东西了,我希望有一个简单的框架,同时性能更好一些的框架, 所以开发这套框架 这套框架的路由的想法来自rails的方法, 目前实现了Resources方法. 这套框架 这套框架可以干什么呢? 仅仅能开发web api而已,没有其他的功能. 目前Render方法还没做好, 如果你有耐心请等一等, 如果没有可以用http你们的包,代码不多可以直接看代码, 封装都很薄的.

下面建立一个简单的例子, 一个简单的hello word.

flute.HandleFunc("/", "GET", func(c *flute.Context) {
  c.RenderText(200, "hello word")
})
flute.Start(":1234")

github的wiki后续会加入更多的例子

Index

Constants

This section is empty.

Variables

View Source
var Config *config.Config
View Source
var Message map[string]string

Functions

func HandleFunc

func HandleFunc(path string, method string, f func(c *Context))

基础路由

func LoadConfig

func LoadConfig() error

加载配置文件到Message里面去

func Resources

func Resources(path string, controller ControllerInterface, middlewares ...MiddlewareInterface)

restful 路由风格

func Start

func Start() error

启动服务器, 需要传入一个地址

func StartLog

func StartLog()

Types

type Context

type Context struct {
	ResponseWriter http.ResponseWriter
	Request        *http.Request
}

这里把http的两个请求和成一个新的数据结构,加入一些方法

func (*Context) RenderJson

func (c *Context) RenderJson(status int, data interface{})

返回json

func (*Context) RenderText

func (c *Context) RenderText(status int, data string)

返回字符串

func (*Context) Vars

func (c *Context) Vars() map[string]string

获取url上面的参数,比如{id}

type Controller

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

controller 基类

func (*Controller) Create

func (controller *Controller) Create()

func (*Controller) Delete

func (controller *Controller) Delete()

func (*Controller) GetVars

func (controller *Controller) GetVars(key string) string

获取地址上的参数

func (*Controller) Index

func (controller *Controller) Index()

func (*Controller) Init

func (controller *Controller) Init(context *Context)

func (*Controller) ParamsFile

func (controller *Controller) ParamsFile(key string) (f multipart.File, h *multipart.FileHeader, e error)

获取文件

func (*Controller) ParamsFrom

func (controller *Controller) ParamsFrom(key string) string

获取表单的内容

func (*Controller) ParamsJson

func (controller *Controller) ParamsJson() (json *paramsJson.Json, e error)

获取body内容, 并解析成json, 详细看github.com/bitly/go-simplejson的用法

func (*Controller) Show

func (controller *Controller) Show()

func (*Controller) Update

func (controller *Controller) Update()

type ControllerInterface

type ControllerInterface interface {
	Init(context *Context)
	Index()
	Show()
	Create()
	Update()
	Delete()
}

controller 接口, 和rails基本类似

type Middleware

type Middleware struct {
	*Context
}

中间件类, 是一个类似controller的东西, 可以用户在发生组restful路由的时候进行定制 比如插入日子, 进行权限判断

func (*Middleware) After

func (plugin *Middleware) After()

路由结束前发生的操作

func (*Middleware) Before

func (plugin *Middleware) Before() bool

路由发生前发生的操作

func (*Middleware) Init

func (plugin *Middleware) Init(c *Context)

初始化

type MiddlewareInterface

type MiddlewareInterface interface {
	Init(context *Context)
	Before() bool
	After()
}

type Router

type Router struct {
	*mux.Router
}

内部路由

var Flute *Router

func NewRouter

func NewRouter() *Router

func (*Router) AddFunc

func (r *Router) AddFunc(path string, method string, f func(c *Context))

func (*Router) Resources

func (r *Router) Resources(path string, controller ControllerInterface, middlewares []MiddlewareInterface)

内部的restful路由实现, 还有中间件的调用, 总是感觉不优雅, 如果你有更好的思路,请告诉我,谢谢

func (*Router) Start

func (r *Router) Start(addr string) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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