gorilla / mux

Package gorilla/mux is a powerful HTTP router and URL matcher for building Go web servers with 🦍
https://gorilla.github.io
BSD 3-Clause "New" or "Revised" License
20.93k stars 1.85k forks source link

Add Body Input / Output to Route [feature] #677

Open deBarrosS opened 2 years ago

deBarrosS commented 2 years ago

Description of the solution I'd like

How would it work? How would it change the API?

By walking the Router we are able to Get Routes' PathTemplates, QueriesTemplates, etc. But we are not able(at least I haven't been) to get the body accepted by the request neither the possible responses' bodies. This could really come in handy when automating the writing of API Documentations as well as validating the requests received.

By adding the functions like BodyInput( ) and BodyOutput( ) to a Route, we'd be able to set Input/Output body parameters (add Headers as well?). Therefore, by adding a function GetBodyInput() and GetBodyOutput() it'd be possible to read them. To the output nevertheless, the question to be made is : do we only set the 200 status response or do we set a map with codes as keys?

Alternatives I've considered

As to the alternatives, I'm trying to create an struct containing the routes, the parameters and the outputs to gaether them all in one place, but it seems to me that I'd be quite usefull to have them set on the Route itself.

hyahm commented 2 years ago

you can try https://github.com/hyahm/xmux example:

package main

import (
    "fmt"
    "net/http"

    "github.com/hyahm/xmux"
)

type R struct {
    Code int         `json:"code"`
    Data interface{} `json:"data"`
}

type User struct {
    Username string `json:"username"`
    Password string `json:"password"`
}

func main() {
    router := xmux.NewRouter()
    router.BindResponse(&R{})
        // router.Exit
    router.Post("/login", func(w http.ResponseWriter, r *http.Request) {
        fmt.Println(string(xmux.GetInstance(r).Body))
    }).BindJson(User{})
    router.Run()
}

when you request

curl -X 'POST' -d '
{
    "username": "foo",
    "password": "bar"
}'  http://localhost:8080/login

in console you can see the log

{
    "username": "foo",
    "password": "bar"
}
2022/06/17 09:31:04 connect_id: 1655429464276835500,method: POST        url: /login     time: 0.000975   status_code: 200, response: {"code":0,"data":null}

For more custom logs please use router.Exit to modify

amustaque97 commented 2 years ago

By adding the functions like BodyInput( ) and BodyOutput( ) to a Route, we'd be able to set Input/Output body parameters (add Headers as well?)

@deBarrosS could you be a bit more specific what are you trying to say with an example?

For adding headers as a part of request or response you can use middleware. If you haven't checked it before I would request you to please take a look at the project: https://github.com/gorilla/handlers/tree/master

HTTP status are the part of net/http package. Here is the link of all the HTTP status codes: https://go.dev/src/net/http/status.go

do we only set the 200 status response or do we set a map with codes as keys?

Again couldn't understand what are you trying to say here?