-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
46 lines (40 loc) · 1.14 KB
/
Copy pathmain.go
File metadata and controls
46 lines (40 loc) · 1.14 KB
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
package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"log"
"net/http"
)
func main() {
r := mux.NewRouter()
intersect := intersectServiceImpl{}
r.HandleFunc("/intersect", cubicHandler(intersect)).Methods("POST")
fmt.Println("Starting server at :8080")
log.Fatal(http.ListenAndServe(":8080", r))
}
// Handler for the POST request
func cubicHandler(service IntersectService) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Parse the JSON request
var cubicRequest CubicRequest
if err := json.NewDecoder(r.Body).Decode(&cubicRequest); err != nil {
http.Error(w, "Invalid request", http.StatusBadRequest)
return
}
// Check for intersection
if service.Intersects(cubicRequest.First, cubicRequest.Second) {
volume := service.IntersectedVolume(cubicRequest.First, cubicRequest.Second)
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(CubicResponse{Success: true, Volume: volume})
if err != nil {
log.Fatal(err)
}
} else {
err := json.NewEncoder(w).Encode(CubicResponse{Success: false})
if err != nil {
log.Fatal(err)
}
}
}
}