-
Notifications
You must be signed in to change notification settings - Fork 5
/
cf-env.go
76 lines (66 loc) · 1.68 KB
/
cf-env.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
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"os"
)
var bgColor = "white" // set a sane default color for background
type Environment struct {
Color string
Environment []string
Header map[string][]string
}
func (e *Environment) SetColor(c string) {
e.Color = c
}
func (e *Environment) SetEnvironment() {
e.Environment = os.Environ()
}
func (e *Environment) SetHeader(r *http.Request) {
e.Header = r.Header
}
func killHandler(w http.ResponseWriter, r *http.Request) {
os.Exit(5)
}
func handler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" || r.URL.Path == "/index.html" {
env := Environment{}
env.SetEnvironment()
env.SetHeader(r)
env.SetColor(bgColor)
t := template.New("index.html")
t, err := t.ParseFiles("tmpl/index.html")
if err != nil {
log.Fatal("ParseFiles: ", err)
}
if t == nil {
log.Fatal("ParseFiles: template is nil", nil)
}
t.Execute(w, env)
} else {
http.Error(w, "File not found!", http.StatusInternalServerError)
}
}
func main() {
if c := os.Getenv("CFENV_BGCOLOR"); c != "" {
bgColor = c
}
// We will also check to see if $PORT was set, if it was $PORT takes precedence
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
// Set the listening port by setting the $CFENV_PORT env variable
if os.Getenv("CFENV_PORT") != "" {
log.Fatal("cf-env: CFENV_PORT has been deprecated please use PORT instead!")
}
http.HandleFunc("/kill", killHandler)
http.HandleFunc("/", handler)
http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets"))))
err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}