forked from q6r/masteringdecisions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
181 lines (152 loc) · 5.65 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/astaxie/beego/config"
"github.com/gin-gonic/gin"
"github.com/go-gorp/gorp"
"github.com/rageix/ginAuth"
)
var dbmap *gorp.DbMap
func main() {
conf, err := config.NewConfig("ini", "config.conf")
if err != nil {
log.Fatalln(err)
}
dbmap = InitDatabase(conf)
defer func() {
err := dbmap.Db.Close()
if err != nil {
log.Fatalln(err)
}
}()
routes := gin.Default()
// Debug Mode only clean route
// used for testing purposes
if gin.Mode() == "debug" {
routes.GET("/clean", func(c *gin.Context) {
err := dbmap.TruncateTables()
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err})
return
}
c.JSON(http.StatusOK, gin.H{"result": "cleaned"})
})
}
// enable logging into a file
err = enableLoggerFile(routes, conf.String("logfile"))
if err != nil {
log.Fatalln(err)
}
// Templates
////////////////
routes.LoadHTMLGlob("templates/*")
routes.Static("static/", "static/")
// Roots of pages for the frontend
/////////////////////////
routes.GET("/", HRootHome)
routes.GET("/login.html", HRootLogin)
routes.GET("/logout.html", HRootLogout)
routes.GET("/ballot.html", HRootBallot)
routes.GET("/decision/:decision_id", HRootDecision)
routes.GET("/results/:decision_id", HRootResults)
// Person
////////////////
routes.POST("/person", ginAuth.Use, AuthAsAdmin, HPersonCreate)
routes.GET("/persons", HPersonsList)
routes.GET("/person/:person_id/info", HPersonInfo)
routes.GET("/person/:person_id/decisions", HPersonDecisions)
routes.DELETE("/person/:person_id", ginAuth.Use, AuthAsAdmin, HPersonDelete)
routes.PUT("/person/:person_id", ginAuth.Use, AuthAsAll, HPersonUpdate)
// Decision
////////////////
// decision homes
routes.POST("/decision", ginAuth.Use, AuthAsAll, HDecisionCreate)
routes.GET("/decision/:decision_id/duplicate", ginAuth.Use, AuthAsAll, HDecisionDuplicate)
routes.GET("/decisions", HDecisionsList)
routes.GET("/decision/:decision_id/info", HDecisionInfo)
routes.DELETE("/decision/:decision_id", ginAuth.Use, AuthAsAll, HDecisionDelete)
routes.PUT("/decision/:decision_id", ginAuth.Use, AuthAsAll, HDecisionUpdate)
// decision's alternatives
routes.POST("/decision/:decision_id/alternative", ginAuth.Use, AuthAsAll, HAlternativeCreate)
routes.GET("/decision/:decision_id/alternatives", HDecisionAlternativesList)
routes.GET("/decision/:decision_id/alternative/:alternative_id/info", HAlternativeInfo)
routes.DELETE("/decision/:decision_id/alternative/:alternative_id", ginAuth.Use, AuthAsAll, HAlternativeDelete)
routes.PUT("/decision/:decision_id/alternative/:alternative_id", ginAuth.Use, AuthAsAll, HAlternativeUpdate)
// decision's ballots
routes.GET("/decision/:decision_id/ballot/:ballot_id", HBallotAllInfo)
routes.GET("/decision/:decision_id/ballot/:ballot_id/invite", HBallotInvite)
routes.GET("/decision/:decision_id/ballots", HDecisionBallotsList)
routes.POST("/decision/:decision_id/ballot", HBallotCreate)
routes.POST("/decision/:decision_id/ballot_silent", HBallotCreateSilent)
routes.GET("/decision/:decision_id/ballot/:ballot_id/info", HBallotInfo)
routes.DELETE("/decision/:decision_id/ballot/:ballot_id", HBallotDelete)
routes.PUT("/decision/:decision_id/ballot/:ballot_id", HBallotUpdate)
routes.GET("/decision/:decision_id/ballot/:ballot_id/login/:secret", HBallotLogin)
routes.GET("/ballot_whoami", HBallotWhoami)
// decision's ballot's votes
routes.GET(
"/decision/:decision_id/ballot/:ballot_id/alternative/:alternative_id/criterion/:criterion_id/vote/:weight", HVoteCreate)
routes.GET(
"/decision/:decision_id/ballot/:ballot_id/votes", HVotesBallotList)
routes.DELETE(
"/decision/:decision_id/ballot/:ballot_id/alternative/:alternative_id/criterion/:criterion_id/vote",
HVoteDelete)
routes.PUT(
"/decision/:decision_id/ballot/:ballot_id/alternative/:alternative_id/criterion/:criterion_id/vote/:weight",
HVoteUpdate)
// decision's ballot's rating criterion
routes.GET(
"/decision/:decision_id/ballot/:ballot_id/criterion/:criterion_id/vote/:rating", HRatingCreate)
routes.GET(
"/decision/:decision_id/criterion/:criterion_id/votes", HRatingBallots)
routes.DELETE(
"/decision/:decision_id/ballot/:ballot_id/criterion/:criterion_id/vote",
HRatingDelete)
routes.PUT(
"/decision/:decision_id/ballot/:ballot_id/criterion/:criterion_id/vote/:rating",
HRatingUpdate)
// decision's criterions
routes.GET("/decision/:decision_id/criterions", HDecisionCriterionsList)
routes.POST("/decision/:decision_id/criterion", HCriterionCreate)
routes.GET("/decision/:decision_id/criterion/:criterion_id/info", HCriterionInfo)
routes.DELETE("/decision/:decision_id/criterion/:criterion_id", HCriterionDelete)
routes.PUT("/decision/:decision_id/criterion/:criterion_id", HCriterionUpdate)
// Login/Logout
routes.POST("/login", HAuthLogin)
routes.GET("/logout", HAuthLogout)
routes.GET("/whoami", ginAuth.Use, AuthAsAll, HAuthWhoAmI)
// Setup the authentication
ginAuth.ConfigPath = "./config.conf"
ginAuth.Unauthorized = HAuthUnauthenticated
ginAuth.Authorized = HAuthAuthenticated
err = ginAuth.LoadConfig()
if err != nil {
log.Fatalln(err)
}
httpPort, err := conf.Int("http_port")
if err != nil {
log.Fatalln(err)
}
err = routes.Run(fmt.Sprintf(":%d", httpPort))
if err != nil {
log.Fatalln(err)
}
}
// enableLoggerFile enables logging to a file path
// empty logFilePath to disable logging to a file
func enableLoggerFile(routes *gin.Engine, logFilePath string) error {
// Don't want logger
if logFilePath == "" {
return nil
}
logFile, err := os.OpenFile(logFilePath,
os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
if err != nil {
return err
}
routes.Use(gin.LoggerWithWriter(logFile))
return nil
}