Simplified server API

This commit is contained in:
Eduard Urbach 2024-03-27 12:49:55 +01:00
parent c4d4a1f0f0
commit 08d9f39c49
Signed by: eduard
GPG key ID: 49226B848C78F6C8

View file

@ -17,10 +17,7 @@ import (
// Server is the interface for an HTTP server. // Server is the interface for an HTTP server.
type Server interface { type Server interface {
Delete(path string, handler Handler)
Get(path string, handler Handler) Get(path string, handler Handler)
Post(path string, handler Handler)
Put(path string, handler Handler)
Request(method string, path string, body io.Reader) Response Request(method string, path string, body io.Reader) Response
Router() *router.Router[Handler] Router() *router.Router[Handler]
Run(address string) error Run(address string) error
@ -70,21 +67,6 @@ func (s *server) Get(path string, handler Handler) {
s.Router().Add("GET", path, handler) s.Router().Add("GET", path, handler)
} }
// Post registers your function to be called when the given POST path has been requested.
func (s *server) Post(path string, handler Handler) {
s.Router().Add("POST", path, handler)
}
// Delete registers your function to be called when the given DELETE path has been requested.
func (s *server) Delete(path string, handler Handler) {
s.Router().Add("DELETE", path, handler)
}
// Put registers your function to be called when the given PUT path has been requested.
func (s *server) Put(path string, handler Handler) {
s.Router().Add("PUT", path, handler)
}
// 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.