Skip to content

Commit 5fbda60

Browse files
committed
Update
1 parent 59bde61 commit 5fbda60

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed

xweb/cors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ func Cors(opts *CorsOptions) func(h http.Handler) http.Handler {
2727
return func(h http.Handler) http.Handler {
2828
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2929
if r.Method == http.MethodOptions {
30-
w.WriteHeader(http.StatusNoContent)
3130
w.Header().Add("Access-Control-Allow-Origin", opts.Origin)
3231
w.Header().Add("Access-Control-Allow-Credentials", strconv.FormatBool(opts.AllowCredentials))
3332
w.Header().Add("Access-Control-Allow-Methods", strings.Join(opts.AllowMethods, ","))
3433
w.Header().Add("Access-Control-Allow-Headers", strings.Join(opts.AllowHeaders, ","))
34+
w.WriteHeader(http.StatusNoContent)
3535
return
3636
}
3737

xweb/error.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package xweb
2+
3+
type (
4+
errorWithResponse struct {
5+
error
6+
response Response
7+
}
8+
9+
ErrorPayload struct {
10+
Message string `json:"message,omitempty"`
11+
Fields map[string]string `json:"fields,omitempty"`
12+
}
13+
)
14+
15+
func WrapErrorWithResponse(err error, res Response) error {
16+
return &errorWithResponse{error: err, response: res}
17+
}

xweb/handler.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package xweb
22

33
import (
4+
"fmt"
45
"net/http"
56
)
67

@@ -10,15 +11,15 @@ func (h Handler[P]) ServeHTTP(w http.ResponseWriter, r *http.Request) {
1011
xr := &Request[P]{Request: r}
1112
response, err := h(xr)
1213
if err != nil {
13-
//if wrappedError, ok := err.(*errorWithResponse); ok {
14-
// response = wrappedError.response
15-
//} else {
16-
// LogError(r.Context(), err)
17-
// response = &xweb.JSONResponse{
18-
// StatusCode: http.StatusInternalServerError,
19-
// Payload: ErrorPayload{Message: http.StatusText(http.StatusInternalServerError)},
20-
// }
21-
//}
14+
if wrappedError, ok := err.(*errorWithResponse); ok {
15+
response = wrappedError.response
16+
} else {
17+
LogError(r.Context(), err)
18+
response = &JSONResponse{
19+
StatusCode: http.StatusInternalServerError,
20+
Payload: ErrorPayload{Message: http.StatusText(http.StatusInternalServerError)},
21+
}
22+
}
2223
}
2324

2425
if response == nil {
@@ -32,6 +33,6 @@ func (h Handler[P]) ServeHTTP(w http.ResponseWriter, r *http.Request) {
3233

3334
w.WriteHeader(response.GetStatusCode())
3435
if err := response.WriteBody(w); err != nil {
35-
//LogError(r.Context(), fmt.Errorf("response.WriteBody: %v", err))
36+
LogError(r.Context(), fmt.Errorf("response.WriteBody: %v", err))
3637
}
3738
}

xweb/provider.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import (
99
)
1010

1111
type Provider struct {
12-
Addr string
13-
Routes func(router *Router)
12+
Addr string
13+
BaseURL string
14+
Routes func(router *Router)
1415
}
1516

1617
func (p *Provider) Register(app *xfoundation.App) error {
@@ -24,7 +25,12 @@ func (p *Provider) Register(app *xfoundation.App) error {
2425
if err != nil {
2526
return err
2627
}
27-
app.Logger.Info("starting http server", zap.String("addr", listener.Addr().String()))
28+
29+
if p.BaseURL == "" {
30+
p.BaseURL = "http://" + listener.Addr().String()
31+
}
32+
app.Logger.Info("starting http server", zap.String("url", p.BaseURL))
33+
2834
go httpServer.Serve(listener)
2935
return nil
3036
})

0 commit comments

Comments
 (0)