|
1 | 1 | # httpsuite |
2 | 2 |
|
3 | | -**httpsuite** is a Go library designed to simplify the handling of HTTP requests, validations, and responses |
4 | | -in microservices. By providing a clear structure and modular approach, it helps developers write |
5 | | -cleaner, more maintainable code with reduced boilerplate. |
| 3 | +httpsuite is a lightweight, idiomatic Go library that simplifies HTTP request parsing, validation, |
| 4 | +and response handling in microservices. It’s designed to reduce boilerplate and promote clean, |
| 5 | +maintainable, and testable code — all while staying framework-agnostic. |
6 | 6 |
|
7 | | -## Features |
| 7 | +## ✨ Features |
8 | 8 |
|
9 | | -- **Request Parsing**: Streamline the parsing of incoming HTTP requests, including URL parameters. |
10 | | -- **Validation:** Centralize validation logic for easy reuse and consistency. |
11 | | -- **Response Handling:** Standardize responses across your microservices for a unified client experience. |
12 | | -- **Modular Design:** Each component (Request, Validation, Response) can be used independently, |
13 | | -enhancing testability and flexibility. |
| 9 | +- 🧾 **Request Parsing**: Automatically extract and map JSON payloads and URL path parameters to Go structs. |
| 10 | +- ✅ **Validation:** Centralized validation using struct tags, integrated with standard libraries like `go-playground/validator`. |
| 11 | +- 📦 **Unified Responses:** Standardize your success and error responses (e.g., [RFC 7807 Problem Details](https://datatracker.ietf.org/doc/html/rfc7807)) for a consistent API experience. |
| 12 | +- 🔌 **Modular Design:** Use each component independently — ideal for custom setups, unit testing, or advanced use cases. |
| 13 | +- 🧪 **Test-Friendly:** Decouple parsing and validation logic for simpler, more focused test cases. |
14 | 14 |
|
15 | | -### Supported routers |
| 15 | +### 🔌 Supported routers |
16 | 16 |
|
17 | | -- Gorilla MUX |
18 | | -- Chi |
19 | | -- Go Standard |
20 | | -- ...maybe more? Submit a PR with an example. |
| 17 | +- [Chi](https://github.com/go-chi/chi) |
| 18 | +- [Gorilla MUX](https://github.com/gorilla/mux) |
| 19 | +- Go standard `http.ServeMux` |
| 20 | +- ...and potentially more — [Submit a PR with an example!](https://github.com/rluders/httpsuite) |
21 | 21 |
|
22 | | -## Installation |
| 22 | +## 🛠 Installation |
23 | 23 |
|
24 | 24 | To install **httpsuite**, run: |
25 | 25 |
|
26 | 26 | ``` |
27 | 27 | go get github.com/rluders/httpsuite/v2 |
28 | 28 | ``` |
29 | 29 |
|
30 | | -## Usage |
| 30 | +## 🚀 Usage |
31 | 31 |
|
32 | | -### Request Parsing with URL Parameters |
| 32 | +```go |
| 33 | +import ( |
| 34 | + "github.com/go-chi/chi/v5" |
| 35 | + "github.com/rluders/httpsuite/v2" |
| 36 | + "net/http" |
| 37 | +) |
33 | 38 |
|
34 | | -Check out the [example folder for a complete project](./examples) demonstrating how to integrate **httpsuite** into |
35 | | -your Go microservices. |
| 39 | +type SampleRequest struct { |
| 40 | + ID int `json:"id" validate:"required"` |
| 41 | + Name string `json:"name" validate:"required,min=3"` |
| 42 | +} |
36 | 43 |
|
37 | | -## Contributing |
| 44 | +func (r *SampleRequest) SetParam(fieldName, value string) error { |
| 45 | + if fieldName == "id" { |
| 46 | + id, err := strconv.Atoi(value) |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + r.ID = id |
| 51 | + } |
| 52 | + return nil |
| 53 | +} |
38 | 54 |
|
39 | | -Contributions are welcome! Feel free to open issues, submit pull requests, and help improve **httpsuite**. |
| 55 | +func main() { |
| 56 | + r := chi.NewRouter() |
40 | 57 |
|
41 | | -## License |
| 58 | + r.Post("/submit/{id}", func(w http.ResponseWriter, r *http.Request) { |
| 59 | + req, err := httpsuite.ParseRequest[*SampleRequest](w, r, chi.URLParam, "id") |
| 60 | + if err != nil { |
| 61 | + return // ProblemDetails already sent |
| 62 | + } |
| 63 | + |
| 64 | + httpsuite.SendResponse(w, http.StatusOK, req, nil, nil) |
| 65 | + }) |
| 66 | + |
| 67 | + http.ListenAndServe(":8080", r) |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +💡 Try it: |
| 72 | + |
| 73 | +``` |
| 74 | +curl -X POST http://localhost:8080/submit/123 \ |
| 75 | + -H "Content-Type: application/json" \ |
| 76 | + -d '{"name":"John"}' |
| 77 | +``` |
| 78 | + |
| 79 | +## 📂 Examples |
| 80 | + |
| 81 | +Check out the `examples/` folder for a complete working project demonstrating: |
| 82 | + |
| 83 | +- Full request lifecycle |
| 84 | +- Param parsing |
| 85 | +- Validation |
| 86 | +- ProblemDetails usage |
| 87 | +- JSON response formatting |
| 88 | + |
| 89 | +## 📖 Tutorial & Article |
| 90 | + |
| 91 | +- [Improving Request Validation and Response Handling in Go Microservices](https://medium.com/@rluders/improving-request-validation-and-response-handling-in-go-microservices-cc54208123f2) |
| 92 | + |
| 93 | +## 🤝 Contributing |
| 94 | + |
| 95 | +All contributions are welcome! Whether it's a bug fix, feature proposal, or router integration example: |
| 96 | + |
| 97 | +- Open an issue |
| 98 | +- Submit a PR |
| 99 | +- Join the discussion! |
| 100 | + |
| 101 | +## 🪪 License |
42 | 102 |
|
43 | 103 | The MIT License (MIT). Please see [License File](LICENSE) for more information. |
0 commit comments