This repository has been archived by the owner on Jun 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
109 lines (94 loc) · 2.91 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
package main
import (
_ "embed"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/StevenWeathers/exothermic-story-mapping/pkg/database"
"github.com/StevenWeathers/exothermic-story-mapping/pkg/email"
"github.com/gorilla/mux"
"github.com/gorilla/securecookie"
"github.com/spf13/viper"
)
//go:embed schema.sql
var schemaSQL string
var embedUseOS bool
var (
version = "dev"
)
// ServerConfig holds server global config values
type ServerConfig struct {
// port the application server will listen on
ListenPort string
// the domain of the application for cookie securing
AppDomain string
// name of the frontend cookie
FrontendCookieName string
// name of the user cookie
SecureCookieName string
// controls whether or not the cookie is set to secure, only works over HTTPS
SecureCookieFlag bool
// email to promote a user to ADMIN on app startup
// the user should already be registered for this to work
AdminEmail string
// Whether or not to enable google analytics tracking
AnalyticsEnabled bool
// ID used for google analytics
AnalyticsID string
// the app version
Version string
// Which avatar service is utilized
AvatarService string
// PathPrefix allows the application to be run on a shared domain
PathPrefix string
}
type server struct {
config *ServerConfig
router *mux.Router
email *email.Email
cookie *securecookie.SecureCookie
database *database.Database
}
func main() {
embedUseOS = len(os.Args) > 1 && os.Args[1] == "live"
log.Println("Exothermic version " + version)
InitConfig()
cookieHashkey := viper.GetString("http.cookie_hashkey")
pathPrefix := viper.GetString("http.path_prefix")
router := mux.NewRouter()
if pathPrefix != "" {
router = router.PathPrefix(pathPrefix).Subrouter()
}
s := &server{
config: &ServerConfig{
ListenPort: viper.GetString("http.port"),
AppDomain: viper.GetString("http.domain"),
AdminEmail: viper.GetString("admin.email"),
FrontendCookieName: viper.GetString("http.frontend_cookie_name"),
SecureCookieName: viper.GetString("http.backend_cookie_name"),
SecureCookieFlag: viper.GetBool("http.secure_cookie"),
AnalyticsEnabled: viper.GetBool("analytics.enabled"),
AnalyticsID: viper.GetString("analytics.id"),
Version: version,
AvatarService: viper.GetString(("config.avatar_service")),
PathPrefix: pathPrefix,
},
router: router,
cookie: securecookie.New([]byte(cookieHashkey), nil),
}
s.email = email.New(s.config.AppDomain, s.config.PathPrefix)
s.database = database.New(s.config.AdminEmail, schemaSQL)
go h.run()
s.routes()
srv := &http.Server{
Handler: s.router,
Addr: fmt.Sprintf(":%s", s.config.ListenPort),
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Println("Access the WebUI via 127.0.0.1:" + s.config.ListenPort)
log.Fatal(srv.ListenAndServe())
}