a web framework for golang
用 Go 实现的一个简单的 MVC 模式框架,目前支持:
- 路由/RESTFUL(route)
- 控制器(handler)
- 视图(templates)
- 表单(form)
- 静态文件(static)
- 数据库(database)
请确保Go环境已经安装,如未安装请参考 Go 环境安装,请安装最新版。
go get github.com/go-code/her
package main
import (
"github.com/go-code/her"
)
func main() {
app := her.NewApplication()
app.Route.Handle("/", func() string {
return "hello world!"
})
app.Route.Handle("/hello/{val}", func(val string) string {
return "hello " + val
})
app.Route.Handle("/hi/{val}", func(ctx *her.Context, val string) {
ctx.WriteString("hi " + val)
})
app.Start()
}
启动程序访问8080端口,默认端口为8080
app := her.NewApplication()
// app.Route and app.Database and app.Template
app.Start()
app := her.NewApplication(map[string]interface{}{
"TemplatePath": "templates",
"Address": "0.0.0.0",
"XSRFCookies": true,
"CookieSecret": "book_secert",
"Port": 8080,
"Debug": true,
})
Route (mux)
app.Route.Handle("/", func() {
// handle code
}) // get
app.Route.Handle("/", func() {
// handle code
}).Methods("POST") //post
app.Route.Handle("/{key}", func(key string) {
// handle code
})
app.Route.Handle("/{key}/{id:[0-9]+}", func(key string, id string) {
// handle code
})
app.Route.Handle("/", func() string {
return "hello world!"
})
app.Route.Handle("/", func(ctx *her.Context) {
ctx.WriteString("hello world!")
})
app.Route.Handle("/{val}", func(ctx *her.Context, val string) {
ctx.WriteString("hello world!")
})
app.Route.Handle("/static/{path:.*}", her.StaticFileHandler("static")) // static 为静态文件目录
// import go-sqlite3
import _ "github.com/mattn/go-sqlite3"
// config
app.Database.Connection("sqlite", "sqlite3", "./book.s3db") // key, driver, data source
// use
DB = her.NewDB()
db := DB.Open("sqlite")
defer db.Close()
// sql code
app.Template.FuncMap(map[string]interface{}{
"text": func(text string) template.HTML {
return template.HTML(text)
},
})
// use
{{text "her"}}
John, Monkey