-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
74 lines (64 loc) · 1.65 KB
/
main.go
File metadata and controls
74 lines (64 loc) · 1.65 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
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
74
package main
import (
"github.com/rluders/httpsuite/v3"
"log"
"net/http"
"strconv"
)
type SampleRequest struct {
ID int `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
}
type SampleResponse struct {
ID int `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
}
func (r *SampleRequest) SetParam(fieldName, value string) error {
switch fieldName {
case "id":
id, err := strconv.Atoi(value)
if err != nil {
return err
}
r.ID = id
}
return nil
}
func StdMuxParamExtractor(r *http.Request, key string) string {
if key != "id" {
return ""
}
// Remove "/submit/" (7 characters) from the URL path to get just the "id"
// Example: /submit/123 -> 123
return r.URL.Path[len("/submit/"):] // Skip the "/submit/" part
}
// You can test it using:
//
// curl -X POST http://localhost:8080/submit/123 \
// -H "Content-Type: application/json" \
// -d '{"name": "John Doe", "age": 30}'
func main() {
// Creating the router using the Go standard mux
mux := http.NewServeMux()
// Define the endpoint POST
mux.HandleFunc("/submit/", func(w http.ResponseWriter, r *http.Request) {
// Using the function for parameter extraction to the ParseRequest
req, err := httpsuite.ParseRequest[*SampleRequest](w, r, StdMuxParamExtractor, nil, "id")
if err != nil {
log.Printf("Error parsing request: %v", err)
return
}
resp := &SampleResponse{
ID: req.ID,
Name: req.Name,
Age: req.Age,
}
// Sending success response
httpsuite.SendResponse[SampleResponse](w, http.StatusOK, *resp, nil, nil)
})
// Starting the server
log.Println("Starting server on :8080")
log.Fatal(http.ListenAndServe(":8080", mux))
}