-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.go
118 lines (103 loc) · 3.05 KB
/
app.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
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
112
113
114
115
116
117
118
package main
import "net/http"
import "log"
import "github.com/gorilla/mux"
import "html/template"
import "time"
// Random ID creation.
import (
"math/rand"
"strconv"
)
// Number of 1-4 digit base-36 numbers.
const MAX_IDS = 1727604
var templates = template.Must(template.ParseFiles(
"html/base.html",
"html/index.html",
"html/create.html",
"html/server.html",
"html/client.html"))
func init() {
rand.Seed(time.Now().UnixNano())
}
// IndexData contains the data to pass to the index.html template.
type IndexData struct {
Id string
}
// IndexController displays the home page.
func IndexController(writer http.ResponseWriter, request *http.Request) {
idNum := rand.Intn(MAX_IDS)
id := strconv.FormatInt(int64(idNum), 36)
err := templates.ExecuteTemplate(writer, "index.html", IndexData{id})
if err != nil {
log.Fatal("Failed to execute template: ", err)
}
}
// JobCreationData contains the data to pass to the create.html template.
type JobCreationData struct {
Id string
}
// JobCreationController displays the job creation page.
func JobCreationController(writer http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
id := vars["id"]
err := templates.ExecuteTemplate(writer, "create.html", JobCreationData{id})
if err != nil {
log.Fatal("Failed to execute template: ", err)
}
}
// JobServerData contains the data to pass to the server.html template.
type JobServerData struct {
Id string
MapperCode string
ReducerCode string
NumReducers string
DataFiles string
}
// JobServerController displays the job server page.
func JobServerController(writer http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
id := vars["id"]
mapper := request.FormValue("mapper")
reducer := request.FormValue("reducer")
numReducers := request.FormValue("numreducers")
dataFiles := request.FormValue("datafiles")
data := JobServerData{
Id: id,
MapperCode: mapper,
ReducerCode: reducer,
NumReducers: numReducers,
DataFiles: dataFiles}
err := templates.ExecuteTemplate(writer, "server.html", data)
if err != nil {
log.Fatal("Failed to execute template: ", err)
}
}
// ClientData contains the data to pass to the client.html template.
type ClientData struct {
Id string
}
// ClientController displays the job creation page.
func ClientController(writer http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
id := vars["id"]
err := templates.ExecuteTemplate(writer, "client.html", ClientData{id})
if err != nil {
log.Fatal("Failed to execute template: ", err)
}
}
// main sets up the URL routes and launches the HTTP server.
func main() {
router := mux.NewRouter()
router.HandleFunc("/", IndexController)
router.HandleFunc("/create/{id}", JobCreationController)
router.HandleFunc("/server/{id}", JobServerController)
router.HandleFunc("/job/{id}", ClientController)
router.Handle("/js/{rest}", http.StripPrefix("/js/",
http.FileServer(http.Dir("js/"))))
http.Handle("/", router)
err := http.ListenAndServe(":5500", nil)
if err != nil {
log.Fatal("ListenAndServe failed: ", err)
}
}