-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
123 lines (101 loc) · 2.67 KB
/
server.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
package main
import (
"flag"
"log"
"os"
"os/exec"
"path"
"strings"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/metamemelord/portfolio-website/handlers"
"github.com/metamemelord/portfolio-website/pkg/worker"
)
func init() {
if os.Getenv("ENV") == "release" || os.Getenv("GIN_MODE") == "release" {
return
}
rebuildAllFlag := flag.Bool("rebuild-all", false, "Rebuild project and dependencies")
rebuildFlag := flag.Bool("rebuild", false, "Rebuild project")
flag.Parse()
rebuildAll := *rebuildAllFlag
rebuild := *rebuildFlag || rebuildAll
var err error
dir, err := os.Getwd()
if err != nil {
log.Fatalln("Don't have permission to read FS")
}
_, err = os.Stat(path.Join(dir, "dist", "index.html"))
if err == nil && !(rebuild || rebuildAll) {
log.Println("Seems like project is already built, skipping this step.")
return
}
rebuildAll = err != nil || rebuildAll
if rebuildAll {
log.Println("Performing cleanup for rebuilding..")
err = os.RemoveAll(path.Join(dir, "node_modules"))
if err != nil {
log.Fatalln("Could not delete node_modules")
}
err = os.RemoveAll(path.Join(dir, "dist"))
if err != nil {
log.Fatalln("Could not delete dist")
}
log.Print("Done!\n")
}
out, err := exec.Command("node", "-v").Output()
if err != nil {
log.Fatalln("Node not installed")
}
log.Println("Node.js", string(out))
if rebuildAll {
log.Println("Attempting to build project dependencies..")
if err = exec.Command("npm", "install").Run(); err != nil {
log.Fatalln("Could not build dependencies!")
}
log.Println("Done!")
}
if rebuild || rebuildAll {
log.Println("Building project...")
if err = exec.Command("npm", "run", "build").Run(); err != nil {
log.Fatalln("Could not build project!")
}
log.Println("Done!")
}
}
func init() {
if os.Getenv("APP_AUTH") == "" {
log.Fatalln("APP_AUTH not provided")
}
}
func main() {
g := gin.New()
g.Use(gin.LoggerWithWriter(os.Stdout))
g.Use(cors.Default())
g.Use(handlers.SubdomainRedirectionMiddleware)
PORT := os.Getenv("PORT")
if PORT == "" {
PORT = "3000"
}
PORT = ":" + PORT
handlers.Register(g)
log.Println("Portfolio running on port", PORT)
go func() {
for {
worker.RefreshData()
worker.CheckAndMarkRedirectionInactive()
time.Sleep(2 * time.Hour)
}
}()
envKeepAliveCron := strings.ToLower(os.Getenv("KEEP_ALIVE_CRON"))
if len(envKeepAliveCron) == 0 || envKeepAliveCron == "true" || envKeepAliveCron == "1" {
go worker.KeepAlive(time.Minute)
}
tlsEnabled := os.Getenv("TLS_ENABLED")
if tlsEnabled == "true" || tlsEnabled == "1" {
_ = g.RunTLS(PORT, os.Getenv("TLS_CERT_PATH"), os.Getenv("TLS_KEY_PATH"))
} else {
_ = g.Run(PORT)
}
}