-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
76 lines (65 loc) · 1.41 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"fmt"
"github.com/manuelarte/milogo"
"github.com/manuelarte/milogo/pkg"
"net/http"
"net/http/httptest"
"os"
"time"
"github.com/gin-gonic/gin"
)
type Address struct {
Street string `json:"street"`
Number string `json:"number"`
ZipCode string `json:"zipcode"`
}
type User struct {
Name string `json:"name"`
Surname string `json:"surname"`
Age int `json:"age"`
Address Address `json:"address"`
}
type RestResponse[T any] struct {
Data T `json:"data"`
Metadata any `json:"_metadata"`
}
func setupRouter() *gin.Engine {
r := gin.Default()
wrapFieldOption, _ := pkg.WithWrapField("data")
r.Use(milogo.Milogo(wrapFieldOption))
// Get user value
r.GET("/users/:name", func(c *gin.Context) {
name := c.Params.ByName("name")
user := User{
Name: name,
Surname: "Example",
Age: 1,
Address: Address{
Street: "mystreet",
Number: "mynumber",
ZipCode: "myzipcode",
},
}
c.IndentedJSON(http.StatusOK, RestResponse[User]{
Data: user,
Metadata: map[string]string{
"_link": "/users/" + name,
},
})
})
return r
}
func main() {
r := setupRouter()
go func() {
time.Sleep(time.Second)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/users/manuel?fields=name,surname,address", nil)
r.ServeHTTP(w, req)
fmt.Printf(w.Body.String())
os.Exit(1)
}()
// Listen and Server in 0.0.0.0:8080
_ = r.Run(":8080")
}