Skip to content

Commit 0be9991

Browse files
chore: prefix http/* modules with "http" prefix for easier usage
1 parent 93d106b commit 0be9991

File tree

15 files changed

+28
-89
lines changed

15 files changed

+28
-89
lines changed

http/error/error.go http/httperror/error.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package error
1+
package httperror
22

33
import (
44
"fmt"

http/errorhandler/errorhandler.go http/httperror/errorhandler.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
package errorhandler
1+
package httperror
22

33
import (
44
"net/http"
5-
6-
httperror "go.inout.gg/foundations/http/error"
75
)
86

9-
var _ Handler = (HandlerFunc)(nil)
10-
var _ ErrorHandler = (ErrorHandlerFunc)(nil)
7+
var (
8+
_ Handler = (HandlerFunc)(nil)
9+
_ ErrorHandler = (ErrorHandlerFunc)(nil)
10+
)
1111

1212
// Handler is like http.Handler, but with an additional error return value.
1313
type Handler interface {
@@ -47,7 +47,7 @@ func WithErrorHandler(errorHandler ErrorHandler) func(HandlerFunc) http.HandlerF
4747

4848
// DefaultErrorHandler is the default error handler.
4949
var DefaultErrorHandler = ErrorHandlerFunc(func(w http.ResponseWriter, r *http.Request, err error) {
50-
if err, ok := err.(httperror.HttpError); ok {
50+
if err, ok := err.(HttpError); ok {
5151
http.Error(w, err.Error(), err.StatusCode())
5252
return
5353
}

http/handler/todo.go http/httphandler/todo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package handler
1+
package httphandler
22

33
import "net/http"
44

http/ip/ip.go http/httpip/ip.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ip
1+
package httpip
22

33
import (
44
"net"

http/middleware/chain.go http/httpmiddleware/chain.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package middleware
1+
package httpmiddleware
22

33
import "net/http"
44

55
var _ Middleware = (*Chain)(nil)
66

7+
// Chain represents a sequence of middlewares acting as a single Middleware.
78
type Chain struct {
89
middlewares []Middleware
910
}

http/middleware/chain_test.go http/httpmiddleware/chain_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package middleware
1+
package httpmiddleware
22

33
import (
44
"net/http"

http/middleware/middleware.go http/httpmiddleware/middleware.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package middleware
1+
package httpmiddleware
22

33
import "net/http"
44

http/request/request.go http/httprequest/request.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package request
1+
package httprequest
22

33
import (
44
"encoding/json"
@@ -26,16 +26,16 @@ var (
2626
func DecodeJSON[T any](r *http.Request) (*T, error) {
2727
ctx := r.Context()
2828

29-
var v T
30-
if err := json.NewDecoder(r.Body).Decode(&v); err != nil {
29+
var v *T
30+
if err := json.NewDecoder(r.Body).Decode(v); err != nil {
3131
return nil, fmt.Errorf("http/request: unable to decode JSON: %w", err)
3232
}
3333

34-
if err := Validator.StructCtx(ctx, &v); err != nil {
34+
if err := Validator.StructCtx(ctx, v); err != nil {
3535
return nil, err
3636
}
3737

38-
return &v, nil
38+
return v, nil
3939
}
4040

4141
// DecodeForm converts a url.Values (including form values) from the incoming

http/request/request_test.go http/httprequest/request_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package request
1+
package httprequest
22

33
import (
44
"bytes"
File renamed without changes.

http/response/json_devel.go http/httpresponse/json_devel.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//go:build !production
22

3-
package response
3+
package httpresponse
44

55
import (
66
"encoding/json"

http/response/templ.go http/httpresponse/templ.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package response
1+
package httpresponse
22

33
import (
44
"fmt"

http/server/server.go http/httpserver/server.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
package server
1+
package httpserver
22

33
import (
44
"context"
55
"crypto/tls"
66
"errors"
7+
"log/slog"
78
"net"
89
"net/http"
910
"time"
1011

11-
"log/slog"
12-
12+
"go.inout.gg/foundations/debug"
1313
"go.inout.gg/foundations/startstop"
1414
"golang.org/x/crypto/acme/autocert"
1515
"golang.org/x/sync/errgroup"
@@ -27,15 +27,18 @@ type Server struct {
2727
type ServerConfig struct {
2828
Handler http.Handler
2929

30-
// If EnableACME flag is set Port option is ignored.
30+
// If EnableACME flag is this option is ignored.
3131
Port int
3232

3333
// If EnableACME is true, then the server will use Let's Encrypt to automatically.
3434
EnableACME bool
3535
ACMEHosts []string
3636
}
3737

38+
// NewServer creates a new HTTP server using the provded config.
3839
func NewServer(config *ServerConfig) *Server {
40+
debug.Assert(config.Handler != nil, "expected Handler to be configured")
41+
3942
return &Server{
4043
log: slog.Default().With("name", "Server"),
4144
config: config,

sql/db/middleware.go

-22
Original file line numberDiff line numberDiff line change
@@ -1,22 +0,0 @@
1-
package db
2-
3-
import (
4-
"net/http"
5-
6-
"github.com/jackc/pgx/v5/pgxpool"
7-
"go.inout.gg/foundations/http/middleware"
8-
)
9-
10-
// FromRequest returns the pool associated with the given http request.
11-
func FromRequest(req *http.Request) (*pgxpool.Pool, error) {
12-
return FromContext(req.Context())
13-
}
14-
15-
// Middleware returns a middleware that injects the given pool into the request context.
16-
func Middleware(db *pgxpool.Pool) middleware.MiddlewareFunc {
17-
return func(next http.Handler) http.Handler {
18-
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
19-
next.ServeHTTP(w, req.WithContext(WithContext(req.Context(), db)))
20-
})
21-
}
22-
}

template/html/middleware.go

-43
Original file line numberDiff line numberDiff line change
@@ -1,43 +0,0 @@
1-
package html
2-
3-
import (
4-
"context"
5-
"errors"
6-
"net/http"
7-
8-
"go.inout.gg/foundations/http/middleware"
9-
"go.inout.gg/foundations/must"
10-
)
11-
12-
type ctxKey struct{}
13-
14-
var kCtxKey = ctxKey{}
15-
16-
func Middleware(r Renderer) middleware.MiddlewareFunc {
17-
return func(next http.Handler) http.Handler {
18-
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
19-
next.ServeHTTP(w, req.WithContext(context.WithValue(req.Context(), kCtxKey, r)))
20-
})
21-
}
22-
}
23-
24-
// Render renders an HTML template with the given name and variables.
25-
func Render(w http.ResponseWriter, req *http.Request, name string, vars ...any) error {
26-
render, ok := req.Context().Value(kCtxKey).(Renderer)
27-
if !ok {
28-
return errors.New(
29-
"http/render: unable to retrieve render from request context. Make sure to use corresponding middleware.",
30-
)
31-
}
32-
33-
if err := render.Render(w, name, vars); err != nil {
34-
return err
35-
}
36-
37-
return nil
38-
}
39-
40-
// MustRender is like Render, but panics if an error occurs.
41-
func MustRender(w http.ResponseWriter, req *http.Request, name string, vars ...any) {
42-
must.Must1(Render(w, req, name, vars...))
43-
}

0 commit comments

Comments
 (0)