2026-02-11 16:56:17: merge [main:afeadb1d9a241da30507f3683781558dbccfefc6];
Create and publish a Docker image / build-and-push-image (push) Canceled after 0s
Create and publish a Docker image / build-and-push-image (push) Canceled after 0s
This commit is contained in:
1 parent
aecc42c6d8
commit
d4091f8805
25 files changed
+1664
-92
No files matched your search
@@ -0,0 +1,6 @@
|
||||
package domain
|
||||
|
||||
const (
|
||||
ApiPrefix = "/api"
|
||||
GrpcApiPrefix = "api/"
|
||||
)
|
||||
@@ -0,0 +1,41 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/txix-open/isp-kit/json"
|
||||
)
|
||||
|
||||
type HttpError struct {
|
||||
statusCode int
|
||||
userMessage string
|
||||
details []any
|
||||
err error
|
||||
}
|
||||
|
||||
func NewHttpError(statusCode int, userMessage string, internalError error) *HttpError {
|
||||
return &HttpError{
|
||||
statusCode: statusCode,
|
||||
userMessage: userMessage,
|
||||
err: internalError,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *HttpError) Error() string {
|
||||
return e.err.Error()
|
||||
}
|
||||
|
||||
func (e *HttpError) WriteError(w http.ResponseWriter) error {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(e.statusCode)
|
||||
data := map[string]any{
|
||||
"errorCode": http.StatusText(e.statusCode),
|
||||
"errorMessage": e.userMessage,
|
||||
"details": e.details,
|
||||
}
|
||||
return json.NewEncoder(w).Encode(data) // nolint:wrapcheck
|
||||
}
|
||||
|
||||
func (e *HttpError) WithDetails(details ...any) {
|
||||
e.details = details
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type RequestContext struct {
|
||||
request *http.Request
|
||||
responseWriter http.ResponseWriter
|
||||
|
||||
endpoint string
|
||||
|
||||
queryParams map[string]string
|
||||
}
|
||||
|
||||
func NewContext(request *http.Request, response http.ResponseWriter, endpoint string) *RequestContext {
|
||||
return &RequestContext{
|
||||
request: request,
|
||||
responseWriter: response,
|
||||
endpoint: endpoint,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *RequestContext) Request() *http.Request {
|
||||
return c.request
|
||||
}
|
||||
|
||||
func (c *RequestContext) ResponseWriter() http.ResponseWriter {
|
||||
return c.responseWriter
|
||||
}
|
||||
|
||||
func (c *RequestContext) SetResponseWriter(writer http.ResponseWriter) {
|
||||
c.responseWriter = writer
|
||||
}
|
||||
|
||||
func (c *RequestContext) Endpoint() string {
|
||||
return c.endpoint
|
||||
}
|
||||
|
||||
func (c *RequestContext) Context() context.Context {
|
||||
return c.request.Context()
|
||||
}
|
||||
|
||||
func (c *RequestContext) SetContext(ctx context.Context) {
|
||||
c.request = c.request.WithContext(ctx)
|
||||
}
|
||||
|
||||
func (c *RequestContext) Param(name string) string {
|
||||
value := c.request.Header.Get(name)
|
||||
if value != "" {
|
||||
return strings.TrimSpace(value)
|
||||
}
|
||||
|
||||
if c.queryParams == nil {
|
||||
query := c.request.URL.Query()
|
||||
c.queryParams = map[string]string{}
|
||||
for key, values := range query {
|
||||
if len(values) == 0 {
|
||||
continue
|
||||
}
|
||||
c.queryParams[strings.ToLower(key)] = values[0]
|
||||
}
|
||||
}
|
||||
value = c.queryParams[strings.ToLower(name)]
|
||||
|
||||
return strings.TrimSpace(value)
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package domain
|
||||
|
||||
type RouteKey struct {
|
||||
Method string
|
||||
CleanPath string
|
||||
WithApiPrefix bool
|
||||
}
|
||||
|
||||
type RouteConfig struct {
|
||||
Transport string
|
||||
Addresses []string
|
||||
}
|
||||
Reference in new issue
Block a user