Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Feature/Addition Host domain #2185

Merged
merged 2 commits into from
Dec 9, 2021

Conversation

dmuriel
Copy link
Contributor

@dmuriel dmuriel commented Dec 7, 2021

This PR adds new functionality for Buffalo, by using the Host function from Gorilla/Mux, buffalo can now restrict the handlers and render those that only match the specified host. This is very useful if you have multiple portals in the same application and/or want to use the same path for different portals.

...

func HomeApp(c buffalo.Context) error {
	return c.Render(http.StatusOK, r.String("OK APP"))
}

func HomeLMS(c buffalo.Context) error {
	return c.Render(http.StatusOK, r.String("OK LMS"))
}

func New() *buffalo.App {
	bapp := buffalo.New(buffalo.Options{
		Env:         envy.Get("GO_ENV", "development"),
		SessionName: "_application_session",
	})

        app := bapp.Host("app.domain.com")
        app.GET("/dashboard", HomeApp) // This will render the dashboard for the Application

        lms := bapp.Host("lms.domain.com")
        lms.GET("/dashboard", HomeLMS) // This will render the dashboard for the LMS

	return bapp
}

...

When testing, you need to specify the exact path, including the host, otherwise it will return a 404:

func (as ActionSuite) Test_App_Dashboard() {
    // This one will not throw a 404
    res := as.HTML("http://app.domain.com/dashboard/").Get()
    as.Equal(http.StatusOK, res.Code)
    as.Contains(res.Body.String(), "OK APP")
}

func (as ActionSuite) Test_LMS_Dashboard() {
    // This one will throw a 404
    res := as.HTML("http://cms.domain.com/dashboard/").Get()
    as.Equal(http.StatusOK, res.Code)
    as.Contains(res.Body.String(), "OK LMS")
}

@dmuriel dmuriel requested a review from a team as a code owner December 7, 2021 17:12
@paganotoni paganotoni changed the base branch from main to development December 7, 2021 19:49
@paganotoni paganotoni merged commit d179109 into gobuffalo:development Dec 9, 2021
@paganotoni
Copy link
Member

Thanks for this one @dmuriel! Love it.

@saurori
Copy link
Contributor

saurori commented Dec 16, 2021

I created a PR for adding this to the docs: gobuffalo/docs#605

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

Successfully merging this pull request may close these issues.

3 participants