These are chat archives for gin-gonic/gin

13th
May 2015
Ryan Brooks
@ryanbyyc
May 13 2015 19:55
Hey guys, trying to figure out if there’s a way to get subdomain parameters running; in gorillamux we can do r.Host("{subdomain:[a-z]+}.domain.com”) - anyone know if its possible with Gin?
Manu MA
@manucorporat
May 13 2015 22:06
yes you can do that by using a middleware
Ryan Brooks
@ryanbyyc
May 13 2015 22:09
I don’t suppose you can recommend one before I go down the rabbithole myself?
Manu MA
@manucorporat
May 13 2015 22:11
func readSubdomain(c *gin.Context) {
    firstDot := strings.IndexRune(c.Request.Host, '.')
    if firstDot > 0 {
        c.Set("subdomain", c.Request.Host[0:firstDot])
    }
}

func main() {
    router := gin.Default()
    router.Use(readSubdomain)
    // ...
}
@sirstompsalot
you can convine it with groups + abort()
combine*
Manu MA
@manucorporat
May 13 2015 22:17
func main() {
    router := gin.Default()
    subdomainNeeded := router.Group("/", subdomainNeeded)
    {
        subdomainNeeded.GET("/login", loginIndex)
        subdomainNeeded.POST("/login", loginInSubdomain)
    }
    // ...
}

func loginInSubdomain(c *gin.Context) {
    subdomain := c.MustGet("subdomain").(string)
}

func subdomainNeeded(c *gin.Context) {
    firstDot := strings.IndexRune(c.Request.Host, '.')
    if firstDot > 0 {
        c.Set("subdomain", c.Request.Host[0:firstDot])
    } else {
        c.AbortWithStatus(401)
    }
}
Ryan Brooks
@ryanbyyc
May 13 2015 22:33
@manucorporat Hot holy hell, that's rad. Alright, thanks - very eager to flesh it out more.