Fiber: 馃 Multiple domains

Created on 5 Sep 2020  路  6Comments  路  Source: gofiber/fiber

How do we achieve something like this (https://echo.labstack.com/cookbook/subdomains) using fiber?

馃 Question

Most helpful comment

I didn't test it, but it should work 馃憤

package main

import (
    "log"

    "github.com/gofiber/fiber"
    "github.com/gofiber/fiber/middleware"
)

type (
    Host struct {
        Fiber *fiber.App
    }
)

func main() {
    // Hosts
    hosts := map[string]*Host{}

    //-----
    // API
    //-----

    api := fiber.New()
    api.Use(middleware.Logger())
    api.Use(middleware.Recover())

    hosts["api.localhost:3000"] = &Host{api}

    api.Get("/", func(c *fiber.Ctx) {
        c.SendString("API")
    })

    //------
    // Blog
    //------

    blog := fiber.New()
    blog.Use(middleware.Logger())
    blog.Use(middleware.Recover())

    hosts["blog.localhost:3000"] = &Host{blog}

    blog.Get("/", func(c *fiber.Ctx) {
        c.SendString("Blog")
    })

    //---------
    // Website
    //---------

    site := fiber.New()
    site.Use(middleware.Logger())
    site.Use(middleware.Recover())

    hosts["localhost:3000"] = &Host{site}

    site.Get("/", func(c *fiber.Ctx) {
        c.SendString("Website")
    })

    // Server
    app := fiber.New()
    app.Use(func(c *fiber.Ctx) {
        host := hosts[c.Hostname()]
        if host == nil {
            c.SendStatus(fiber.StatusNotFound)
        } else {
            host.Fiber.Handler()(c.Fasthttp)
        }
    })
    log.Fatal(app.Listen(":3000"))
}

All 6 comments

Thanks for opening your first issue here! 馃帀 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

I didn't test it, but it should work 馃憤

package main

import (
    "log"

    "github.com/gofiber/fiber"
    "github.com/gofiber/fiber/middleware"
)

type (
    Host struct {
        Fiber *fiber.App
    }
)

func main() {
    // Hosts
    hosts := map[string]*Host{}

    //-----
    // API
    //-----

    api := fiber.New()
    api.Use(middleware.Logger())
    api.Use(middleware.Recover())

    hosts["api.localhost:3000"] = &Host{api}

    api.Get("/", func(c *fiber.Ctx) {
        c.SendString("API")
    })

    //------
    // Blog
    //------

    blog := fiber.New()
    blog.Use(middleware.Logger())
    blog.Use(middleware.Recover())

    hosts["blog.localhost:3000"] = &Host{blog}

    blog.Get("/", func(c *fiber.Ctx) {
        c.SendString("Blog")
    })

    //---------
    // Website
    //---------

    site := fiber.New()
    site.Use(middleware.Logger())
    site.Use(middleware.Recover())

    hosts["localhost:3000"] = &Host{site}

    site.Get("/", func(c *fiber.Ctx) {
        c.SendString("Website")
    })

    // Server
    app := fiber.New()
    app.Use(func(c *fiber.Ctx) {
        host := hosts[c.Hostname()]
        if host == nil {
            c.SendStatus(fiber.StatusNotFound)
        } else {
            host.Fiber.Handler()(c.Fasthttp)
        }
    })
    log.Fatal(app.Listen(":3000"))
}

Assuming my example did the trick I'm closing this issue, feel free to re-open if you have further questions!

Or you could join us on Discord where the community is always ready to answer your question!

Hello, how to achive the same with fiber v2?

This doesn't appear to work for version 2.
host.Fiber.Handler()(c.Fasthttp)
throws an error.

host.fiber.Handler()(c.Context()) works for v2

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lucasmdomingues picture lucasmdomingues  路  3Comments

mhf-ir picture mhf-ir  路  3Comments

koddr picture koddr  路  4Comments

bashery picture bashery  路  4Comments

Ivan-Feofanov picture Ivan-Feofanov  路  3Comments