duana1021 2017-03-14 19:50
浏览 156
已采纳

Golang:如何处理和提供子域名?

The problem is serving the domain as well as subdomains x,y,z (or in this example blog, admin, and design). When running the following and requesting blog.localhost:8080/ firefox cant find the server www.blog.localhost:8080.

package main

import (
    "html/template"
    "log"
    "net/http"
)

var tpl *template.Template

const (
    domain = "localhost"
    blogDomain = "blog." + domain
    adminDomain = "admin." + domain
    designDomain = "design." + domain
)


func init() {
     tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
}

func main() {


    // Default Handlers
    http.HandleFunc("/", index)

    // Blog Handlers
    http.HandleFunc(blogDomain+"/", blogIndex)

    // Admin Handlers
    http.HandleFunc(adminDomain+"/", adminIndex)

    // Design Handlers
    http.HandleFunc(designDomain+"/", designIndex)

    http.Handle("/static/", http.StripPrefix("/static", http.FileServer(http.Dir("static"))))
    http.ListenAndServe(":8080", nil) 

}

func index(res http.ResponseWriter, req *http.Request) {
    err := tpl.ExecuteTemplate(res, "index.gohtml", nil)
    if err != nil {
        log.Fatalln("template didn't execute: ", err)
    }
}

// Blog Handlers
func blogIndex(res http.ResponseWriter, req *http.Request) {
    err := tpl.ExecuteTemplate(res, "index.gohtml", nil)
    if err != nil {
        log.Fatalln("template didn't execute: ", err)
    }
}

// Admin Handlers
func adminIndex(res http.ResponseWriter, req *http.Request) {
    err := tpl.ExecuteTemplate(res, "index.gohtml", nil)
    if err != nil {
        log.Fatalln("template didn't execute: ", err)
    }
}

// Design Handlers
func designIndex(res http.ResponseWriter, req *http.Request) {
    err := tpl.ExecuteTemplate(res, "index.gohtml", nil)
    if err != nil {
        log.Fatalln("template didn't execute: ", err)
    }
}

Is it possible to serve subdomains using the standard library? If so how?

EDIT: Requesting localhost:8080/ works fine

EDIT2: I edited /etc/hosts to include the subdomains:

127.0.0.1 blog.localhost.com
127.0.0.1 admin.localhost.com
127.0.0.1 design.localhost.com

Pinging them works correctly but firefox cannot reach them.

  • 写回答

1条回答 默认 最新

  • dongyinju5977 2017-03-14 22:55
    关注

    Given the hosts file in your second edit, you can point firefox to, for example, blog.localhost.com:8080 but then you also have to handle that domain pattern, i.e. http.HandleFunc(blogDomain+":8080/", blogIndex).

    If that's not what you want you can instead listen on port 80 i.e. http.ListenAndServe(":80", nil), you might need to run your app in sudo so that it has permission to use that port, then your blog.localhost.com should work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制