-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
39 lines (33 loc) · 1.12 KB
/
main.go
File metadata and controls
39 lines (33 loc) · 1.12 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
package main
import(
"acme/api"
"fmt"
"net/http"
)
func rootHandler(writer http.ResponseWriter, request *http.Request) {
fmt.Fprintf(writer, "Hello, World!")
}
func CorsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Access-Control-Allow-Origin", "*")
// Continue with the next handler
next.ServeHTTP(writer, request)
})
}
func main() {
// Use mutliplexer to allow different methods for same PATH but different
// method VERB.
router := http.NewServeMux()
router.HandleFunc("GET /", rootHandler)
router.HandleFunc("GET /api/users/{id}", api.GetSingleUser)
router.HandleFunc("GET /api/users", api.GetUsers)
router.HandleFunc("DELETE /api/users/{id}", api.DeleteUser)
router.HandleFunc("PUT /api/users/{id}", api.UpdateUser)
router.HandleFunc("POST /api/users", api.CreateUser)
// Starting the HTTP server on port 8080
fmt.Println("Server listening on port 8080...")
err := http.ListenAndServe(":8080", CorsMiddleware(router))
if err != nil {
fmt.Println("Error starting server:", err)
}
}