-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain.go
More file actions
108 lines (85 loc) · 2.13 KB
/
Copy pathmain.go
File metadata and controls
108 lines (85 loc) · 2.13 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
106
107
108
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"os"
"time"
"github.com/rdbell/nvote/schemas"
checkErr "github.com/rdbell/nvote/check"
"github.com/gobuffalo/packr/v2"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/rdbell/go-nostr"
)
var pool *nostr.RelayPool
func init() {
rand.Seed(time.Now().UnixNano())
// Configure app
switch os.Getenv("NV_ENVIRONMENT") {
case "prod":
appConfig = readConfig("./config_prod.json")
break
case "local":
appConfig = readConfig("./config_local.json")
break
default:
appConfig = readConfig("./config_dev.json")
break
}
schemas.InitConfig(appConfig)
// Load templates
box := packr.New("WebTemplatesBox", "./views")
loadTemplates(box)
// Init SQLite DB
initSQLite()
setupPostsTable()
setupUsersTable()
setupVotesTable()
setupMetadataTable()
go fetchEvents()
}
func main() {
// Echo instance
e := echo.New()
// Middleware
if appConfig.Environment == "dev" {
e.Use(middleware.Logger())
}
e.Use(middleware.Recover())
// Don't perform any re-routing in development environment
if appConfig.Environment != "" && appConfig.Environment != "dev" {
e.Pre(httpsRedir)
}
// Remove trailing slashes from URL paths
e.Pre(middleware.RemoveTrailingSlash())
// Enable CSRF protection
e.Use(middleware.CSRFWithConfig(middleware.CSRFConfig{
TokenLookup: "form:csrf",
CookiePath: "/",
}))
// Set up custom context with user-related vars and response headers
e.Use(setupContext)
// Add X-Frame-Options header
e.Use(addXFrameOptionsHeader)
// Enable GZIP compression
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{Level: 5}))
// Set renderer
e.Renderer = t
// Error handler function
e.HTTPErrorHandler = httpErrorHandler
// Set routes
setRoutes(e)
// Start server
e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", appConfig.ListenPort)))
}
// readConfig reads a config file into an AppConfig struct
func readConfig(filePath string) *schemas.AppConfig {
file, err := ioutil.ReadFile(filePath)
checkErr.Panic(err)
a := &schemas.AppConfig{}
err = json.Unmarshal([]byte(file), &a)
checkErr.Panic(err)
return a
}