utron Go 的 MVC 框架 utron

agolangf • 2258 次点击    
这是一个分享于 的项目,其中的信息可能已经有所发展或是发生改变。
`utron` 是一个 Go 语言轻量级的 MVC 框架,用于快速构建可伸缩以及可靠的数据库驱动的 Web 应用。 特性: * Postgres, MySQL 和 Foundation 数据库支持 * 模块化 * 支持中间件,所有 [alice](https://github.com/justinas/alice) 兼容的中间件都可以使用 * Gopher spirit (可使用 Go 语言的其他库) * 轻量级,只包含 MVC * 支持多配置文件,包括 json、yaml 和 toml 控制器示例代码: <pre class="brush:cpp ;toolbar: true; auto-links: false;">package controllers import (     &#34;net/http&#34;     &#34;strconv&#34;     &#34;github.com/gernest/utron&#34;     &#34;github.com/gernest/utron/fixtures/todo/models&#34;     &#34;github.com/gorilla/schema&#34; ) var decoder = schema.NewDecoder() type TODO struct {     *utron.BaseController     Routes []string } func (t *TODO) Home() {     todos := []*models.Todo{}     t.Ctx.DB.Order(&#34;created_at desc&#34;).Find(&amp;todos)     t.Ctx.Data[&#34;List&#34;] = todos     t.Ctx.Template = &#34;index&#34;     t.HTML(http.StatusOK) } func (t *TODO) Create() {     todo := &amp;models.Todo{}     req := t.Ctx.Request()     req.ParseForm()     if err := decoder.Decode(todo, req.PostForm); err != nil {         t.Ctx.Data[&#34;Message&#34;] = err.Error()         t.Ctx.Template = &#34;error&#34;         t.HTML(http.StatusInternalServerError)         return     }     t.Ctx.DB.Create(todo)     t.Ctx.Redirect(&#34;/&#34;, http.StatusFound) } func (t *TODO) Delete() {     todoID := t.Ctx.Params[&#34;id&#34;]     ID, err := strconv.Atoi(todoID)     if err != nil {         t.Ctx.Data[&#34;Message&#34;] = err.Error()         t.Ctx.Template = &#34;error&#34;         t.HTML(http.StatusInternalServerError)         return     }     t.Ctx.DB.Delete(&amp;models.Todo{ID: ID})     t.Ctx.Redirect(&#34;/&#34;, http.StatusFound) } func NewTODO() *TODO {     return &amp;TODO{         Routes: []string{             &#34;get;/;Home&#34;,             &#34;post;/create;Create&#34;,             &#34;get;/delete/{id};Delete&#34;,         },     } } func init() {     utron.RegisterController(NewTODO()) }</pre>
授权协议:
MIT
开发语言:
Google Go 查看源码»
操作系统:
跨平台
2258 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传