Martini框架初学与源码解读
来源:互联网 发布:kx tda100 软件 编辑:程序博客网 时间:2024/05/21 07:46
Martini框架是使用Go语言作为开发语言的一个强力的快速构建模块化web应用与服务的开发框架。
一、安装
go get github.com/codegangsta/martini
二、使用
可以使用如下的代码来测试我们安装的包是否可用:
server.go
package mainimport "github.com/codegangsta/martini"func main() { m := martini.Classic() //创建一个典型的martini实例 m.Get("/", func() string { //接收对\的GET方法请求,第二个参数是对一请求的处理方法 return "Hello world!" }) m.Run() //运行服务器}
在命令行中输入下面的命令运行上面的代码:
go run server.go
接下来可以使用如下的网址访问应用:
http://localhost:3000
三、API举例
常量用于指定应用所处的环境:
const ( Dev string = "development" Prod string = "production" Test string = "test")
变量控制应用所处的环境:
var Env = Dev
1.type BeforeFunc
BeforeFunc类型的方法在ResponseWriter方法被生效前调用。
type BeforeFunc func(ResponseWriter)
2.type ClassicMartini
带有典型方法的Martini实例类型。
type ClassicMartini struct { *Martini Router}
3.func Classic() *ClassicMartini
使用这个方法创建一个典型的Martini实例。可以使用这个Martini实例来进行应用的管理:
func Classic() *ClassicMartini
4.type ReturnHandler
ReturnHandler是Martini提供的用于路由处理并返回内容的服务。ReturnHandler对于向基于值传递的ResponseWriter写入是可响应的。
type ReturnHandler func(Context, []reflect.Value)
5.type Route
Route是一个用于表示Martini路由层的接口。
type Route interface { // URLWith returns a rendering of the Route's url with the given string params. URLWith([]string) string Name(string)}
6.type Router
Router是Martini的路由接口。提供HTTP变量、处理方法栈、依赖注入。
type Router interface { // 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 // Any adds a route for any HTTP method request to the specified matching pattern. Any(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, Context)}
7.func NewRouter() Router
创建一个路由实例。
func NewRouter() Router
8.type Routes
Routes是Martini路由层的辅助服务。
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}
参考博客:
灰大羊-博客园
golang martini 源码阅读笔记之martini核心
阅读全文
0 0
- Martini框架初学与源码解读
- martini框架源码阅读分析
- Martini源码解析-实例1
- golang: Martini之inject源码分析
- golang: Martini之inject源码分析
- libgupnp源码解读之对象框架与设备发现
- spring 源码解读与设计详解:1 框架搭建
- 解读flask框架,flask源码解读
- ACE框架解读 - 源码篇
- 全方位解读Android多媒体框架源码
- 全方位解读Android多媒体框架源码
- 全方位解读Android多媒体框架源码
- HTMLparser开源框架包源码解读
- android AChartEnginee讲解之源码框架解读
- Java集合框架源码解读(1)ArrayList
- PHP框架CI之源码解读
- spring 源码解读与设计详解: 7 BeanDefinitionParserDelegate深入解读
- 源码项目与框架
- 使用easyBCD安装ubuntu双系统
- 习题6.8
- 6.7
- 配置mysql
- No modules name apps解决
- Martini框架初学与源码解读
- Sessions, Window Stations and Desktops(GetDesktopWindow函数得到的桌面句柄, 是Csrss.exe创建的一个窗口)
- Linux MySQL-5.7 root初始密码修改
- yum缓存文件的使用
- [监管法规]金融机构报告涉嫌恐怖融资的可疑交易管理办法〔2007〕第1号
- 深入解析ConcurrentHashMap的实现原理
- 【设计模式】常用Java设计模式之单例模式
- C51片内数据传送指令
- uml九种图和四种关系