about summary refs log tree commit diff
path: root/src/http.go
blob: 88326fc89f075f3a333f58872e68a3343f513077 (plain)
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
package main

import (
	"flag"
	"fmt"
	"html/template"
	"io/ioutil"
	"log"
	"net/http"
	"strings"

	"github.com/gorilla/mux"
)

var (
	port *int
)

func registerHTTPFlags() {
	port = flag.Int("port", 8080, "The port for HTTP")
}

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),
		Handler: r,
	}
}

// Host the index file
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) {
	// get all challenges from the db
	challs := getAllChallenges(db)

	log.Println("got all challenges")

	// define a challenges struct storing the challenges.
	// This struct can be used in a template
	challenges := Challenges{}

	for _, chal := range challs {
		challenges.Challenge = append(challenges.Challenge, chal)
	}

	log.Println("Done inserting all the challengesi nto the challenges struct")

	fmt.Printf("challenges: %#v\n", challenges)

	log.Println("Generating a new template used to render the challenges")

	t := template.New("")
	t, err := t.ParseFiles("./hosted/view.html")
	if err != nil {
		log.Println(err)
		return
	}

	log.Println("Done generating and parsing the template")

	t.ExecuteTemplate(w, "view", challenges)

	log.Println("Done executing the template")

	// read the view.html file and write its content to the response writer
	//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))
	}
}