-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdls.go
84 lines (66 loc) · 1.92 KB
/
dls.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
package main
import (
"html"
"html/template"
"log"
"net"
"net/http"
"os"
"strings"
"github.com/gorilla/mux"
)
type tmplFill struct {
Title string
SubTitle string
BackgroundColor string
Countdown string
CountdownID string
}
func main() {
var port = os.Getenv("dls_port")
if port == "" {
port = "3030"
}
router := mux.NewRouter()
router.HandleFunc("/", handleRequest)
router.NotFoundHandler = http.HandlerFunc(handleRequest)
http.Handle("/", router)
http.Handle("/vendor/", http.FileServer(http.Dir("static")))
http.Handle("/img/", http.FileServer(http.Dir("static")))
http.Handle("/css/", http.FileServer(http.Dir("static")))
log.Println("Running...")
log.Fatal(http.ListenAndServe(":"+port, nil))
}
func handleRequest(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("template/index.html"))
tmpl.Execute(w, resolveFill(r.Host))
}
func resolveFill(host string) tmplFill {
records, err := net.LookupTXT(host)
fill := tmplFill{Title: html.EscapeString(host), SubTitle: ""}
if err != nil {
return fill
}
for _, record := range records {
if strings.HasPrefix(record, "dls_title=") {
fill.Title = html.EscapeString(strings.Replace(record, "dls_title=", "", 1))
}
if strings.HasPrefix(record, "dls_subtitle=") {
fill.SubTitle = html.EscapeString(strings.Replace(record, "dls_subtitle=", "", 1))
}
if strings.HasPrefix(record, "dls_backgroundcolor=") {
fill.BackgroundColor = html.EscapeString(strings.Replace(record, "dls_backgroundcolor=", "", 1))
}
if strings.HasPrefix(record, "dls_countdown=") {
fill.Countdown = html.EscapeString(strings.Replace(record, "dls_countdown=", "", 1))
if fill.Title == "{{Countdown}}" {
fill.Title = ""
fill.CountdownID = "container-title"
} else if fill.SubTitle == "{{Countdown}}" {
fill.SubTitle = ""
fill.CountdownID = "container-subtitle"
}
}
}
return fill
}