Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(httpin): example of using httpin with chi #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# community-examples

go-chi community examples

- [httpin](./httpin/): decode an HTTP request into a custom struct, including querystring, headers, form-data, JSON/XML body, path variables, etc.
19 changes: 19 additions & 0 deletions httpin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# httpin

**httpin** - Decode an HTTP request into a custom struct, including querystring, forms, HTTP headers, etc.

Documentation: https://ggicci.github.io/httpin/

Integrate with chi: https://ggicci.github.io/httpin/integrations/gochi.

## Run this example

```bash
# start the http server
go run main.go


curl -H "Authorization: abc" "http://localhost:3333/users/ggicci/repos?visibility=private&fork=1"
# Output:
# {"Username":"ggicci","Visibility":"private","Fork":true,"Token":"abc"}
```
8 changes: 8 additions & 0 deletions httpin/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module httpin

go 1.19

require (
github.com/ggicci/httpin v0.10.1
github.com/go-chi/chi/v5 v5.0.7
)
21 changes: 21 additions & 0 deletions httpin/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
github.com/ggicci/httpin v0.10.1 h1:qLHjEBjn/ErbUHFryeXYb6ZJtbt4B9dCAVd+Bl8GaxM=
github.com/ggicci/httpin v0.10.1/go.mod h1:RpidMNiWsPdLRwjuXAcvguOOWsEv0KS/ekjTp53UPqY=
github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8=
github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/justinas/alice v1.2.0 h1:+MHSA/vccVCF4Uq37S42jwlkvI2Xzl7zTPCN5BnZNVo=
github.com/justinas/alice v1.2.0/go.mod h1:fN5HRH/reO/zrUflLfTN43t3vXvKzvZIENsNEe7i7qA=
github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs=
github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo=
github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg=
github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
40 changes: 40 additions & 0 deletions httpin/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"encoding/json"
"net/http"

"github.com/ggicci/httpin"
"github.com/go-chi/chi/v5"
)

type ListUserReposInput struct {
Username string `in:"path=username"`
Visibility string `in:"query=visibility"`
Fork bool `in:"query=fork"`
Token string `in:"header=Authorization"`
}

func ListUserRepos(rw http.ResponseWriter, r *http.Request) {
// Retrieve you data in one line of code!
input := r.Context().Value(httpin.Input).(*ListUserReposInput)

json.NewEncoder(rw).Encode(input)
}

func init() {
// Register a directive named "path" to retrieve values from `chi.URLParam`,
// i.e. decode path variables.
httpin.UseGochiURLParam("path", chi.URLParam)
}

func main() {
r := chi.NewRouter()

// Bind input struct with handler.
r.With(
httpin.NewInput(ListUserReposInput{}),
).Get("/users/{username}/repos", ListUserRepos)

http.ListenAndServe(":3333", r)
}