-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.go
73 lines (59 loc) · 1.34 KB
/
routes.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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/google/uuid"
)
func (S *Server) ConfigureRoutes() {
S.Mux.Handle("/start_pool", StartPool(S))
S.Mux.Handle("/add_job", AddJob(S))
S.Mux.Handle("/compare", Compare())
}
func Compare() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.WriteHeader(400)
w.Write([]byte("Only GET Method is allowed\n"))
return
}
Bench()
}
}
func AddJob(S *Server) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(400)
w.Write([]byte("Only POST Method is allowed\n"))
return
}
j := Job{}
j.ID = uuid.New()
raw_data, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(500)
fmt.Println("Error in Reading the data:", err)
w.Write([]byte("Some Error Happened\n"))
return
}
err = json.Unmarshal(raw_data, &j)
if err != nil {
w.WriteHeader(500)
fmt.Println("Error in Unmarshal:", err)
w.Write([]byte("Some Error Happened\n"))
return
}
go S.Pool.AddJob(&j)
}
}
func StartPool(S *Server) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.WriteHeader(400)
w.Write([]byte("Only GET Method is allowed\n"))
return
}
S.Pool.Start()
}
}