Compare commits

..

No commits in common. "0442fc1e2e80a11087681da87795d874711465cb" and "c5ad01daee1a5d959091b96751937421679c39d8" have entirely different histories.

18 changed files with 23 additions and 40 deletions

2
.gitignore vendored
View file

@ -5,4 +5,4 @@
!*.md !*.md
!*.mod !*.mod
!*.sum !*.sum
!*.txt !*.txt

View file

@ -81,4 +81,4 @@ func (ctx *context) Status(status int) Context {
func (ctx *context) String(body string) error { func (ctx *context) String(body string) error {
ctx.response.body = append(ctx.response.body, body...) ctx.response.body = append(ctx.response.body, body...)
return nil return nil
} }

View file

@ -66,4 +66,4 @@ func TestRedirect(t *testing.T) {
response := s.Request("GET", "/", nil, nil) response := s.Request("GET", "/", nil, nil)
assert.Equal(t, response.Status(), 301) assert.Equal(t, response.Status(), 301)
assert.Equal(t, response.Header("Location"), "/target") assert.Equal(t, response.Header("Location"), "/target")
} }

View file

@ -1,4 +1,4 @@
package web package web
// Handler is a function that deals with the given request/response context. // Handler is a function that deals with the given request/response context.
type Handler func(Context) error type Handler func(Context) error

View file

@ -4,4 +4,4 @@ package web
type Header struct { type Header struct {
Key string Key string
Value string Value string
} }

View file

@ -87,4 +87,4 @@ Please see the [license documentation](https://urbach.dev/license).
## Copyright ## Copyright
© 2024 Eduard Urbach © 2024 Eduard Urbach

View file

@ -79,4 +79,4 @@ func (req *request) addParameter(key string, value string) {
Key: key, Key: key,
Value: value, Value: value,
}) })
} }

View file

@ -51,4 +51,4 @@ func TestRequestParam(t *testing.T) {
response := s.Request("GET", "/blog/my-article", nil, nil) response := s.Request("GET", "/blog/my-article", nil, nil)
assert.Equal(t, response.Status(), 200) assert.Equal(t, response.Status(), 200)
assert.Equal(t, string(response.Body()), "my-article") assert.Equal(t, string(response.Body()), "my-article")
} }

View file

@ -76,4 +76,4 @@ func (res *response) Write(body []byte) (int, error) {
func (res *response) WriteString(body string) (int, error) { func (res *response) WriteString(body string) (int, error) {
res.body = append(res.body, body...) res.body = append(res.body, body...)
return len(body), nil return len(body), nil
} }

View file

@ -97,4 +97,4 @@ func TestResponseHeaderOverwrite(t *testing.T) {
assert.Equal(t, response.Status(), 200) assert.Equal(t, response.Status(), 200)
assert.Equal(t, response.Header("Content-Type"), "text/html") assert.Equal(t, response.Header("Content-Type"), "text/html")
assert.Equal(t, string(response.Body()), "") assert.Equal(t, string(response.Body()), "")
} }

View file

@ -19,7 +19,6 @@ import (
// Server is the interface for an HTTP server. // Server is the interface for an HTTP server.
type Server interface { type Server interface {
Get(path string, handler Handler) Get(path string, handler Handler)
Ready() chan struct{}
Request(method string, path string, headers []Header, body io.Reader) Response Request(method string, path string, headers []Header, body io.Reader) Response
Router() *router.Router[Handler] Router() *router.Router[Handler]
Run(address string) error Run(address string) error
@ -32,7 +31,6 @@ type server struct {
contextPool sync.Pool contextPool sync.Pool
router *router.Router[Handler] router *router.Router[Handler]
errorHandler func(Context, error) errorHandler func(Context, error)
ready chan struct{}
} }
// NewServer creates a new HTTP server. // NewServer creates a new HTTP server.
@ -56,7 +54,6 @@ func NewServer() Server {
errorHandler: func(ctx Context, err error) { errorHandler: func(ctx Context, err error) {
log.Println(ctx.Request().Path(), err) log.Println(ctx.Request().Path(), err)
}, },
ready: make(chan struct{}),
} }
s.contextPool.New = func() any { return s.newContext() } s.contextPool.New = func() any { return s.newContext() }
@ -68,11 +65,6 @@ func (s *server) Get(path string, handler Handler) {
s.Router().Add("GET", path, handler) s.Router().Add("GET", path, handler)
} }
// Ready returns a channel that will be closed once the listener is ready for connection handling.
func (s *server) Ready() chan struct{} {
return s.ready
}
// Request performs a synthetic request and returns the response. // Request performs a synthetic request and returns the response.
// This function keeps the response in memory so it's slightly slower than a real request. // This function keeps the response in memory so it's slightly slower than a real request.
// However it is very useful inside tests where you don't want to spin up a real web server. // However it is very useful inside tests where you don't want to spin up a real web server.
@ -107,7 +99,6 @@ func (s *server) Run(address string) error {
stop := make(chan os.Signal, 1) stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM) signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
close(s.ready)
<-stop <-stop
return nil return nil
} }
@ -270,4 +261,4 @@ func (s *server) newContext() *context {
status: 200, status: 200,
}, },
} }
} }

View file

