http://nasust.hatenablog.com/entry/2016/11/17/002020 for goroutine
---------------------------------------------------------
http://neos21.hatenablog.com/entry/2018/04/22/080000
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
https://qiita.com/To_BB/items/aa68027a9f319bce7a33
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーhttps://paiza.hatenablog.com/entry/2018/03/23/paizacloud_golang_revel
cf http://railsapps.github.io/installrubyonrails-ubuntu.html RORウブンツ
https://qiita.com/laineek/items/121a9a20d5eb26fb56f4 ROR超入門
------------------------------------------------------------
go/src/myapp/conf/app.conf:
[dev]
db.info = root:@/mydb?charset=utf8&parseTime=True
--------------------------------------
go/src/myapp/app/controllers/gorm.go:
package controllers
import (
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
"github.com/revel/revel"
"myapp/app/models"
"log"
)
var DB *gorm.DB
func InitDB() {
dbInfo, _ := revel.Config.String("db.info")
db, err := gorm.Open("mysql", dbInfo)
if err != nil {
log.Panicf("Failed gorm.Open: %v\n", err)
}
db.DB()
db.AutoMigrate(&models.Post{})
DB = db
}
---------------------------------------------
go/src/myapp/app/init.go:
package app
import (
"github.com/revel/revel"
"myapp/app/controllers"
)
...
func init() {
...
revel.OnAppStart(controllers.InitDB)
}
-------------------------------------------
go/src/myapp/app/models/post.go:
package models
type Post struct {
Id uint64 `gorm:"primary_key" json:"id"`
Body string `sql:"size:255" json:"body"`
}
----------------------------------------------
go/src/myapp/app/models/post.go:
package models
type Post struct {
Id uint64 `gorm:"primary_key" json:"id"`
Body string `sql:"size:255" json:"body"`
}
------------------------------------------
go/src/myapp/conf/routes:
GET / Post.RedirectToPosts
GET /posts Post.Index
POST /posts Post.Create
POST /posts/:id/delete Post.Delete
-----------------------------------------------
go/src/myapp/app/controllers/post.go:
package controllers
import (
"github.com/revel/revel"
"myapp/app/models"
"errors"
)
type Post struct {
*revel.Controller
}
func (c Post) Index() revel.Result {
posts := []models.Post{}
result := DB.Order("id desc").Find(&posts);
err := result.Error
if err != nil {
return c.RenderError(errors.New("Record Not Found"))
}
return c.Render(posts)
}
func (c Post) Create() revel.Result {
post := models.Post{
Body: c.Params.Form.Get("body"),
}
ret := DB.Create(&post)
if ret.Error != nil {
return c.RenderError(errors.New("Record Create failure." + ret.Error.Error()))
}
return c.Redirect("/posts")
}
func (c Post) Delete() revel.Result {
id := c.Params.Route.Get("id")
posts := []models.Post{}
ret := DB.Delete(&posts, id)
if ret.Error != nil {
return c.RenderError(errors.New("Record Delete failure." + ret.Error.Error()))
}
return c.Redirect("/posts")
}
func (c Post) RedirectToPosts() revel.Result {
return c.Redirect("/posts")
}
-------------------------------------------
go/myapp/app/views/Post/index.html:
{{set . "title" "Todo list"}}
{{template "header.html" .}}
<header class="jumbotron" style="background-color:#A9F16C">
<div class="container">
<div class="row">
<h1>Todo list</h1>
<p></p>
</div>
</div>
</header>
<div class="container">
<div class="row">
<div class="span6">
{{template "flash.html" .}}
</div>
</div>
</div>
<div class="container">
<form action="/posts" method="post">
<div class="form-group">
<div class="row">
<label for="todo" class="col-xs-2">Todo</label>
<input type="text" name="body" class="col-xs-8">
<div class="col-xs-2">
<button type="submit" class="btn btn-success">
<i class="fa fa-plus"></i> Add Todo
</button>
</div>
</div>
</div>
</form>
<h2>Current Todos</h2>
<table class="table table-striped todo-table">
<thead>
<th>Todos</th><th> </th>
</thead>
<tbody>
{{ range .posts }}
<tr>
<td>
<div>{{ .Body }}</div>
</td>
<td>
<form action="/posts/{{.Id}}/delete" method="post">
<button class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
{{ end }}
</tbody>
</table>
</div>
{{template "footer.html" .}}