Macaron Documentation
  • Welcome
  • Starter Guide
  • Core Concepts
  • Core Services
  • Custom Services
  • Middlewares
    • Routing
    • Templating
    • Gzip
    • Localization
    • Data Binding and Validation
    • Cache
    • Captcha
    • Session
    • Cross-Site Request Forgery
    • Embed Binary Data
    • Serving Multiple Sites
  • FAQs
  • 简体中文
    • 初学者指南
    • 核心概念
    • 核心服务
    • 自定义服务
    • 中间件和辅助模块
      • 路由模块
      • 模板引擎
      • Gzip 压缩
      • 应用本地化
      • 数据绑定与验证
      • 缓存管理(Cache)
      • 验证码服务
      • 会话管理(Session)
      • 跨域请求攻击(CSRF)
      • 嵌入二进制数据
      • 服务多个站点
    • 常见问题
Powered by GitBook
On this page
  • Installation
  • Usage
  • Dynamic match

Was this helpful?

  1. Middlewares

Serving Multiple Sites

PreviousEmbed Binary DataNextFAQs

Last updated 5 years ago

Was this helpful?

Module switcher provides host switch functionality for .

Installation

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.

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:

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

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