@ -35,7 +35,6 @@ func TestRun(t *testing.T) {
go func() { go func() {
defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM) defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
<-s.Ready()
_, err := http.Get("http://127.0.0.1:8080/") _, err := http.Get("http://127.0.0.1:8080/")
assert.Nil(t, err) assert.Nil(t, err)
}() }()
@ -49,7 +48,6 @@ func TestBadRequest(t *testing.T) {
go func() { go func() {
defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM) defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
<-s.Ready()
conn, err := net.Dial("tcp", ":8080") conn, err := net.Dial("tcp", ":8080")
assert.Nil(t, err) assert.Nil(t, err)
defer conn.Close() defer conn.Close()
@ -75,7 +73,6 @@ func TestBadRequestHeader(t *testing.T) {
go func() { go func() {
defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM) defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
<-s.Ready()
conn, err := net.Dial("tcp", ":8080") conn, err := net.Dial("tcp", ":8080")
assert.Nil(t, err) assert.Nil(t, err)
defer conn.Close() defer conn.Close()
@ -98,7 +95,6 @@ func TestBadRequestMethod(t *testing.T) {
go func() { go func() {
defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM) defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
<-s.Ready()
conn, err := net.Dial("tcp", ":8080") conn, err := net.Dial("tcp", ":8080")
assert.Nil(t, err) assert.Nil(t, err)
defer conn.Close() defer conn.Close()
@ -120,7 +116,6 @@ func TestBadRequestPath(t *testing.T) {
go func() { go func() {
defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM) defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
<-s.Ready()
conn, err := net.Dial("tcp", ":8080") conn, err := net.Dial("tcp", ":8080")
assert.Nil(t, err) assert.Nil(t, err)
defer conn.Close() defer conn.Close()
@ -146,7 +141,6 @@ func TestBadRequestProtocol(t *testing.T) {
go func() { go func() {
defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM) defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
<-s.Ready()
conn, err := net.Dial("tcp", ":8080") conn, err := net.Dial("tcp", ":8080")
assert.Nil(t, err) assert.Nil(t, err)
defer conn.Close() defer conn.Close()
@ -169,7 +163,6 @@ func TestConnectionClose(t *testing.T) {
go func() { go func() {
defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM) defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
<-s.Ready()
conn, err := net.Dial("tcp", ":8080") conn, err := net.Dial("tcp", ":8080")
assert.Nil(t, err) assert.Nil(t, err)
defer conn.Close() defer conn.Close()
@ -190,7 +183,6 @@ func TestEarlyClose(t *testing.T) {
go func() { go func() {
defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM) defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
<-s.Ready()
conn, err := net.Dial("tcp", ":8080") conn, err := net.Dial("tcp", ":8080")
assert.Nil(t, err) assert.Nil(t, err)
@ -211,4 +203,4 @@ func TestUnavailablePort(t *testing.T) {
s := web.NewServer() s := web.NewServer()
s.Run(":8080") s.Run(":8080")
} }

View file

@ -12,4 +12,4 @@ func main() {
}) })
s.Run(":8080") s.Run(":8080")
} }

6
go.mod
View file

@ -3,6 +3,6 @@ module git.urbach.dev/go/web
go 1.24 go 1.24
require ( require (
git.urbach.dev/go/assert v0.0.0-20250606150337-559d3d3afcda git.urbach.dev/go/assert v0.0.0-20250225153414-fc1f84f19edf
git.urbach.dev/go/router v0.0.0-20250606151301-c10e38fec918 git.urbach.dev/go/router v0.0.0-20250601162231-e35d5715d1a5
) )

8
go.sum
View file

@ -1,4 +1,4 @@
git.urbach.dev/go/assert v0.0.0-20250606150337-559d3d3afcda h1:VN6ZQwtwLOm2xTms+v8IIeeNjvs55qyEBNArv3dPq9g= git.urbach.dev/go/assert v0.0.0-20250225153414-fc1f84f19edf h1:BQWa5GKNUsA5CSUa/+UlFWYCEVe3IDDKRbVqBLK0mAE=
git.urbach.dev/go/assert v0.0.0-20250606150337-559d3d3afcda/go.mod h1:PNI/NSBOqvoeU58/7eBsIR09Yoq2S/qtSRiTrctkiq0= git.urbach.dev/go/assert v0.0.0-20250225153414-fc1f84f19edf/go.mod h1:y9jGII9JFiF1HNIju0u87OyPCt82xKCtqnAFyEreCDo=
git.urbach.dev/go/router v0.0.0-20250606151301-c10e38fec918 h1:fdeFr4gwou9KxvQqhcrPbLSYM9xzvaF1pwp53DzU3co= git.urbach.dev/go/router v0.0.0-20250601162231-e35d5715d1a5 h1:3qlZjgWbrHw4LWM4uBzOTldbpqCLJdeSgvcYK6f3xpc=
git.urbach.dev/go/router v0.0.0-20250606151301-c10e38fec918/go.mod h1:seUQ5raGaj6fDeZP6d7JdgnWQys8oTrtFdvBhAp1IZA= git.urbach.dev/go/router v0.0.0-20250601162231-e35d5715d1a5/go.mod h1:O+doTe0DZdT2XMsTY5pe6qUMqEEIAbwtX6Xp6EqHw34=

View file

@ -38,4 +38,4 @@ func parseURL(url string) (scheme string, host string, path string, query string
path = url path = url
return return
} }

View file

@ -46,4 +46,4 @@ func Text(ctx web.Context, body string) error {
func XML(ctx web.Context, body string) error { func XML(ctx web.Context, body string) error {
ctx.Response().SetHeader("Content-Type", "text/xml") ctx.Response().SetHeader("Content-Type", "text/xml")
return ctx.String(body) return ctx.String(body)
} }

View file

@ -64,4 +64,4 @@ func TestContentTypes(t *testing.T) {
assert.Equal(t, string(response.Body()), test.Response) assert.Equal(t, string(response.Body()), test.Response)
}) })
} }
} }