-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove gin framework from httpapi for more control
The Gin framework is good but restricts validation, JSON inputs and middleware control. By going standard library, we will have more validation control, non-JSON input and standard library compatability.
- Loading branch information
Showing
7 changed files
with
482 additions
and
333 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
// Validator is an object that can be validated. | ||
type Validator interface { | ||
Validate() error | ||
} | ||
|
||
// Encode writes the object to the response writer. It is usually used as the | ||
// last step in a handler. | ||
func Encode[T any](w http.ResponseWriter, status int, v T) { | ||
w.Header().Set("Content-Type", "application/json") | ||
// Write to buffer first to ensure the object is json encodable | ||
// before writing to the response writer. | ||
var buf bytes.Buffer | ||
if err := json.NewEncoder(&buf).Encode(v); err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
log.Error().Err(err).Msg("could not encode response") | ||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()}) | ||
return | ||
} | ||
w.WriteHeader(status) | ||
w.Write(buf.Bytes()) | ||
} | ||
|
||
// DecodeValid decodes the request body into the object and then validates it. | ||
// Look at problems to see if there are any issues. | ||
func DecodeValid[T Validator](r *http.Request) (T, error) { | ||
var v T | ||
if r.Header.Get("Content-Type") != "application/json" { | ||
return v, fmt.Errorf("expected content type application/json, got %s", r.Header.Get("Content-Type")) | ||
} | ||
// --------------------------- | ||
if err := json.NewDecoder(r.Body).Decode(&v); err != nil { | ||
return v, fmt.Errorf("decode json: %w", err) | ||
} | ||
if err := v.Validate(); err != nil { | ||
return v, fmt.Errorf("validation error: %w", err) | ||
} | ||
// --------------------------- | ||
return v, nil | ||
} |
Oops, something went wrong.