从根服务主页和静态内容 [英] Serve homepage and static content from root

查看:98
本文介绍了从根服务主页和静态内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



使用以下简单的Web服务器作为一个例子:

pre $ $ code $
$ b $(
fmt
net / http


func main(){
http.HandleFunc(/,HomeHandler)// homepage
http.ListenAndServe( :8080,无)
}

func HomeHandler(w http.ResponseWriter,r * http.Request){
fmt.Fprintf(w,HomeHandler)

$ / code>

如果我这样做

 http.Handle(/,http.FileServer(http.Dir(./)))

我收到一个恐慌,说我有两次注册/。我在互联网上找到的每个Golang示例都建议从不同的目录中提供静态内容,但对于诸如sitemap.xml,favicon.ico,robots.txt和其他通过练习的文件来说并没有多大帮助我要求的行为是在大多数Web服务器(如Apache,Nginx或IIS)中找到的行为,它在哪里首先遍历你的规则,如果没有找到规则,它会查找一个实际的文件,如果没有找到文件,那么它就是404s。我的猜测是,我不需要编写 http.HandlerFunc ,而需要编写一个 http.Handler 来检查是否我我是在引用一个带有扩展名的文件,如果是这样,检查是否存在文件并提供文件,否则404或服务主页是请求/。不幸的是,我不确定如何开始这样的任务。



我的一部分内容是我大大地过度复杂化了这种情况,这让我觉得我失踪了什么?任何指导将不胜感激。

解决方案

我想到的一件事可能会帮助您,您可以创建自己的ServeMux。我添加到您的示例,以便chttp是ServeMux,您可以提供静态文件。 HomeHandler然后检查它是否应该提供文件。我只是检查一个。但你可以做很多事情。

 包主

导入(
fmt
net / http
字符串


var chttp = http.NewServeMux()

func main(){

chttp.Handle(/,http.FileServer(http.Dir(./)))

http.HandleFunc( /,HomeHandler)//首页
http.ListenAndServe(:8080,nil)
}

func HomeHandler(w http.ResponseWriter,r * http.Request) {

if(strings.Contains(r.URL.Path,。)){
chttp.ServeHTTP(w,r)
} else {
fmt.Fprintf(w,HomeHandler)
}
}


In Golang, how do I serve static content out of the root directory while still having a root directory handler for serving the homepage.

Use the following simple web server as an example:

package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", HomeHandler) // homepage
    http.ListenAndServe(":8080", nil)
}

func HomeHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "HomeHandler")
}

If I do

http.Handle("/", http.FileServer(http.Dir("./")))

I receive a panic saying that I have two registrations for "/". Every Golang example I've found on the internet suggests serving their static content out of different directories, but that doesn't help much for things like sitemap.xml, favicon.ico, robots.txt and other files which are by-practice or mandated to always be served out of the root.

The behavior I seek is the behavior which is found in most web servers such as Apache, Nginx, or IIS, where it first traverses your rules, and if no rule is found it looks for an actual file, and if no file is found it 404s. My guess is that instead of writing a http.HandlerFunc, I need to write a http.Handler which checks if I am referencing a file with an extension, and if so checks for file existence and serves the file, otherwise it 404s or serves the homepage is the request was for "/". Unfortunately I'm not certain how to even begin such a task.

Part of me says I'm massively over-complicating the situation which makes me think that I am missing something? Any guidance would be appreciated.

解决方案

One thing I thought of that might help you is that you can create your own ServeMux. I added to your example so that chttp is a ServeMux that you can have serve static files. The HomeHandler then checks to see if it should serve a file or not. I just check for a "." but you could do a lot of things. Just an idea, might not be what you are looking for.

package main

import (
    "fmt"
    "net/http"
    "strings"
)   

var chttp = http.NewServeMux()

func main() {

    chttp.Handle("/", http.FileServer(http.Dir("./")))

    http.HandleFunc("/", HomeHandler) // homepage
    http.ListenAndServe(":8080", nil)
}   

func HomeHandler(w http.ResponseWriter, r *http.Request) {

    if (strings.Contains(r.URL.Path, ".")) {
        chttp.ServeHTTP(w, r)
    } else {
        fmt.Fprintf(w, "HomeHandler")
    }   
} 

这篇关于从根服务主页和静态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