Skip to content

Commit 00901c9

Browse files
docs: update sample document
1 parent 215da6a commit 00901c9

36 files changed

+970
-254
lines changed

README.md

+13-3
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,13 @@
139139
## 📦 Installation
140140

141141
```go
142-
go get github.com/thuongtruong109/gouse
142+
go get -u github.com/thuongtruong109/gouse
143+
```
144+
145+
or add to your `go.mod` file:
146+
147+
```go
148+
required github.com/thuongtruong109/gouse
143149
```
144150

145151
## 🕯️ Quick Start
@@ -186,7 +192,7 @@ func main() {
186192

187193
`INPUT`: 1000000
188194

189-
<!-- ## 📚 Examples -->
195+
# ![Benchmark](/public/benchmark.png)
190196

191197
## 📁 Project Structure
192198

@@ -202,7 +208,11 @@ $ git clone https://github.com/thuongtruong109/gouse.git
202208

203209
## 📝 Contributing
204210

205-
- We welcome your contributions! If you're looking for issues to work on, try looking at the good first issue list. We do our best to tag issues suitable for new external contributors with that label, so it's a great way to find something you can help with!
211+
Gouse is an open source project and built on the top of other open-source projects. Welcome all the kinds of contributions. No matter it is for typo fix, bug fix or big new features. Please do not hesitate to ask a question or send a pull request.
212+
213+
We strongly value documentation and integration with other projects, so we are very glad to accept improvements for these aspects.
214+
215+
If you're looking for issues to work on, try looking at the good first issue list. We do our best to tag issues suitable for new external contributors with that label, so it's a great way to find something you can help with!
206216

207217
- Please read our [`Code of Conduct`](.github/CODE_OF_CONDUCT.md) before contributing.
208218

api.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ type IHeader struct {
137137
}
138138

139139
func Header(w http.ResponseWriter, r *http.Request) {
140-
w.Header().Set("Content-Type", "application/json")
140+
setJSONHeader(w)
141141

142142
for k, v := range r.Header {
143143
fmt.Fprintf(w, "%q: %q\n\n", k, v)
@@ -153,6 +153,7 @@ func Header(w http.ResponseWriter, r *http.Request) {
153153
ContentLength: 200,
154154
ContentType: "application/json",
155155
}
156+
w.WriteHeader(200)
156157
json.NewEncoder(w).Encode(p)
157158
}
158159

docs/docs/introduction/about.md

+16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
![Project Structure](https://raw.githubusercontent.com/thuongtruong109/gouse/diagram/project-structure.svg)
66

7+
## 📊 Benchmark
8+
9+
![Benchmark](https://github.com/thuongtruong109/gouse/blob/main/public/benchmark.png)
10+
711
## 📜 Changelog
812

913
- Gouse is under active development. This means that new features, bug fixes, and breaking changes will be released frequently. We encourage you to keep the [`CHANGELOG`](https://github.com/thuongtruong109/gouse/blob/main/CHANGELOG.md) open while upgrading to see what's new!
@@ -36,3 +40,15 @@
3640
- [Cache for Go](github.com/patrickmn/go-cache) - An in-memory key:value store/cache (similar to Memcached) library for Go 🗄
3741
- [Toml Go](github.com/pelletier/go-toml) - A Go library for managing configuration data from files 📄
3842
- [Yaml](gopkg.in/yaml.v2) - A YAML support for the Go language 📄
43+
44+
## 🧑‍🤝‍🧑 Contributing
45+
46+
Gouse is an open source project and built on the top of other open-source projects. Welcome all the kinds of contributions. No matter it is for typo fix, bug fix or big new features. Please do not hesitate to ask a question or send a pull request.
47+
48+
We strongly value documentation and integration with other projects, so we are very glad to accept improvements for these aspects.
49+
50+
If you're looking for issues to work on, try looking at the good first issue list. We do our best to tag issues suitable for new external contributors with that label, so it's a great way to find something you can help with!
51+
52+
- Please read our [`Code of Conduct`](https://github.com/thuongtruong109/gouse/blob/main/.github/CODE_OF_CONDUCT.md) before contributing.
53+
54+
- Refer to the [`Contributing Guide`](https://github.com/thuongtruong109/gouse/blob/main/.github/CONTRIBUTING.md) for more information on how to get started.

docs/docs/introduction/getting-started.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
go get github.com/thuongtruong109/gouse
99
```
1010

11+
or add to your `go.mod` file:
12+
13+
```go
14+
required github.com/thuongtruong109/gouse
15+
```
16+
1117
## 🕯️ Quick Start
1218

1319
```go
@@ -38,4 +44,4 @@ func main() {
3844
}
3945
```
4046

41-
- Please refer to the [`receipts`](/receipts/array) for more examples.
47+
- Please refer to the [`receipts`](/receipts/array) for more examples.

docs/docs/receipts/api.md

+75-45
Original file line numberDiff line numberDiff line change
@@ -4,74 +4,104 @@
44

55
```go
66
import (
7+
"context"
8+
"fmt"
79
"net/http"
8-
"time"
910
"github.com/thuongtruong109/gouse"
1011
)
1112
```
1213

13-
## 1. Loadbalancer
14+
## 1. Validate
1415

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>
1617

1718
```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"`
3523
}
3624

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+
}
3944
}
4045
```
4146

42-
## 2. Server
47+
## 2. Upload file
4348

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>
4550

4651
```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+
})
5559

56-
gs.Server()
60+
http.ListenAndServe(":8080", nil)
5761
}
5862
```
5963

60-
## 3. Upload file
64+
## 3. Header
6165

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>
6367

6468
```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)
7575
}
7676
}
7777
```
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+
```

