> For the complete documentation index, see [llms.txt](https://go-macaron.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://go-macaron.com/middlewares/switcher.md).

# Serving Multiple Sites

Module switcher provides host switch functionality for [Macaron](https://github.com/go-macaron/macaron).

* [GitHub](https://github.com/go-macaron/switcher)
* [API Reference](https://gowalker.org/github.com/go-macaron/switcher)

## Installation

```bash
go get github.com/go-macaron/switcher
```

## Usage

If you want to run 2 instances in one program, Host Switcher is the feature you're looking for.

```go
func main() {
    m1 := macaron.Classic()
    // Register m1 middlewares and routers.

    m2 := macaron.Classic()
    // Register m2 middlewares and routers.

    hs := switcher.NewHostSwitcher()
    // Set instance corresponding to host address.
    hs.Set("gowalker.org", m1)
    hs.Set("gogs.io", m2)
    hs.Run()
}
```

By default, this program will listen on ports `4000`(for `m1`) and `4001`(for `m2`) in `macaron.DEV` mode just for convenience. And only listen on `4000` in `macaron.PROD` mode.

### Dynamic match

In case you have different subdomains that need only one Macaron instance:

```go
// ...
m := macaron.Classic()
// Register m middlewares and routers.

hs := macaron.NewHostSwitcher()
// Set instance corresponding to host address.
hs.Set("*.example.com", m)
hs.Run()
// ...
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://go-macaron.com/middlewares/switcher.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
