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
|
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"text/template"
"github.com/gorilla/mux"
)
var (
port *int // port the http server listens on
usernames []string // list of usernames
)
// Credentials stores user credentials
type Credentials struct {
Username string
Accesscode string
Hostname string
}
func registerHTTPFlags() {
port = flag.Int("port", 8081, "The port the http server should listen on")
}
func setupHTTPServer() http.Server {
r := mux.NewRouter()
r.HandleFunc("/", indexHandler)
return http.Server{
Addr: fmt.Sprintf("0.0.0.0:%d", *port),
Handler: r,
}
}
// Host of the index file
func indexHandler(w http.ResponseWriter, r *http.Request) {
t := template.New("")
t, err := t.ParseFiles("./hosted/index.html")
if err != nil {
log.Println(err)
return
}
content := Content{
Hostname: os.Getenv("HOSTNAME"),
}
t.ExecuteTemplate(w, "index", content)
}
func readFileToReponse(w http.ResponseWriter, path string) {
requestedFile := strings.Replace(path, "..", "", -1)
contents, readError := ioutil.ReadFile(fmt.Sprintf("hosted/%s", requestedFile))
if readError != nil {
w.Write([]byte(fmt.Sprintf("unable to read %s", requestedFile)))
} else {
w.Write([]byte(contents))
}
}
|