Simplified server API

This commit is contained in:
Eduard Urbach 2024-03-27 12:49:55 +01:00
parent cf3daa1f1b
commit fff7b6ae47
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0

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.