Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can suport domain router? like github.com/gorilla/mux #4037

Open
icetech233 opened this issue Aug 22, 2024 · 6 comments
Open

can suport domain router? like github.com/gorilla/mux #4037

icetech233 opened this issue Aug 22, 2024 · 6 comments

Comments

@icetech233
Copy link

can suport domain router? like github.com/gorilla/mux

Matching Routes
Routes can also be restricted to a domain or subdomain. Just define a host pattern to be matched. They can also have variables:

r := mux.NewRouter()
// Only matches if domain is "www.example.com".
r.Host("www.example.com")
// Matches a dynamic subdomain.
r.Host("{subdomain:[a-z]+}.example.com")
There are several other matchers that can be added. To match path prefixes:

r.PathPrefix("/products/")

@icetech233
Copy link
Author

icetech233 commented Aug 22, 2024

gorilla/mux

r.Host("{subdomain:[a-z]+}.example.com")

@icetech233
Copy link
Author

r.HandleFunc("/products", ProductsHandler).
Host("www.example.com").
Methods("GET").
Schemes("http")

@JimChenWYU
Copy link

package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	api := r.Group("/api")
	api.GET("/users/{id}", func(ctx *gin.Context) {
		ctx.JSON(http.StatusOK, gin.H{
			"message": "hello world",
		})
	})

	if err := r.Run(":8080"); err != nil {
		panic(err)
	}
}

image

Is this what you want?

@icetech233
Copy link
Author

package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	api := r.Group("/api")
	api.GET("/users/{id}", func(ctx *gin.Context) {
		ctx.JSON(http.StatusOK, gin.H{
			"message": "hello world",
		})
	})

	if err := r.Run(":8080"); err != nil {
		panic(err)
	}
}

image

Is this what you want?

不是 ,域名 的英文 不认识吗??写了 host domain

@ChenPuChu
Copy link

package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	api := r.Group("/api")
	api.GET("/users/{id}", func(ctx *gin.Context) {
		ctx.JSON(http.StatusOK, gin.H{
			"message": "hello world",
		})
	})

	if err := r.Run(":8080"); err != nil {
		panic(err)
	}
}

image
Is this what you want?

不是 ,域名 的英文 不认识吗??写了 host domain

我想我明白你的意思了,你是想实现向nginx那样,可以根据域名匹配接口的功能嘛

@icetech233
Copy link
Author

package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	api := r.Group("/api")
	api.GET("/users/{id}", func(ctx *gin.Context) {
		ctx.JSON(http.StatusOK, gin.H{
			"message": "hello world",
		})
	})

	if err := r.Run(":8080"); err != nil {
		panic(err)
	}
}

image
Is this what you want?

不是 ,域名 的英文 不认识吗??写了 host domain

我想我明白你的意思了,你是想实现向nginx那样,可以根据域名匹配接口的功能嘛

对的,nginx 就有域名的功能, gorilla/mux 也有

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants