|
4 | 4 |
|
5 | 5 | ```go
|
6 | 6 | import (
|
| 7 | + "context" |
| 8 | + "fmt" |
7 | 9 | "net/http"
|
8 |
| - "time" |
9 | 10 | "github.com/thuongtruong109/gouse"
|
10 | 11 | )
|
11 | 12 | ```
|
12 | 13 |
|
13 |
| -## 1. Loadbalancer |
| 14 | +## 1. Validate |
14 | 15 |
|
15 |
| -Description: Load balancer with health check<br>Input params: (ILbConf.ProxyPort, ILbConf.Backends)<br> |
| 16 | +Description: Validate request data<br>Input params: (ctxBind func() error, ctxReq func() context.Context, req any)<br> |
16 | 17 |
|
17 | 18 | ```go
|
18 |
| -func Loadbalancer() { |
19 |
| - lbCfg := gouse.ILbConf{ |
20 |
| - ProxyPort: "8080", |
21 |
| - Backends: []gouse.ILb{ |
22 |
| - { |
23 |
| - URL: "http://localhost:8081", |
24 |
| - IsDead: false, |
25 |
| - }, |
26 |
| - { |
27 |
| - URL: "http://localhost:8082", |
28 |
| - IsDead: false, |
29 |
| - }, |
30 |
| - { |
31 |
| - URL: "http://localhost:8083", |
32 |
| - IsDead: false, |
33 |
| - }, |
34 |
| - }, |
| 19 | +func Validate() { |
| 20 | + type UserRequest struct { |
| 21 | + Name string `json:"name" validate:"required"` |
| 22 | + Email string `json:"email" validate:"required,email"` |
35 | 23 | }
|
36 | 24 |
|
37 |
| - // gouse.HealthCheck() // Enable health check (optional) |
38 |
| - gouse.LoadBalancer(lbCfg.ProxyPort, lbCfg.Backends) |
| 25 | + req := UserRequest{ |
| 26 | + Name: "John", |
| 27 | + Email: "invalid-email", |
| 28 | + } |
| 29 | + |
| 30 | + ctxBind := func() error { |
| 31 | + return nil |
| 32 | + } |
| 33 | + |
| 34 | + ctxReq := func() context.Context { |
| 35 | + return context.Background() |
| 36 | + } |
| 37 | + |
| 38 | + err := gouse.Validate(ctxBind, ctxReq, req) |
| 39 | + if err != nil { |
| 40 | + gouse.Println("Validation failed:", err) |
| 41 | + } else { |
| 42 | + gouse.Println("Validation succeeded!") |
| 43 | + } |
39 | 44 | }
|
40 | 45 | ```
|
41 | 46 |
|
42 |
| -## 2. Server |
| 47 | +## 2. Upload file |
43 | 48 |
|
44 |
| -Description: Start server with graceful shutdown mode for API<br>Input params: (gouse.IServer)<br> |
| 49 | +Description: Upload file to server<br>Input params: (servePath, storagePath string)<br> |
45 | 50 |
|
46 | 51 | ```go
|
47 |
| -func Server() { |
48 |
| - gs := gouse.IServer{ |
49 |
| - Port: "3000", |
50 |
| - StartMsg: "Starting server at port http://localhost:3000", |
51 |
| - ShutdownMsg: "Shutting down server...", |
52 |
| - SleepTimeout: 5 * time.Second, |
53 |
| - HeaderTimeout: 5 * time.Second, |
54 |
| - } |
| 52 | +func UploadFile() { |
| 53 | + http.HandleFunc("/upload-single", func(w http.ResponseWriter, r *http.Request) { |
| 54 | + gouse.UploadSingle(w, r, "http://localhost:8080/img", "./public") |
| 55 | + }) |
| 56 | + http.HandleFunc("/upload-multi", func(w http.ResponseWriter, r *http.Request) { |
| 57 | + gouse.UploadMulti(w, r, "http://localhost:8080/img", "./public") |
| 58 | + }) |
55 | 59 |
|
56 |
| - gs.Server() |
| 60 | + http.ListenAndServe(":8080", nil) |
57 | 61 | }
|
58 | 62 | ```
|
59 | 63 |
|
60 |
| -## 3. Upload file |
| 64 | +## 3. Header |
61 | 65 |
|
62 |
| -Description: Upload file to server<br>Input params: (servePath, storagePath string)<br> |
| 66 | +Description: Return the header of the request<br>Input params: (w http.ResponseWriter, r *http.Request)<br> |
63 | 67 |
|
64 | 68 | ```go
|
65 |
| -func UploadFile() { |
66 |
| - { |
67 |
| - http.HandleFunc("/upload-single", func(w http.ResponseWriter, r *http.Request) { |
68 |
| - gouse.UploadSingle(w, r, "http://localhost:8080/img", "./public") |
69 |
| - }) |
70 |
| - http.HandleFunc("/upload-multi", func(w http.ResponseWriter, r *http.Request) { |
71 |
| - gouse.UploadMulti(w, r, "http://localhost:8080/img", "./public") |
72 |
| - }) |
73 |
| - |
74 |
| - http.ListenAndServe(":8080", nil) |
| 69 | +func Header() { |
| 70 | + http.HandleFunc("/header", gouse.Header) |
| 71 | + |
| 72 | + fmt.Println("Server is running on http://localhost:8080") |
| 73 | + if err := http.ListenAndServe(":8080", nil); err != nil { |
| 74 | + fmt.Println("Server failed to start:", err) |
75 | 75 | }
|
76 | 76 | }
|
77 | 77 | ```
|
| 78 | + |
| 79 | +## 4. Pagination |
| 80 | + |
| 81 | +Description: Setup pagination<br>Input params: (page, size int)<br> |
| 82 | + |
| 83 | +```go |
| 84 | +func Pagination() { |
| 85 | + pagination := gouse.NewPagination(10, 1) |
| 86 | + |
| 87 | + pagination.SetPage("2") |
| 88 | + pagination.SetSize("15") |
| 89 | + pagination.SetOrderBy("desc") |
| 90 | + |
| 91 | + pagination.Total = 100 |
| 92 | + |
| 93 | + fmt.Println("Pagination Info:") |
| 94 | + fmt.Println("Page:", pagination.GetPage()) |
| 95 | + fmt.Println("Size per Page:", pagination.GetSize()) |
| 96 | + fmt.Println("Order By:", pagination.GetOrderBy()) |
| 97 | + fmt.Println("Total Pages:", pagination.GetTotalPages()) |
| 98 | + fmt.Println("Next Page:", pagination.GetNextPage()) |
| 99 | + fmt.Println("Prev Page:", pagination.GetPrevPage()) |
| 100 | + fmt.Println("Has More Pages:", pagination.GetHasMore(pagination.Total)) |
| 101 | + fmt.Println("Offset:", pagination.GetOffset()) |
| 102 | + fmt.Println("First Page:", pagination.GetFirstPage()) |
| 103 | + fmt.Println("Last Page:", pagination.GetLastPage()) |
| 104 | + fmt.Println("Total Items:", pagination.Total) |
| 105 | + fmt.Println("Query String:", pagination.GetQueryString()) |
| 106 | +} |
| 107 | +``` |
0 commit comments