GolangNote

Golang笔记

goji 使用子域名示例

Permalink

goji 是很棒的golang web 框架,下面是自定义不同子域名指向不同的handler

goji 使用子域名示例

Go: goji 子域名
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package main

import (
    "net/http"

    "github.com/zenazn/goji"
    "github.com/zenazn/goji/web"
)

func main() {
    mux1 := web.New()
    mux1.Get("/", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("domain one mux\n"))
    })
    http.Handle("domainone.com/", mux1)

    mux2 := web.New()
    mux2.Get("/", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("domain two mux\n"))
    })
    http.Handle("domaintwo.com/", mux2)

    goji.Get("/", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("main mux\n"))
    })
    goji.Serve()
}

测试:

Bash: curl
1
2
3
4
5
6
$ curl -H "Host: domainone.com" localhost:8000
domain one mux
$ curl -H "Host: domaintwo.com" localhost:8000
domain two mux
$ curl  localhost:8000
main mux

参考 https://godoc.org/net/url#URL

Related articles

bolt 使用示例

bolt 是一款高性能的key value 数据库,下面是它的使用示例:...

关掉goji 的log 输出

goji 默认对每个请求都输出请求时间,在项目刚起步时可以作为性能参考,但在实际生产环境里,可以关掉log...

Write a Comment to "goji 使用子域名示例"

Submit Comment Login
Based on Golang + fastHTTP + youdb | go1.16 Processed in 1ms