-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
35 lines (28 loc) · 746 Bytes
/
main.go
File metadata and controls
35 lines (28 loc) · 746 Bytes
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
package main
import (
"fmt"
"net/http"
"sync"
)
func main() {
// To handle concurrent requests, each request to the api should run on its own goroutine
// That's what I am doing here
http.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
var wg sync.WaitGroup = sync.WaitGroup{}
wg.Add(1)
switch r.Method {
case http.MethodGet:
go handleGet(w, r, &wg)
case http.MethodPost:
go handlePost(w, r, &wg)
case http.MethodDelete:
go handleDelete(w, r, &wg)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprintf(w, "Method %s not allowed\n", r.Method)
}
wg.Wait() // wait for go routines to finish
})
fmt.Println("Server listening on :8080")
http.ListenAndServe(":8080", nil)
}