Skip to content

Instantly share code, notes, and snippets.

@techslides
techslides / insert.go
Last active August 29, 2015 13:55
SQL INSERT with Go Lang and Martini
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"
)
@techslides
techslides / martini-render-timestamp-convert.go
Last active August 29, 2015 13:55
Martini Render FuncMap Function to Convert Unix timestamp into nice Date format to be used in a template file
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)
},
}))
@techslides
techslides / funcmap.go
Last active September 28, 2015 10:37
Martini Render FuncMap Function to unescape string into HTML and time converting
//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)
},
@techslides
techslides / layout.tmpl
Last active August 29, 2015 13:55
Passing variable for custom title to layout.html template in Martini
//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">
@techslides
techslides / gist:8761307
Created February 2, 2014 00:33
Martini and Binding utility for reading form params and validation
//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
@techslides
techslides / gist:8761310
Created February 2, 2014 00:33
Martini and Binding utility for reading form params and validation
//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
@techslides
techslides / martini-binding.go
Last active August 29, 2015 13:55
Martini and Binding utility for reading form params and validation
//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) {
@techslides
techslides / martini-error.go
Created February 2, 2014 16:02
Martini Quick Error Check and Rendering
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)
@techslides
techslides / update-delete.go
Last active August 29, 2015 13:56
Martini Routes for PUT and DELETE calls to users
//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")
@techslides
techslides / update-post.go
Created February 11, 2014 22:23
Updating a post with authentication and user validation
//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"])