Questions tagged [go-chi]

This tag should be used on questions related to the go-chi HTTP router.

go-chi is a lightweight router for building HTTP services, and especially REST API servers, in Go. It uses the context package introduced in Go 1.7 and aims at being fast, idiomatic and compatible with the standard Go net/http library.

68 questions
2
votes
1 answer

Go-Chi Fileserver Example Backend with VueJS Frontend Brings Error 404 Not Found When I Type A Route From Browser

The Fileserver Example Code: r.Get(path, func(w http.ResponseWriter, r *http.Request) { rctx := chi.RouteContext(r.Context()) // sets the route context pathPrefix := strings.TrimSuffix(rctx.RoutePattern(),…
darylvickerman
  • 572
  • 5
  • 12
2
votes
1 answer

How to log the HTTP return code with the chi router?

I use chi as my router and wrote a simple middleware that logs the request being made: func logCalls(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { log.Info().Msgf("%v → %v",…
WoJ
  • 27,165
  • 48
  • 180
  • 345
2
votes
1 answer

Request created with http.NewRequestWithContext() looses context when passed to middleware

In program bellow I have two routers. One is working at localhost:3000 and acts like a public access point. It also may send requests with data to another local address which is localhost:8000 where data is being processed. Second router is working…
mcv_dev
  • 338
  • 3
  • 14
2
votes
1 answer

How to set context-path for go-chi?

In Spring Boot app a base path can be set for all API resources with a property server.servlet.context-path. So the actual endpoint path will be server.servlet.context-path + endpoint path. For example, if server.servlet.context-path is set to…
Bing Qiao
  • 451
  • 1
  • 5
  • 14
2
votes
2 answers

How do I get CORS working with golang chi server?

I am trying to get cross origin request working against a golang chi server (https://github.com/go-chi/chi). The preflight request made by the browser isn't getting the expected headers (screen shot below). Here is a script that sets up a simple go…
ajith
  • 37
  • 1
  • 4
2
votes
1 answer

How to get parameters from URL in HTTP handler

I'm working on a web service application with endpoint/crypto/rates accepting as an input two “symbols” of currencies. The web service should search for currency exchange data between these characters in the database, and if there is no value in the…
DonPepemon
  • 101
  • 2
  • 9
2
votes
1 answer

Go Dep - Missing sub-package "chi/middleware" after dep ensure

Missing middleware subpackage from go-chi after running dep ensure. project-backend | main.go:8:2: cannot find package "github.com/go-chi/chi/middleware" in any of: project-backend | /go/src/backend/vendor/github.com/go-chi/chi/middleware…
Developer
  • 395
  • 3
  • 13
1
vote
1 answer

Serving SPA with Golang and Chi router enters a loop

I´m trying to serve a SPA with Golang and solving the 404 error became a challenge. In the init() func I buffered into memory the index.html file. I created a indexHandler that returns the index file buffered. I call the indexHandler in the…
1
vote
1 answer

Golang Post 400 bad request

I am trying to do a simple login. I followed a tutorial on Udemy. It went through rendering templates with a render function. I decided I wanted to handle templates differently and not have a render function and changed everything around. But I did…
1
vote
1 answer

How to test that a request fails with an invalid content type?

I'm using Chi with the builtin AllowContentType middleware. Given this sample package main import ( "net/http" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" ) func main() { http.ListenAndServe(":3000",…
baitendbidz
  • 187
  • 3
  • 19
1
vote
0 answers

Set custom handler in gRPC

I want to use a custom Handler( Specifically want to use a chi router) in gRPC. I have tried to search on the internet but don’t found help to set a custom handler in the gRPC server. Below is the code for my chi handler and I want to set it in gRPC…
Girish Bhutiya
  • 3,111
  • 5
  • 31
  • 50
1
vote
1 answer

go chi treating sub route path as URL param

i am creating a base router and adding few middlewares and health check route as below baseRouter := chi.NewRouter() baseRouter.Use(middleware.Logger) baseRouter.Use(core.CorsHandler) baseRouter.Get("/", func(w http.ResponseWriter, r *http.Request)…
vignesh
  • 498
  • 8
  • 18
1
vote
2 answers

Flutter Web: The script from .. was loaded even though its MIME type (“text/plain”) is not a valid JavaScript MIME type

I have backend code written in golang and fronted using flutter web. I am trying to serve flutter web via chi router and everything seems to work fine but I get the following errors in browser console. The script from…
Nux
  • 5,890
  • 13
  • 48
  • 74
1
vote
1 answer

Unable to run controller while using chi middleware in golang

I am writing a middleware function in golang net/http using chi but the controller function isn't being executed (couldn't print 1 in the function). Calling the middleware: r.With(myMiddleware.Authware).Post("/tryon", mainController.Tryon) package…
Krushnal Patel
  • 422
  • 2
  • 14
1
vote
0 answers

Log error returned in ServeHTTP with custom logger

I am using Go Chi with a custom handler function that returns an error. Ideally I want to write a middleware function that uses my custom handler function and return an error that will eventually need to be handled. I will either handle the error in…
Corbuk
  • 670
  • 1
  • 8
  • 23