GolangNote

Golang笔记

goji 使用子域名示例

Permalink

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

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

本文网址: https://golangnote.com/topic/103.html 转摘请注明来源

Related articles

bolt 使用示例

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

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

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.22.3 Processed in 0ms