-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
94 lines (81 loc) · 2.33 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
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"regexp"
"strings"
)
type Page struct {
Result string
Message string
Status string
}
var status = map[string]string{
"pass": "pass",
"fail": "fail",
"ok": "pass",
"ko": "fail",
"pending": "pending",
"wait": "pending",
"inconclusive": "inconclusive",
}
var validPath = regexp.MustCompile("^/([a-zA-Z0-9_.-]*)$")
func fileHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, fmt.Sprintf("static/%s", r.URL.Path[1:]))
}
func staticHandler(w http.ResponseWriter, r *http.Request) {
renderStaticTemplate(w, r.URL.Path[1:])
}
func dynamicHandler(w http.ResponseWriter, r *http.Request) {
m := validPath.FindStringSubmatch(r.URL.Path)
if m == nil {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
title := m[1]
if title == "" {
renderStaticTemplate(w, "home.html")
return
}
msg := r.URL.Query().Get("msg")
p := &Page{Result: strings.ToUpper(title), Message: msg, Status: status[title]}
renderDynamicTemplate(w, p)
}
func faviconHandler(w http.ResponseWriter, r *http.Request) {
referer := strings.Split(r.Header.Get("Referer"), "?")
referer = strings.Split(referer[0], "/")
icon := strings.ToLower(referer[len(referer)-1])
if validPath.MatchString("/" + icon) {
icon := status[icon]
if icon == "" {
icon = "default"
}
http.ServeFile(w, r, fmt.Sprintf("favicon/%s.ico", icon))
return
}
http.Error(w, "Invalid Referer", http.StatusPreconditionFailed)
}
func renderDynamicTemplate(w http.ResponseWriter, p *Page) {
templates := template.Must(template.ParseFiles("templates/base.html", "templates/dynamic.html"))
err := templates.Execute(w, p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func renderStaticTemplate(w http.ResponseWriter, title string) {
templates := template.Must(template.ParseFiles("templates/base.html", "templates/"+title))
err := templates.Execute(w, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func main() {
http.HandleFunc("/", dynamicHandler)
http.HandleFunc("/favicon.ico", faviconHandler)
http.HandleFunc("/legal.html", staticHandler)
http.HandleFunc("/home.html", staticHandler)
http.HandleFunc("/style.css", fileHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}