Skip to content

Commit

Permalink
update requestip.IP to not panic when an IP isn't available
Browse files Browse the repository at this point in the history
  • Loading branch information
ntbosscher committed Nov 16, 2020
1 parent 63bdafa commit 34c0c9f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 6 additions & 1 deletion requestip/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,10 @@ func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

func IP(ctx context.Context) string {
return ctx.Value(_ipKey).(string)
value := ctx.Value(_ipKey)
if value == nil {
return ""
}

return value.(string)
}
4 changes: 4 additions & 0 deletions sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/ntbosscher/gobase/er"
"github.com/ntbosscher/gobase/httpdefaults"
"github.com/ntbosscher/gobase/model"
"github.com/ntbosscher/gobase/requestip"
"github.com/ntbosscher/gobase/res"
"github.com/ntbosscher/gobase/res/r"
"log"
Expand All @@ -26,6 +27,9 @@ func main() {
// setup database transactions
router.Use(model.AttachTxHandler("/websocket"))

// make requestip.IP() available
router.Use(requestip.Middleware())

// setup auth
router.WithAuth(httpauth.Config{
// .. setup jwt-based authentication
Expand Down

0 comments on commit 34c0c9f

Please sign in to comment.