Go 的 MVC 框架:utron

jopen 9年前發布 | 12K 次閱讀 utron Google Go/Golang開發

utron是一個 Go 語言輕量級的 MVC 框架,用于快速構建可伸縮以及可靠的數據庫驅動的 Web 應用。

特性:

  • Postgres, MySQL 和 Foundation 數據庫支持

  • 模塊化

  • 支持中間件,所有 alice 兼容的中間件都可以使用

  • Gopher spirit (可使用 Go 語言的其他庫)

  • 輕量級,只包含 MVC

  • 支持多配置文件,包括 json、yaml 和 toml

控制器示例代碼:

package controllers

import (
    "net/http"
    "strconv"

    "github.com/gernest/utron"
    "github.com/gernest/utron/fixtures/todo/models"
    "github.com/gorilla/schema"
)

var decoder = schema.NewDecoder()

type TODO struct {
    *utron.BaseController
    Routes []string
}

func (t *TODO) Home() {
    todos := []*models.Todo{}
    t.Ctx.DB.Order("created_at desc").Find(&todos)
    t.Ctx.Data["List"] = todos
    t.Ctx.Template = "index"
    t.HTML(http.StatusOK)
}
func (t *TODO) Create() {
    todo := &models.Todo{}
    req := t.Ctx.Request()
    req.ParseForm()
    if err := decoder.Decode(todo, req.PostForm); err != nil {
        t.Ctx.Data["Message"] = err.Error()
        t.Ctx.Template = "error"
        t.HTML(http.StatusInternalServerError)
        return
    }

    t.Ctx.DB.Create(todo)
    t.Ctx.Redirect("/", http.StatusFound)
}

func (t *TODO) Delete() {
    todoID := t.Ctx.Params["id"]
    ID, err := strconv.Atoi(todoID)
    if err != nil {
        t.Ctx.Data["Message"] = err.Error()
        t.Ctx.Template = "error"
        t.HTML(http.StatusInternalServerError)
        return
    }
    t.Ctx.DB.Delete(&models.Todo{ID: ID})
    t.Ctx.Redirect("/", http.StatusFound)
}

func NewTODO() *TODO {
    return &TODO{
        Routes: []string{
            "get;/;Home",
            "post;/create;Create",
            "get;/delete/{id};Delete",
        },
    }
}

func init() {
    utron.RegisterController(NewTODO())
}

項目主頁:http://www.baiduhome.net/lib/view/home/1443354721620

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!