/
main.go
127 lines (104 loc) · 2.71 KB
/
main.go
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//Blog main.go
package main
import (
//"github.com/davecheney/profile"
"net/http"
_ "net/http/pprof"
"github.com/goarne/blog/config"
"github.com/goarne/blog/handler"
"github.com/goarne/web"
"gopkg.in/mgo.v2"
)
//The application is simple blog webapplication with html and a REST interface.
//It is created with a custom middleware for handing httprequests,
//routing requests to handlers serving html or json content from a database.
func main() {
//Enables profiler...
go func() {
http.ListenAndServe("localhost:6060", nil)
}()
//Testing the databasesetup.
initDb()
router := web.NewWebRouter()
//The static route handles static html files.
router.AddRoute(createChainedHtmlRoute())
//Adds basic html CRUD support for articles
router.AddRoute(createNewArticleHtmlRoute())
router.AddRoute(createListArticleHtmlRoute())
router.AddRoute(createFindArticleHtmlRoute())
//Adds REST support for CRUD operations on articles.
router.AddRoute(createJsonRoute())
http.ListenAndServe(":8888", router)
}
//Creates a html route
func createNewArticleHtmlRoute() *web.Route {
r := web.NewRoute()
r.Path("/new_article.html")
r.Method("POST")
r.Header("Accept", "html")
r.Handler(handler.NewHtmlTemplateHandler())
return r
}
func createListArticleHtmlRoute() *web.Route {
r := web.NewRoute()
r.Path("/blog/article/list/")
r.Method("GET")
r.Header("Accept", "html")
r.Handler(handler.NewHtmlTemplateHandler())
return r
}
func createFindArticleHtmlRoute() *web.Route {
r := web.NewRoute()
r.Path("/article/{id}")
r.Header("Accept", "html")
r.Method("GET")
r.Handler(handler.NewHtmlTemplateHandler())
return r
}
func createChainedHtmlRoute() *web.Route {
r := web.NewRoute()
r.Path("/")
r.PathPrefix("/*.html")
r.PathPrefix("/css/")
r.PathPrefix("/ckeditor/")
r.PathPrefix("/img/")
r.PathPrefix("/fonts/")
r.PathPrefix("/js/")
r.PathPrefix("/less/")
r.PathPrefix("/mail/")
r.Method("GET")
hc := &web.HandlerChain{}
hc.Add(handler.NewStaticHtmlHandler("index.html"))
hc.Add(handler.NewFileHandler("./html/"))
r.Handler(hc)
return r
}
func createJsonRoute() *web.Route {
r := web.NewRoute()
r.Path("/blog/article/{id}")
r.Method("POST").Method(web.HttpPost).Method("PUT").Method("DELETE").Method("OPTIONS")
r.Header("Accept", "json")
r.Handler(handler.NewRestHandler())
return r
}
func initDb() {
session, err := mgo.Dial(config.DOCKER_MONGODB_URL)
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB(config.DB_NAME).C(config.COLL_ARTICLE)
// Index
index := mgo.Index{
Key: []string{"id"},
Unique: true,
DropDups: true,
Background: true,
Sparse: true,
}
err = c.EnsureIndex(index)
if err != nil {
panic(err)
}
}