-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathmain.go
156 lines (128 loc) · 2.87 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"flag"
"fmt"
"html/template"
"log"
"net/http"
"os"
"strconv"
"time"
)
var (
mux = http.NewServeMux()
DS *Datastore
DbHost string
DbUser string
DbPass string
DbPort int
DbName string
DbSSLMode string
)
type (
Content struct {
Title string
Hostname string
Entries []*LogEntry
}
)
func init() {
flag.StringVar(&DbHost, "db-host", "", "database host")
flag.IntVar(&DbPort, "db-port", 0, "database port")
flag.StringVar(&DbUser, "db-user", "", "database username")
flag.StringVar(&DbPass, "db-pass", "", "database password")
flag.StringVar(&DbName, "db-name", "", "database name")
flag.StringVar(&DbSSLMode, "db-ssl", "", "database ssl mode")
}
func loadTemplate(filename string) (*template.Template, error) {
return template.ParseFiles(filename)
}
func index(w http.ResponseWriter, r *http.Request) {
t, err := loadTemplate("templates/index.html.tmpl")
if err != nil {
fmt.Printf("error loading template: %s\n", err)
return
}
title := os.Getenv("TITLE")
hostname, err := os.Hostname()
if err != nil {
hostname = "unknown"
}
cnt := &Content{
Title: title,
Hostname: hostname,
}
t.Execute(w, cnt)
}
func dbIndex(w http.ResponseWriter, r *http.Request) {
DS.Add(&LogEntry{
Date: time.Now(),
Addr: r.RemoteAddr,
Path: r.URL.Path,
})
t, err := loadTemplate("templates/db.html.tmpl")
if err != nil {
fmt.Printf("error loading template: %s\n", err)
return
}
title := os.Getenv("TITLE")
hostname, err := os.Hostname()
if err != nil {
hostname = "unknown"
}
entries, err := DS.GetAll()
if err != nil {
log.Printf("error retrieving data: %s\n", err)
}
cnt := &Content{
Title: title,
Hostname: hostname,
Entries: entries,
}
t.Execute(w, cnt)
}
func main() {
flag.Parse()
// load vars from env
if DbHost == "" {
//DbHost = os.Getenv("DB_PORT_5432_TCP_ADDR")
DbHost = os.Getenv("DB_HOST")
}
if DbPort == 0 {
//port := os.Getenv("DB_PORT_5432_TCP_PORT")
port := os.Getenv("DB_PORT")
if port != "" {
p, err := strconv.Atoi(port)
if err != nil {
log.Fatalf("unable to parse database port: %s", err)
}
DbPort = p
}
}
if DbUser == "" {
DbUser = os.Getenv("DB_USER")
}
if DbPass == "" {
DbPass = os.Getenv("DB_PASS")
}
if DbName == "" {
DbName = os.Getenv("DB_NAME")
}
sslMode := os.Getenv("DB_SSL_MODE")
if sslMode != "" && DbSSLMode == "" {
DbSSLMode = sslMode
}
ds, err := NewDatastore(DbHost, DbPort, DbUser, DbPass, DbName, DbSSLMode)
if err != nil {
log.Fatalf("unable to connect to datastore: %s", err)
}
DS = ds
defer DS.Close()
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
mux.HandleFunc("/", index)
mux.HandleFunc("/db", dbIndex)
log.Println("listening on :8080")
if err := http.ListenAndServe(":8080", mux); err != nil {
fmt.Errorf("error serving: %s", err)
}
}