-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
62 lines (49 loc) · 1.49 KB
/
main.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
package main
import (
"encoding/json"
"log"
"net/http"
"github.com/gorilla/mux"
)
// Image type containing an ID, and defining JSON encoding
type Image struct {
ID string `json:"id"`
}
var images []Image
// Function that will handle the POST requests made on /endpoint
// It adds an image with input ID in the DataBase
func postFunction(w http.ResponseWriter, r *http.Request) {
// Cast input request to Image
var image Image
_ = json.NewDecoder(r.Body).Decode(&image)
log.Println("Posting Image with ID = ", image.ID, " in my fake database")
// Add it in the Database
images = append(images, image)
}
// Function that will handle the GET requests made on /endpoint
// It looks up the input ID in the database, and returns it if it exists
func getFunction(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
// Cast input request to Image
var image Image
_ = json.NewDecoder(r.Body).Decode(&image)
// Go through images database, and return (encode in JSON) image
var result Image
for _, item := range images {
if item.ID == image.ID {
log.Println("Image with ID = ", image.ID, " found in database")
result = item
break
}
}
json.NewEncoder(w).Encode(result)
}
func main() {
// Initialize router
router := mux.NewRouter()
// Set up router endpoints
router.Methods("POST").Path("/endpoint").HandlerFunc(postFunction)
router.Methods("GET").Path("/endpoint").HandlerFunc(getFunction)
// Listen on port 8080
log.Fatal(http.ListenAndServe(":8080", router))
}