docs/docs/receipts/color.md

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
2+
# <Badge style='font-size: 1.8rem; text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.3); padding: 0.35rem 0.75rem 0.35rem 0;' type='info' text='🔖 Color' />
3+
4+
5+
```go
6+
import (
7+
"fmt"
8+
"github.com/thuongtruong109/gouse"
9+
)
10+
```
11+
12+
## 1. Color hue to rgb
13+
14+
Description: Convert color from Hue to RGB format<br>Parameters: (p, q, t float64) float64 -> float64<br>
15+
16+
```go
17+
func ColorHueToRgb() {
18+
p := 0.2
19+
q := 0.8
20+
t := 120.0
21+
22+
rgb := gouse.Hue2Rgb(p, q, t)
23+
24+
fmt.Printf("RGB value for Hue2Rgb(%f, %f, %f) = %f\n", p, q, t, rgb)
25+
}
26+
```
27+
28+
## 2. Color hlsa to rgba
29+
30+
Description: Convert color from HLSA to RGBA format<br>Parameters: (h, l, s, a float64) color.RGBA -> color.RGBA<br>
31+
32+
```go
33+
func ColorHlsaToRgba() {
34+
h := 120.0 // Hue: 120° (green)
35+
l := 0.5 // Lightness: 50%
36+
s := 0.75 // Saturation: 75%
37+
a := 1.0 // Alpha: fully opaque (100%)
38+
39+
rgba := gouse.Hlsa2Rgba(h, l, s, a)
40+
41+
fmt.Printf("HLSA(%f, %f, %f, %f) = RGBA(%d, %d, %d, %d)\n", h, l, s, a, rgba.R, rgba.G, rgba.B, rgba.A)
42+
}
43+
```
44+
45+
## 3. Color rgba to hlsa
46+
47+
Description: Convert color from RGBA to HLSA format<br>Parameters: (r, g, b, a uint8) -> (float64, float64, float64, float64)<br>
48+
49+
```go
50+
func ColorRgbaToHlsa() {
51+
r, g, b, a := uint8(255), uint8(0), uint8(0), uint8(128) // Red with 50% opacity
52+
53+
h, s, l, af := gouse.Rgba2Hlsa(r, g, b, a)
54+
55+
fmt.Printf("RGBA(%d, %d, %d, %d) => HLSA: %.2f, %.2f, %.2f, %.2f\n", r, g, b, a, h, s, l, af)
56+
}
57+
```
58+
59+
## 4. Color hex to hlsa
60+
61+
Description: Convert color from Hex to HLSA format<br>Parameters: (hex string) -> (float64, float64, float64, float64, error)<br>
62+
63+
```go
64+
func ColorHexToHlsa() {
65+
hex := "#FF5733CC" // RGBA: Red=255, Green=87, Blue=51, Alpha=204 (opaque)
66+
67+
h, s, l, a, err := gouse.Hex2Hlsa(hex)
68+
if err != nil {
69+
fmt.Println("Error:", err)
70+
return
71+
}
72+
73+
fmt.Printf("Hex: %s => HLSA: %.2f, %.2f, %.2f, %.2f\n", hex, h, s, l, a)
74+
}
75+
```
76+
77+
## 5. Color hex to rgba
78+
79+
Description: Convert color from Hex to RGBA format<br>Parameters: (hex string) -> (color.RGBA, error)<br>
80+
81+
```go
82+
func ColorHexToRgba() {
83+
hex := "#FF5733" // RGB: Red=255, Green=87, Blue=51
84+
85+
rgba, err := gouse.Hex2Rgba(hex)
86+
if err != nil {
87+
fmt.Println("Error:", err)
88+
return
89+
}
90+
91+
fmt.Printf("Hex: %s => RGBA: %d, %d, %d, %d\n", hex, rgba.R, rgba.G, rgba.B, rgba.A)
92+
}
93+
```

docs/docs/receipts/context.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
# <Badge style='font-size: 1.8rem; text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.3); padding: 0.35rem 0.75rem 0.35rem 0;' type='info' text='🔖 Context' />
3+
4+
5+
```go
6+
import (
7+
"fmt"
8+
"time"
9+
"github.com/thuongtruong109/gouse"
10+
)
11+
```
12+
13+
## 1. Context
14+
15+
Description: Management of context and cancellation in Go.<br>
16+
17+
```go
18+
func Context() {
19+
ctx, cancel := gouse.CtxCancel()
20+
gouse.SetCtx(ctx)
21+
22+
go func() {
23+
for {
24+
select {
25+
case <-ctx.Done():
26+
fmt.Println("Process cancelled.")
27+
return
28+
default:
29+
// Simulate some work
30+
fmt.Println("Working...")
31+
time.Sleep(1 * time.Second)
32+
}
33+
}
34+
}()
35+
36+
// Simulating program running and waiting for termination signal
37+
time.Sleep(5 * time.Second)
38+
39+
// After 5 seconds, trigger cancellation
40+
cancel()
41+
42+
// Wait for the program to exit
43+
time.Sleep(2 * time.Second)
44+
45+
// Retrieve the context and print if it's cancelled
46+
fmt.Println("Is context cancelled?", gouse.GetCtx().Done() != nil)
47+
}
48+
```

0 commit comments

Comments
 (0)