Example #1
0
func main() {
	r := gin.New()
	r.Use(gin.LoggerWithWriter(log.Writer()))
	r.Use(gin.RecoveryWithWriter(log.Writer()))

	tmpl, err := template.New("").Funcs(tmpl.TemplateFuncs).ParseGlob("templates/*")
	if err != nil {
		panic(err)
	}
	r.SetHTMLTemplate(tmpl)
	r.Static(path.Static("/"), "static")
	r.Static(path.Css("/"), "assets/css")
	r.Static(path.Js("/"), "assets/js")
	r.Static(path.Font("/"), "assets/fonts")
	r.NoRoute(ctl.Render404)

	r.GET(path.Root, posts.Index)
	r.GET(path.Posts, posts.Index)
	r.GET(path.NewPost, posts.New)
	r.POST(path.NewPost, posts.Create)
	r.GET(path.Post(":id"), posts.Show)
	r.GET(path.EditPost(":id"), posts.Edit)
	r.POST(path.EditPost(":id"), posts.Update)
	r.POST(path.DeletePost(":id"), posts.Destroy)

	r.GET(path.Register, users.New)
	r.POST(path.Register, users.Create)
	r.GET(path.Login, users.SessionNew)
	r.POST(path.Login, users.SessionCreate)
	r.GET(path.Logout, users.SessionDestroy)

	r.Run(":8080")
}
Example #2
0
var TemplateFuncs = template.FuncMap{
	"map": tmplMap,

	"rootPath":       func() string { return path.Root },
	"staticFilePath": func(p string) string { return path.Static(p) },
	"cssPath":        func(p string) string { return path.Css(p) },
	"jsPath":         func(p string) string { return path.Js(p) },

	"icon":       hlpIcon,
	"errorsList": hlpErrorsList,

	"postsPath":      func() string { return path.Posts },
	"newPostPath":    func() string { return path.NewPost },
	"postPath":       func(id interface{}) string { return path.Post(id) },
	"editPostPath":   func(id interface{}) string { return path.EditPost(id) },
	"deletePostPath": func(id interface{}) string { return path.DeletePost(id) },

	"registerPath": func() string { return path.Register },
	"loginPath":    func() string { return path.Login },
	"logoutPath":   func() string { return path.Logout },
}

func tmplMap(args ...interface{}) (ret gin.H) {
	ret = make(gin.H, len(args)/2)

	for i := 0; i < len(args)-1; i += 2 {
		key, ok := args[i].(string)
		if !ok {
			continue
		}
		value := args[i+1]