-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (117 loc) · 3.98 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
package main
import (
"embed"
"encoding/json"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/Fanny-Leicht-Gymnasium/untisDSB/config"
)
//go:embed static/*
var staticFiles embed.FS
//go:embed index.html
var indexHTML string
func main() {
err := config.LoadConfig()
if err != nil {
panic(err)
}
go config.WatchConfig(config.ConfigPath) // Start watching for changes
// Serve static files (like CSS, JS, images) from the "static" directory
http.Handle("/static/", http.FileServer(http.FS(staticFiles)))
// Serve HTML page
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(indexHTML))
})
// Serve the URLs for Untis
http.HandleFunc("/untis/urls", func(w http.ResponseWriter, r *http.Request) {
// Create a response structure with URLs
urls := map[string]string{
"today": config.Config.Untis.TodayURL,
"tomorrow": config.Config.Untis.TomorrowURL,
}
// Set response header and encode URLs to JSON
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(urls); err != nil {
http.Error(w, "Error encoding JSON", http.StatusInternalServerError)
}
})
// Serve file URLs in the advertisement path at /ad/urls
http.HandleFunc("/ad", func(w http.ResponseWriter, r *http.Request) {
dirPath := config.Config.Advertisement.Path
// Read all files in the directory
files, err := os.ReadDir(dirPath)
if err != nil {
http.Error(w, "Error reading directory", http.StatusInternalServerError)
return
}
// Create a list of URLs for the files
var fileUrls []string
for _, file := range files {
if !file.IsDir() {
// Generate the URL path for each file
fileUrl := "/ad/" + filepath.Base(file.Name())
fileUrls = append(fileUrls, fileUrl)
}
}
res := struct {
Urls []string `json:"urls"`
SwitchingTime uint `json:"switchingTime"`
RefetchTime uint `json:"refetchTime"`
ReloadIframeOnSizeChange bool `json:"reloadIframeOnSizeChange"`
FixedHight bool `json:"fixedHight"`
Fullscreen bool `json:"fullscreen"`
}{
Urls: fileUrls,
SwitchingTime: config.Config.Advertisement.SwitchingTime,
RefetchTime: config.Config.Advertisement.RefetchTime,
ReloadIframeOnSizeChange: config.Config.Advertisement.ReloadIframeOnSizeChange,
FixedHight: config.Config.Advertisement.FixedHight,
Fullscreen: config.Config.Advertisement.Fullscreen,
}
// Set response header and encode URLs to JSON
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(res); err != nil {
http.Error(w, "Error encoding JSON", http.StatusInternalServerError)
}
})
// Serve files from the advertisement directory
http.Handle("/ad/", http.StripPrefix("/ad/", http.FileServer(http.Dir(config.Config.Advertisement.Path))))
http.HandleFunc("/scrolling-text", func(w http.ResponseWriter, r *http.Request) {
var texts []string
texts = config.Config.ScrollText.Texts
if texts == nil {
texts = []string{}
}
if config.Config.ScrollText.Path != "" {
file, err := os.ReadFile(config.Config.ScrollText.Path)
if err != nil {
log.Printf("Error reading scrolling text file: %v", err)
} else {
for _, line := range strings.Split(string(file), "\n") {
line = strings.TrimSpace(line) // Trim spaces or newlines
if line != "" {
texts = append(texts, line)
}
}
}
}
res := struct {
Texts []string `json:"texts"`
RefetchTime uint `json:"refetchTime"`
}{
Texts: texts,
RefetchTime: config.Config.ScrollText.RefetchTime,
}
json.NewEncoder(w).Encode(res)
})
// Start the server
log.Printf("Server starting on %s...", config.Config.WebServer.ServerAddress)
err = http.ListenAndServe(config.Config.WebServer.ServerAddress, nil)
if err != nil {
log.Fatalf("Error starting server: %v", err)
}
}