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
1
vote
1 answer

Header based routing with Chi

I'm trying to implement two routes using the Chi router. One should be invoked only if the "host" header is set to example.com. But only the lastly added route is invoked. r := chi.NewRouter() r.Use(middleware.Logger) middlewareHeader :=…
user672009
  • 4,379
  • 8
  • 44
  • 77
1
vote
1 answer

How do I serve static files with Go Chi?

I'm learning Go and I'm having a trouble serving static files with my server. I always get 404. The request is for http://localhost:3001/static/breakfast.jpg Router + fileServer mux := chi.NewRouter() mux.Get("/", handlers.Repo.Home) fileServer…
MadGrip
  • 391
  • 4
  • 17
1
vote
1 answer

How are static files served through .Handle() targeted?

EDIT: I am usinng chi as the router. It works differently from the standard one. See the answer for the key difference, relevant to my question. I am confused about how the files from my static site served by my application are targeted for…
WoJ
  • 27,165
  • 48
  • 180
  • 345
1
vote
1 answer

How to use a middleware for a specific route, among other ones?

My simplified routing is similar to r.Route("/api/v1", func(r chi.Router) { r.Route("/case", func(r chi.Router) { // generic case - for everyone r.Get("/{uuid}", caseGetByUuid) r.Put("/", casePut) …
WoJ
  • 27,165
  • 48
  • 180
  • 345
1
vote
1 answer

How to exclude specific route from middleware in go-chi

I've been using go-chi for a project and using an auth middleware for routes like this r := chi.NewRouter() r.Use(authService.AuthMiddleware) r.Route("/platform", func(r chi.Router) { r.Get("/version", RequestPlatformVersion) }) This is…
Thidasa Pankaja
  • 930
  • 8
  • 25
  • 44
1
vote
3 answers

Access DB instance from go-chi route handlers

I am trying to build a REST API with go-chi and Gorm. I am not sure how I should pass the Gorm DB instance to the route handlers. Or if I should create one instance per handler, which does not sound right to me. Should I use middleware, dependency…
aolivera
  • 512
  • 1
  • 4
  • 23
1
vote
1 answer

Chi GoLang http.FileServer returning 404 page not found

I have this very simple code to serve some files: wd, _ := os.Getwd() fs := http.FileServer(http.Dir(filepath.Join(wd,"../", "static"))) r.Handle("/static", fs) But this is throwing a 404 error. This directory is relative to my…
user11740269
1
vote
1 answer

Chi empty http.Request.Body in render.Bind

I am using github.com/pressly/chi to build this simple program where I try to decode some JSON from the http.Request.Body: package main import ( "encoding/json" "fmt" "net/http" "github.com/pressly/chi" …
1
vote
1 answer

CORS not working even though OPTIONS returns HTTP 200

I am using chi and have setup cors as follows func main() { r := chi.NewRouter() r.Use(render.SetContentType(render.ContentTypeJSON)) r.Use(Cors) r.Post("/auth/login", Login) r.Route("/ec2", func(r chi.Router) { …
Jiew Meng
  • 84,767
  • 185
  • 495
  • 805
0
votes
0 answers

How to validate incoming requests against openapi schema

I'm trying to validate all incoming HTTP requests against an OpenAPI spec (written in yaml). Ideally something similar to the js package express-openapi-validator (link). I've been searching for 'openapi validator go' but not having much luck. Is…
Alex K
  • 129
  • 6
0
votes
1 answer

Why the r.Method != "POST" branch does not reach?

The if branch in the snippetCreate func below doesn't reach when I make requests other than POST: func snippetCreate(w http.ResponseWriter, r *http.Request) { // anything under this if statement doesnt work, I tried even write logs and fmt…
0
votes
1 answer

How to log only errors like 404 with chi in go?

I'm using chi with our Go webservices. How to configure the logger (middleware) so it only logs requests that ended up with errors (like 404) but it doesn't log successful requests (with status code 200)? Here's our current implementation (with no…
Jacob
  • 351
  • 2
  • 9
0
votes
1 answer

pgxpool and multi-tenancy BeforeAcquire/AfterAcquire

I'm trying to use postgres user variables to handle multi-tenancy through RLS. However, for performance I want my webapp to use the pg connection pool. I came across this thread: https://github.com/jackc/pgx/issues/288#issuecomment-901975396 and…
Trent
  • 2,909
  • 1
  • 31
  • 46
0
votes
0 answers

Use brotli compression as a middleware

Hey guys I am trying to create a middleware that compresses the response depending on the "Accept-Encoding" header. I have created this logic according: func compress(h http.Handler) http.Handler { return http.HandlerFunc(func(w…
Accio
  • 74
  • 1
  • 8
0
votes
1 answer

How and when to strip the url to serve a static site?

I have the following directory structure for my static web site: │ infinote.exe └───spa │ favicon.ico │ index.html │ ├───css │ app.f99f51d4.css │ vendor.d9e2261d.css │ ├───fonts │ …
WoJ
  • 27,165
  • 48
  • 180
  • 345