This repository has been archived by the owner on Jun 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
web.go
82 lines (65 loc) · 2.26 KB
/
web.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
package main
import (
"crypto/subtle"
"encoding/csv"
"github.com/flosch/pongo2"
"github.com/gorilla/mux"
"net/http"
"os"
"path/filepath"
)
func setup_routes() {
username := os.Getenv("HTTP_USERNAME")
password := os.Getenv("HTTP_PASSWORD")
r := mux.NewRouter()
r.NotFoundHandler = http.HandlerFunc(notFound)
r.HandleFunc("/", BasicAuth(homepage, username, password))
r.HandleFunc("/preview/{name}", BasicAuth(preview, username, password))
r.HandleFunc("/status", BasicAuth(status, username, password))
fs := http.FileServer(http.Dir("static"))
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs))
http.Handle("/", r)
}
func BasicAuth(handler http.HandlerFunc, username string, password string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth()
if !ok || subtle.ConstantTimeCompare([]byte(user), []byte(username)) != 1 || subtle.ConstantTimeCompare([]byte(pass), []byte(password)) != 1 {
w.Header().Set("WWW-Authenticate", `Basic realm="Please enter auth details"`)
w.WriteHeader(401)
w.Write([]byte("Unauthorised.\n"))
return
}
handler(w, r)
}
}
var tplIndex = pongo2.Must(pongo2.FromFile("templates/index.html"))
var tplPreview = pongo2.Must(pongo2.FromFile("templates/preview.html"))
var tplStatus = pongo2.Must(pongo2.FromFile("templates/status.html"))
var tpl404 = pongo2.Must(pongo2.FromFile("templates/404.html"))
func notFound(w http.ResponseWriter, r *http.Request) {
tpl404.ExecuteWriter(pongo2.Context{}, w)
}
func homepage(w http.ResponseWriter, r *http.Request) {
tplIndex.ExecuteWriter(pongo2.Context{"adapters": config.Adapters}, w)
}
func status(w http.ResponseWriter, r *http.Request) {
tplStatus.ExecuteWriter(pongo2.Context{}, w)
}
func preview(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
adapter := findAdapter(vars["name"])
// Provide to template for display
files, _ := filepath.Glob(filepath.Join(adapter.Arguments["folder"], "*.csv"))
headers := []string{}
rows := [][]string{}
if len(files) == 1 {
f, _ := os.Open(files[0])
r := csv.NewReader(f)
records, _ := r.ReadAll()
f.Close()
headers = records[0]
rows = records[1:]
}
tplPreview.ExecuteWriter(pongo2.Context{
"adapter": adapter, "headers": headers, "rows": rows}, w)
}