/
main.go
126 lines (104 loc) · 3.96 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/loconsolutions/smalldocs/config"
"github.com/loconsolutions/smalldocs/context"
"github.com/loconsolutions/smalldocs/controllers"
"github.com/loconsolutions/smalldocs/models"
"github.com/loconsolutions/smalldocs/utils"
"github.com/jdkanani/goa"
"labix.org/v2/mgo"
)
func DBInit() *mgo.Session {
// Mongodb connection
// We need this object to establish a session to our MongoDB.
mongoDBDialInfo := &mgo.DialInfo{
Addrs: strings.Split(context.Config.Get("db.hosts"), ","),
Timeout: 60 * time.Second,
Database: context.Config.Get("db.database"),
// Username: Config.Get("db.username"),
// Password: Config.Get("db.password"),
}
// create a session which maintains a pool of socket connections
// to our MongoDB.
mongoSession, err := mgo.DialWithInfo(mongoDBDialInfo)
if err != nil {
log.Fatalf("CreateSession: %s\n", err)
}
mongoSession.SetMode(mgo.Monotonic, true)
return mongoSession
}
func main() {
// get current directory
root, err := os.Getwd()
utils.Check(err)
// load configuration
cfg := new(config.Config)
context.Config = cfg
err = context.Config.Load(filepath.Join(root, "config.ini"))
utils.Check(err)
// add root directory to config
cfg.Set("app.root", root)
cfg.Set("app.templates", filepath.Join(root, cfg.Get("app.templates")))
cfg.Set("app.static", filepath.Join(root, cfg.Get("app.static")))
// Database initiate
context.DBSession = DBInit()
models.ProjectInit()
models.TopicInit()
models.PageInit()
// static content handler
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("public"))))
// router
mux := new(goa.Router)
mux.SetRenderer(&goa.Renderer{
TemplateDir: context.Config.Get("app.templates"),
LayoutDir: context.Config.Get("app.layouts"),
})
mux.NotFound(controllers.NotFound)
mux.Get("/", controllers.Index)
// projects routes
mux.Get("/projects/?$", controllers.ProjectIndex)
mux.Get("/projects/all/?$", controllers.GetAllProjects)
mux.Get("/projects/:pid/?$", controllers.GetProject)
mux.Get("/projects/:pname/settings/?$", controllers.ProjectSetting)
mux.Post("/projects_check/?$", controllers.CheckProject)
mux.Post("/projects/?$", controllers.PostProject)
mux.Put("/projects/:pid/?$", controllers.SaveProject)
mux.Delete("/projects/:pid/?$", controllers.DeleteProject)
// project topics
mux.Get("/projects/:pid/topics/?$", controllers.GetTopics)
mux.Post("/projects/:pid/topics_check/?$", controllers.CheckTopic)
mux.Post("/projects/:pid/topics/?$", controllers.PostTopic)
mux.Get("/projects/:pid/topics/:tid/?$", controllers.GetTopic)
mux.Get("/topics/:tid/?$", controllers.GetTopic)
mux.Put("/projects/:pid/topics/:tid/?$", controllers.SaveTopic)
mux.Delete("/projects/:pid/topics/:tid/?$", controllers.DeleteTopic)
// topic pages
mux.Get("/projects/:pid/topics/:tid/pages/?$", controllers.GetPages)
mux.Post("/projects/:pid/topics/:tid/pages_check/?$", controllers.CheckPage)
mux.Post("/projects/:pid/topics/:tid/pages/?$", controllers.PostPage)
mux.Get("/projects/:pid/topics/:tid/pages/:pageId/?$", controllers.GetPage)
mux.Get("/pages/:pageId/?$", controllers.GetPage)
mux.Put("/projects/:pid/topics/:tid/pages/:pageId/?$", controllers.SavePage)
mux.Delete("/projects/:pid/topics/:tid/pages/:pageId/?$", controllers.DeletePage)
// docs routes
mux.Get("/docs/:pname/?$", controllers.DocumentIndex)
mux.Get("/docs/:pname/:topicName/?$", controllers.DocumentIndex)
mux.Get("/docs/:pname/:topicName/:pageName/?$", controllers.DocumentIndex)
// edit routes
mux.Get("/edit/:pname/?$", controllers.EditIndex)
mux.Get("/edit/:pname/:topicName/?$", controllers.EditIndex)
mux.Get("/edit/:pname/:topicName/:pageName/?$", controllers.EditIndex)
// add router to http handle
http.Handle("/", mux)
// Start server
serverURL := cfg.Get("server.host") + cfg.Get("server.port")
fmt.Printf("Listening on %s ...", serverURL)
log.Fatal(http.ListenAndServe(serverURL, nil))
}