go-chi/chi

Subdomain example in route_headers.go not working

オープン

#691 opened on 2021/12/28

 (13 件のコメント) (0 件のリアクション) (0 人の担当者)Go (555 件のフォーク)batch import
help wanted

Repository metrics

Stars
 (8,127 個のスター)
PR merge metrics
 (平均マージ 16h 36m) (30d で 2 merged PRs)

説明

I implemented the subdomain example provided in the route_headers.go file. 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")
}

コントリビューターガイド