-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (96 loc) · 3.28 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
package main
import (
"context"
"net/http"
"strings"
"github.com/dimfeld/httptreemux/v5"
"log/slog"
"github.com/gaydin/journey/admin"
"github.com/gaydin/journey/configuration"
"github.com/gaydin/journey/flags"
"github.com/gaydin/journey/https"
"github.com/gaydin/journey/logger"
"github.com/gaydin/journey/server"
"github.com/gaydin/journey/store"
"github.com/gaydin/journey/structure/methods"
"github.com/gaydin/journey/templates"
)
func newHttpRedirect(httpsURL string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, httpsURL+r.RequestURI, http.StatusMovedPermanently)
}
}
func main() {
// Configuration is read from config.json by loading the configuration package
config := configuration.NewConfiguration()
log, closeLogFunc := logger.New(config)
defer func() {
if err := closeLogFunc(); err != nil {
slog.Default().Error("close log file error", logger.Error(err))
}
}()
// Database
db, err := store.New(config)
if err != nil {
log.Error("Couldn't initialize database", logger.Error(err))
return
}
// Global blog data
if err := methods.GenerateBlog(context.Background(), config.Url, db); err != nil {
log.Error("Couldn't generate blog data", logger.Error(err))
return
}
// Templates
if err := templates.Generate(context.Background(), db); err != nil {
log.Error("Couldn't compile templates", logger.Error(err))
return
}
// HTTP(S) Server
httpPort := configuration.Config.HttpHostAndPort
httpsPort := configuration.Config.HttpsHostAndPort
// Check if HTTP/HTTPS flags were provided
if flags.HttpPort != "" {
components := strings.SplitAfterN(httpPort, ":", 2)
httpPort = components[0] + flags.HttpPort
}
if flags.HttpsPort != "" {
components := strings.SplitAfterN(httpsPort, ":", 2)
httpsPort = components[0] + flags.HttpsPort
}
// Determine the kind of https support (as set in the config.json)
if configuration.Config.HttpsUsage {
httpsRouter := httptreemux.NewContextMux()
httpRouter := httptreemux.NewContextMux()
// Blog and pages as https
server.InitializeBlog(httpsRouter, db)
server.InitializePages(httpsRouter)
admin.InitializeAdmin(config, db, httpsRouter)
// Add redirection to http router
httpRouter.GET("/", newHttpRedirect(config.HttpsUrl))
httpRouter.GET("/*path", newHttpRedirect(config.HttpsUrl))
// Start https server
log.Info("Starting https server on port " + httpsPort + "...")
go func() {
if err := https.StartServer(httpsPort, logger.Middleware(httpsRouter, log)); err != nil {
log.Error("Couldn't start the HTTPS server", logger.Error(err))
return
}
}()
// Start http server
log.Info("Starting http server on port " + httpPort + "...")
if err := http.ListenAndServe(httpPort, logger.Middleware(httpRouter, log)); err != nil {
log.Error("Couldn't start the HTTP server:", logger.Error(err))
}
} else {
httpRouter := httptreemux.NewContextMux()
// Blog and pages as http
server.InitializeBlog(httpRouter, db)
server.InitializePages(httpRouter)
admin.InitializeAdmin(config, db, httpRouter)
// Start http server
log.Info("Starting http server on port " + httpPort + "...")
if err := http.ListenAndServe(httpPort, logger.Middleware(httpRouter, log)); err != nil {
log.Error("Couldn't start the HTTP server:", logger.Error(err))
}
}
}