(Which stands for Declarative ENdpoint Definition in Yaml.)
How to use
Define API endpoints in YAML.
hello:
path: /
type: GET
handlerName: Hello
auth:
path: /auth
type: POST
handlerName: Auth
Implement endpoints handler functions and put them in the map[string]http.HandlerFunc.
Unfortunately, Go doesn't allow to call package functions by their names without binding them somehow — be it a map or a type method. That's the reason for the requirement.
package handlers
import "net/http"
var Handlers = map[string]http.HandlerFunc{
"Hello": Hello,
"Auth": Auth,
}
func Hello(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World!"))
}
func Auth(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(403)
w.Write([]byte("You're not welcome here"))
}
Run server, passing previously created map[string]http.HandlerFunc as an argument.
GET http://localhost:3333
HTTP/1.1 200 OK
Date: Thu, 14 Dec 2023 16:27:09 GMT
Content-Length: 12
Content-Type: text/plain; charset=utf-8
Connection: close
Hello World!
POST http://localhost:3333/auth
HTTP/1.1 403 Forbidden
Date: Thu, 14 Dec 2023 16:28:59 GMT
Content-Length: 23
Content-Type: text/plain; charset=utf-8
Connection: close
You're not welcome here