-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmain.go
More file actions
105 lines (85 loc) · 2.94 KB
/
Copy pathmain.go
File metadata and controls
105 lines (85 loc) · 2.94 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"html/template"
"net/http"
"os"
"strconv"
cshAuth "github.com/computersciencehouse/csh-auth"
"github.com/computersciencehouse/vote/database"
"github.com/computersciencehouse/vote/logging"
"github.com/computersciencehouse/vote/sse"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
"mvdan.cc/xurls/v2"
)
var VOTE_TOKEN = os.Getenv("VOTE_TOKEN")
var CONDITIONAL_GATEKEEP_URL = os.Getenv("VOTE_CONDITIONAL_URL")
var VOTE_HOST = os.Getenv("VOTE_HOST")
// Dev mode flags
var DEV_DISABLE_ACTIVE_FILTERS bool = os.Getenv("DEV_DISABLE_ACTIVE_FILTERS") == "true"
var DEV_FORCE_IS_EBOARD bool = os.Getenv("DEV_FORCE_IS_EBOARD") == "true"
var DEV_FORCE_IS_EVALS bool = os.Getenv("DEV_FORCE_IS_EVALS") == "true"
func inc(x int) string {
return strconv.Itoa(x + 1)
}
func MakeLinks(s string) template.HTML {
rx := xurls.Strict()
s = template.HTMLEscapeString(s)
safe := rx.ReplaceAllString(s, `<a href="$0" target="_blank">$0</a>`)
return template.HTML(safe)
}
var oidcClient = OIDCClient{}
var broker *sse.Broker
func main() {
godotenv.Load()
database.Client = database.Connect()
r := gin.Default()
r.StaticFS("/static", http.Dir("static"))
r.SetFuncMap(template.FuncMap{
"inc": inc,
"MakeLinks": MakeLinks,
})
r.LoadHTMLGlob("templates/*")
broker = sse.NewBroker()
csh := cshAuth.CSHAuth{}
csh.Init(
os.Getenv("VOTE_OIDC_ID"),
os.Getenv("VOTE_OIDC_SECRET"),
os.Getenv("VOTE_JWT_SECRET"),
os.Getenv("VOTE_STATE"),
VOTE_HOST,
VOTE_HOST+"/auth/callback",
VOTE_HOST+"/auth/login",
[]string{"profile", "email", "groups"},
)
oidcClient.setupOidcClient(os.Getenv("VOTE_OIDC_ID"), os.Getenv("VOTE_OIDC_SECRET"))
InitConstitution()
if DEV_DISABLE_ACTIVE_FILTERS {
logging.Logger.WithFields(logrus.Fields{"method": "main init"}).Warning("Dev disable active filters is set!")
}
if DEV_FORCE_IS_EBOARD {
logging.Logger.WithFields(logrus.Fields{"method": "main init"}).Warning("Dev force eboard is set!")
}
if DEV_FORCE_IS_EVALS {
logging.Logger.WithFields(logrus.Fields{"method": "main init"}).Warning("Dev force evals is set!")
}
r.GET("/auth/login", csh.AuthRequest)
r.GET("/auth/callback", csh.AuthCallback)
r.GET("/auth/logout", csh.AuthLogout)
r.GET("/", csh.AuthWrapper(GetHomepage))
r.GET("/closed", csh.AuthWrapper(GetClosedPolls))
r.GET("/create", csh.AuthWrapper(GetCreatePage))
r.POST("/create", csh.AuthWrapper(CreatePoll))
r.GET("/poll/:id", csh.AuthWrapper(GetPollById))
r.POST("/poll/:id", csh.AuthWrapper(VoteInPoll))
r.GET("/results/:id", csh.AuthWrapper(GetPollResults))
r.POST("/poll/:id/hide", csh.AuthWrapper(HidePollResults))
r.POST("/poll/:id/close", csh.AuthWrapper(ClosePoll))
r.GET("/eboard", csh.AuthWrapper(HandleGetEboardVote))
r.POST("/eboard", csh.AuthWrapper(HandlePostEboardVote))
r.POST("/eboard/manage", csh.AuthWrapper(HandleManageEboardVote))
r.GET("/stream/:topic", csh.AuthWrapper(broker.ServeHTTP))
go broker.Listen()
r.Run()
}