-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
92 lines (74 loc) · 1.85 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
package main
import (
"log"
"net/http"
"os"
"github.com/BurntSushi/toml"
"github.com/fsnotify/fsnotify"
)
type Redirects struct {
Temporary map[string]string
Permanent map[string]string
}
var redirects Redirects
const redirectsFile = "config/redirects.toml"
func main() {
readRedirects()
go watchRedirectsFile()
readStats()
http.HandleFunc("/", handleRedirect)
log.Fatal(http.ListenAndServe(":8080", nil))
}
func readRedirects() {
file, err := os.ReadFile(redirectsFile)
if err != nil {
log.Fatal(err)
}
err = toml.Unmarshal([]byte(file), &redirects)
if err != nil {
log.Fatal(err)
}
log.Printf("Loaded %d temporary and %d permanent redirects", len(redirects.Temporary), len(redirects.Permanent))
}
func watchRedirectsFile() {
// Create new watcher
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
// Add config directory to watcher
err = watcher.Add("config")
if err != nil {
log.Fatal(err)
}
// Watch for events
for event := range watcher.Events {
if event.Has(fsnotify.Write) && event.Name == redirectsFile {
go readRedirects()
}
}
}
func handleRedirect(w http.ResponseWriter, r *http.Request) {
slug := r.URL.Path[1:] // Remove the leading slash
if url, ok := redirects.Permanent[slug]; ok {
log.Printf("Permanently redirecting %s", slug)
http.Redirect(w, r, url, http.StatusMovedPermanently)
updateStat(slug)
return
}
if url, ok := redirects.Temporary[slug]; ok {
log.Printf("Temporary redirecting %s", slug)
http.Redirect(w, r, url, http.StatusFound)
updateStat(slug)
return
}
// Read and serve custom 404 page, fallback to default if not found
contents, err := os.ReadFile("config/404.html")
if err != nil {
contents, _ = os.ReadFile("assets/404.html")
}
w.WriteHeader(404)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(contents)
}