-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
85 lines (77 loc) · 2.28 KB
/
main.go
File metadata and controls
85 lines (77 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
log "github.com/Sirupsen/logrus"
"github.com/go-macaron/binding"
"github.com/go-macaron/session"
"gopkg.in/macaron.v1"
)
func main() {
log.SetFormatter(&log.JSONFormatter{})
log.SetLevel(log.InfoLevel)
m := macaron.New()
m.Use(macaron.Recovery())
m.Use(session.Sessioner(session.Options{
// Name of provider. Default is "memory".
Provider: "memory",
// Provider configuration, it's corresponding to provider.
ProviderConfig: "",
// Cookie name to save session ID. Default is "MacaronSession".
CookieName: "MacaronSession",
// Cookie path to store. Default is "/".
CookiePath: "/",
// GC interval time in seconds. Default is 3600.
Gclifetime: 3600,
// Max life time in seconds. Default is whatever GC interval time is.
Maxlifetime: 3600,
// Use HTTPS only. Default is false.
Secure: false,
// Cookie life time. Default is 0.
CookieLifeTime: 0,
// Cookie domain name. Default is empty.
Domain: "",
// Session ID length. Default is 16.
IDLength: 16,
// Configuration section name. Default is "session".
Section: "session",
}))
m.Use(macaron.Static("public"))
m.Use(macaron.Renderer(macaron.RenderOptions{
Directory: "./templates",
}))
m.Use(func(ctx *macaron.Context) {
log.WithFields(log.Fields{
"uri": ctx.Req.RequestURI,
"method": ctx.Req.Method,
}).Info("Request logging")
ctx.Next()
})
m.Get("/", func(ctx *macaron.Context) {
ctx.Redirect("/api", 302)
})
m.Get("/api", func(ctx *macaron.Context, f *session.Flash) {
f.Success("Go works successful")
ctx.Data["Headline"] = "API Docs"
ctx.HTML(200, "apidoc")
})
m.Group("/api/persons", func() {
m.Get("/", overviewHandler)
m.Get("/:id", detailHandler)
m.Post("/", binding.Bind(Person{}), createHandler)
m.Put("/:id", binding.Bind(Person{}), upgradeHandler)
m.Patch("/:id", binding.Bind(Person{}), updateHandler)
m.Delete("/:id", deleteHandler)
m.Group("/:id/chapters", func() {
m.Get("/:id", overviewHandler)
m.Post("/new", overviewHandler)
m.Put("/update/:id", overviewHandler)
m.Delete("/delete/:id", overviewHandler)
})
})
m.NotFound(func(ctx *macaron.Context) {
log.WithFields(log.Fields{
"error": ctx.Req.RequestURI,
}).Error("Not found")
ctx.JSON(404, map[string]string{"error": "Route not defined"})
})
m.Run()
}