gin-gonic / gin

Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
https://gin-gonic.com/
MIT License
77.95k stars 7.97k forks source link

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

Open icetech233 opened 3 weeks ago

icetech233 commented 3 weeks ago

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 commented 3 weeks ago

gorilla/mux

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

icetech233 commented 3 weeks ago

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

JimChenWYU commented 3 weeks ago
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 commented 3 weeks ago
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