-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
124 lines (100 loc) · 3.01 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
package main
import (
"encoding/json"
"html/template"
"net/http"
"os"
"github.com/charmbracelet/log"
"github.com/joho/godotenv"
)
type Config struct {
Host string
Modules []Module `json:"modules"`
Analytics Analytics
}
type Module struct {
Name string `json:"name"`
Github string `json:"github"`
}
type Analytics struct {
Enabled bool `json:"enabled"`
Url string `json:"url"`
Id string `json:"id"`
}
func main() {
loadEnvironment()
modules, config := registerModules()
var port = os.Getenv("PORT")
if port == "" {
port = "3000"
log.Warn("Could not find PORT in environment variable, defaulting to 3000")
}
log.Info("Server is started on http://" + config.Host + ":" + port + "/")
http.HandleFunc("/{module}", func(w http.ResponseWriter, r *http.Request) {
if _, ok := modules[r.PathValue("module")]; ok {
type ModulePageData struct {
Module Module
Host string
Analytics Analytics
}
tmp := template.Must(template.ParseFiles("templates/module.tmpl"))
tmp.Execute(w, ModulePageData{Module: modules[r.PathValue("module")], Host: config.Host, Analytics: config.Analytics})
log.Info("Redirecting user to " + modules[r.PathValue("module")].Github)
} else {
w.WriteHeader(http.StatusNotFound)
tmp := template.Must(template.ParseFiles("templates/404.tmpl"))
tmp.Execute(w, config.Analytics)
log.Warn("Module not found for /" + r.PathValue("module"))
}
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Info("Received request for /")
type IndexPageData struct {
Modules []Module
Host string
Analytics Analytics
}
tmp := template.Must(template.ParseFiles("templates/index.tmpl"))
tmp.Execute(w, IndexPageData{Modules: config.Modules, Host: config.Host, Analytics: config.Analytics})
})
log.Fatal(http.ListenAndServe(":"+port, nil))
}
// Register all modules from modules.json
func registerModules() (map[string]Module, Config) {
// Load Modules File
configFile, err := os.ReadFile(os.Getenv("MODULES_FILE"))
if err != nil {
log.Error("Could not load modules file!")
}
// Parse Modules File
var config Config
_ = json.Unmarshal([]byte(configFile), &config)
var modules = make(map[string]Module)
for i := 0; i < len(config.Modules); i++ {
modules[config.Modules[i].Name] = config.Modules[i]
log.Info("Registered module " + config.Modules[i].Name + " from " + config.Modules[i].Github)
}
// Set Host
if os.Getenv("HOST") != "" {
config.Host = os.Getenv("HOST")
} else {
config.Host = "localhost"
log.Warn("Could not find HOST in environment variable, defaulting to localhost")
}
// Enable Umami Analytics
if os.Getenv("UMAMI_URL") != "" {
if os.Getenv("UMAMI_URL") != "" {
config.Analytics.Enabled = true
config.Analytics.Url = os.Getenv("UMAMI_URL")
config.Analytics.Id = os.Getenv("UMAMI_ID")
log.Info("Enabled Umami Analytics")
}
}
return modules, config
}
func loadEnvironment() {
err := godotenv.Load()
if err != nil {
log.Error("Error loading .env file")
}
}