go-chi / go-chi/chi

Subdomain example in route_headers.go not working

Open
#691 13 comments 0 reactions 0 assignees View on GitHub
help wanted
Dominant language
Go
Stars
22.8k
Forks
1.2k
Avg merge
5h 17m
Merged PRs (30d)
10

Description

I implemented the subdomain example provided in the [route_headers.go file](https://github.com/go-chi/chi/blob/master/middleware/route_headers.go).
Unfortunately the example provided is not working. The issue is that sub.example.com goes to the main domain page instead of sub domain page.
Can anyone please let me know how to fix the issue ? Or if the below code is working for them ?

I was testing on my local Ubuntu PC and following are the contents of /etc/hosts
_$ cat /etc/hosts
127.0.0.1 localhost
127.0.0.1 example.com
127.0.0.1 sub.example.com_

```
package main

import (
"fmt"
"net/http"

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)

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

r.Use(middleware.RouteHeaders().
Route("Host", "example.com", middleware.New(r)).
Route("Host", "*.example.com", middleware.New(rSubdomain)).
Handler)

r.Get("/", h)
rSubdomain.Get("/", h2)

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

func h(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Main domain")
}

func h2(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Sub domiain")
}
```

Contributor guide

Open the contributing guide

Research direction

Inspect middleware/route_headers.go and run the supplied program using the listed /etc/hosts entries; first compare the request Host values for example.com and sub.example.com with the documented routes. Done means the root request reliably reaches the main handler for the main domain and the subdomain handler for the subdomain, with coverage or a clarified example for both hosts.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
62/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.