This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.go
157 lines (146 loc) · 4.68 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
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"github.com/kyokomi/emoji"
"github.com/qdm12/golibs/healthcheck"
"github.com/qdm12/golibs/logging"
"github.com/qdm12/golibs/params"
"github.com/qdm12/golibs/server"
)
func main() {
ctx := context.Background()
os.Exit(_main(ctx, os.Args))
}
func _main(ctx context.Context, args []string) int {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
if healthcheck.Mode(args) {
if err := healthcheck.Query(); err != nil {
fmt.Println(err)
return 1
}
return 0
}
fmt.Println("#####################################")
fmt.Println("########## StackEdit Server #########")
fmt.Println("########## by Quentin McGaw #########")
fmt.Println("########## Give some " + emoji.Sprint(":heart:") + " at ##########")
fmt.Println("# github.com/qdm12/stackedit-docker #")
fmt.Print("#####################################\n\n")
envParams := params.NewEnvParams()
logger, err := logging.NewLogger(logging.ConsoleEncoding, logging.InfoLevel, -1)
if err != nil {
fmt.Println(err)
return 1
}
listeningPort, warning, err := envParams.GetListeningPort(params.Default("8000"))
if len(warning) > 0 {
logger.Warn(warning)
}
if err != nil {
logger.Error(err)
return 1
}
logger.Info("Using internal listening port %s", listeningPort)
rootURL, err := envParams.GetRootURL(params.Default("/"))
if err != nil {
logger.Error(err)
return 1
}
logger.Info("Using root URL %q", rootURL)
productionRouter := http.NewServeMux()
productionRouter.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
urlStackeditPath := strings.TrimPrefix(r.URL.Path, rootURL)
filepath := "/dist/" + urlStackeditPath
switch urlStackeditPath {
case "/conf", "/app/conf":
bytes := getAllStackeditEnv()
if _, err := w.Write(bytes); err != nil {
logger.Error(err)
}
return
case "/":
filepath = "/static/landing/"
case "/sitemap.xml":
filepath = "/static/sitemap.xml"
case "/oauth2/callback":
filepath = "/static/oauth2/callback.html"
case "/app", "/app/":
filepath = "/dist/index.html"
default:
switch {
case strings.HasPrefix(urlStackeditPath, "/static/css/static/fonts/"):
filepath = "/dist/" + strings.TrimPrefix(urlStackeditPath, "/static/css/")
case strings.HasPrefix(urlStackeditPath, "/app/static/css/static/fonts/"):
filepath = "/dist/" + strings.TrimPrefix(urlStackeditPath, "/app/static/css/")
case strings.HasPrefix(urlStackeditPath, "/app/"):
filepath = "/dist/" + strings.TrimPrefix(urlStackeditPath, "/app/")
}
}
http.ServeFile(w, r, "/html"+filepath)
})
healthcheckHandlerFunc := healthcheck.GetHandler(func() error { return nil })
serverErrors := make(chan []error)
go func() {
serverErrors <- server.RunServers(ctx,
server.Settings{Name: "production", Addr: "0.0.0.0:" + listeningPort, Handler: productionRouter},
server.Settings{Name: "healthcheck", Addr: "127.0.0.1:9999", Handler: healthcheckHandlerFunc},
)
}()
osSignals := make(chan os.Signal, 1)
signal.Notify(osSignals,
syscall.SIGINT,
syscall.SIGTERM,
os.Interrupt,
)
select {
case errors := <-serverErrors:
for _, err := range errors {
logger.Error(err)
}
return 1
case signal := <-osSignals:
message := fmt.Sprintf("Stopping program: caught OS signal %q", signal)
logger.Warn(message)
return 2
case <-ctx.Done():
message := fmt.Sprintf("Stopping program: %s", ctx.Err())
logger.Warn(message)
return 1
}
}
// Returns all stackedit env values as JSON
func getAllStackeditEnv() []byte {
env := struct {
DropboxAppKey string `json:"dropboxAppKey"`
DropboxAppKeyFull string `json:"dropboxAppKeyFull"`
GithubClientID string `json:"githubClientId"`
GithubClientSecret string `json:"githubClientSecret"`
GoogleClientID string `json:"googleClientId"`
GoogleAPIKey string `json:"googleApiKey"`
WordpressClientID string `json:"wordpressClientId"`
PaypalReceiverEmail string `json:"paypalReceiverEmail"`
AllowSponsorship bool `json:"allowSponsorship"`
}{}
envParams := params.NewEnvParams()
env.DropboxAppKey, _ = envParams.GetEnv("DROPBOX_APP_KEY")
env.DropboxAppKeyFull, _ = envParams.GetEnv("DROPBOX_APP_KEY_FULL")
env.GithubClientID, _ = envParams.GetEnv("GITHUB_CLIENT_ID")
env.GithubClientSecret, _ = envParams.GetEnv("GITHUB_CLIENT_SECRET")
env.GoogleClientID, _ = envParams.GetEnv("GOOGLE_CLIENT_ID")
env.GoogleAPIKey, _ = envParams.GetEnv("GOOGLE_API_KEY")
env.WordpressClientID, _ = envParams.GetEnv("WORDPRESS_CLIENT_ID")
env.PaypalReceiverEmail, _ = envParams.GetEnv("PAYPAL_RECEIVER_EMAIL")
if len(env.PaypalReceiverEmail) > 0 {
env.AllowSponsorship = true
}
b, _ := json.Marshal(env)
return b
}