This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"database/sql" | |
"github.com/codegangsta/martini" | |
"github.com/codegangsta/martini-contrib/render" | |
"github.com/codegangsta/martini-contrib/binding" | |
_ "github.com/go-sql-driver/mysql" | |
"html/template" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
m.Use(render.Renderer(render.Options{ | |
Funcs: []template.FuncMap{ | |
{ | |
"formatTime": func(args ...interface{}) string { | |
t1 := time.Unix(args[0].(int64), 0) | |
return t1.Format(time.Stamp) | |
}, | |
})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//needs import ("time") | |
m.Use(render.Renderer(render.Options{ | |
Directory: "templates", | |
Layout: "layout", | |
Funcs: []template.FuncMap{ | |
{ | |
"formatTime": func(args ...interface{}) string { | |
t1 := time.Unix(args[0].(int64), 0) | |
return t1.Format(time.Stamp) | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//inside Martini GET function m.Get("/", func(r render.Render) {... | |
//newmap := map[string]interface{}{"metatitle": "this is custom title for SEO", "posts": posts} | |
//r.HTML(200, "posts", newmap) | |
//layout.tmpl | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//needs import ("github.com/codegangsta/martini-contrib/binding") | |
type Post struct { | |
Id int64 `db:"post_id"` | |
Created int64 | |
Title string `form:"Title"` | |
Body string `form:"Body" binding:"required"` | |
} | |
//make a post |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//needs import ("github.com/codegangsta/martini-contrib/binding") | |
type Post struct { | |
Id int64 `db:"post_id"` | |
Created int64 | |
Title string `form:"Title"` | |
Body string `form:"Body" binding:"required"` | |
} | |
//make a post |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//needs import ("github.com/codegangsta/martini-contrib/binding") | |
type Post struct { | |
Id int64 `db:"post_id"` | |
Created int64 | |
Title string `form:"Title"` | |
Body string `form:"Body" binding:"required"` | |
} | |
func (bp Post) Validate(errors *binding.Errors, req *http.Request) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
m.Get("/:id", func(args martini.Params, r render.Render) { | |
var post Post | |
err = dbmap.SelectOne(&post, "select * from posts where post_id=?", args["id"]) | |
//simple error check | |
if err != nil { | |
r.HTML(404, "error", "This post is not found.") | |
} else { | |
r.HTML(200, "post", post) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Update a user | |
m.Put("/users/:id", binding.Bind(User{}), func(args martini.Params, user User, r render.Render) { | |
//convert id from string to int64 | |
f, _ := strconv.ParseInt(args["id"],0,64) | |
user.Id = f | |
count, err := dbmap.Update(&user) | |
checkErr(err, "Update failed") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Update a post | |
m.Put("/posts/:id", binding.Bind(Post{}), func(args martini.Params, post Post, r render.Render, authuser sessionauth.User) { | |
//capture passed in parameters from Ajax call | |
var newTitle = post.Title | |
var newBody = post.Body | |
//retrieve the post to retrieve original owner | |
err = dbmap.SelectOne(&post, "select * from posts where post_id=?", args["id"]) |
OlderNewer