about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorEmile <hanemile@protonmail.com>2019-10-18 23:37:06 +0200
committerEmile <hanemile@protonmail.com>2019-10-18 23:37:06 +0200
commit55acb46da2450116834ef2357d01925fdcee09a3 (patch)
treeae919a2e57f8432a33e995edfd106afeb4e3a13f /src
parent57b708418beede17ac18f20004eb3d47bebd41c0 (diff)
added some endpoints
Diffstat (limited to 'src')
-rw-r--r--src/http.go45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/http.go b/src/http.go
index a813ae6..6ba657a 100644
--- a/src/http.go
+++ b/src/http.go
@@ -3,7 +3,10 @@ package main
 import (
 	"flag"
 	"fmt"
+	"io/ioutil"
+	"log"
 	"net/http"
+	"strings"
 
 	"github.com/gorilla/mux"
 )
@@ -20,6 +23,11 @@ func setupHTTPServer() http.Server {
 	r := mux.NewRouter()
 
 	r.HandleFunc("/", indexHandler)
+	r.HandleFunc("/create", createGetHandler).Methods("GET")
+	r.HandleFunc("/create", createPostHandler).Methods("POST")
+	r.HandleFunc("/view", viewGetHandler).Methods("GET")
+	r.HandleFunc("/edit", editGetHandler).Methods("GET")
+	r.HandleFunc("/edit", editPostHandler).Methods("POST")
 
 	return http.Server{
 		Addr:    fmt.Sprintf("0.0.0.0:%d", *port),
@@ -31,3 +39,40 @@ func setupHTTPServer() http.Server {
 func indexHandler(w http.ResponseWriter, r *http.Request) {
 	fmt.Fprintf(w, "%s", "Hello World!\n")
 }
+
+func createGetHandler(w http.ResponseWriter, r *http.Request) {
+	log.Println("create GET")
+	readFileToResponse(w, "/create.html")
+}
+
+func createPostHandler(w http.ResponseWriter, r *http.Request) {
+	log.Println("create POST")
+}
+
+func viewGetHandler(w http.ResponseWriter, r *http.Request) {
+	log.Println("ciew GET")
+	readFileToResponse(w, "/view.html")
+}
+
+func editGetHandler(w http.ResponseWriter, r *http.Request) {
+	log.Println("edit GET")
+	readFileToResponse(w, "/edit.html")
+}
+
+func editPostHandler(w http.ResponseWriter, r *http.Request) {
+	log.Println("edit POST")
+
+}
+
+// Helper function to host files off of "hosted/" directory
+func readFileToResponse(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))
+	}
+}