From c856c7e66e3ad29ea0491f1f4951b31deb8279ea Mon Sep 17 00:00:00 2001 From: Emile Date: Sun, 20 Oct 2019 16:53:28 +0200 Subject: functional --- src/http.go | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/http.go (limited to 'src/http.go') diff --git a/src/http.go b/src/http.go new file mode 100644 index 0000000..248eea4 --- /dev/null +++ b/src/http.go @@ -0,0 +1,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)) + } +} -- cgit 1.4.1