30

# golang搭建静态web服务器

 5 years ago
source link: https://studygolang.com/articles/18113?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

golang搭建静态web服务器

2018年03月10日 09:58:57 晴_空 阅读数:1275

<article class="baidu_pl" style="box-sizing: inherit; outline: 0px; display: block; position: relative; padding-top: 16px; color: rgba(0, 0, 0, 0.75); font-family: -apple-system, "SF UI Text", Arial, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: common-ligatures; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21682469/article/details/79505338

我胡汉三又回来啦。好久没发文了,为保持平台上的活跃度,我今天就分享下个刚学到的知识,使用golang搭建静态web 服务器 ,亲测可用,附代码!

使用过 golang 语言的程序猿都应该知道,在使用 golang 开发的时候,我们是不需要诸如 iis , apache , nginx , [kangle](https://www.baidu.com/s?wd=kangle&tn=24004469_oem_dg&rsv_dl=gh_pl_sl_csd) 等服务器支持的。

为什么呢?

原因是, golangnet/http 包中已经提供了 HTTP 的客户端与服务端实现方案。

网上言论都说 golang 不适合做web开发,相对 php、java、.net、nodejs 等各类后端语言来说,使用 golang 来做web开发,确实是一个大工程。

昨晚恰好看到一篇关于使用 golang 搭建web服务器的文章,心痒难耐,于是自己也折腾了一下,用来练练手。

我是新手上路,照搬文章里的内容,总是磕磕碰碰,每次运行都是找不到路径。代码是这样的:

func main() {
    http.Handle("/css/", http.FileServer(http.Dir("template")))
    http.Handle("/js/", http.FileServer(http.Dir("template")))

    http.ListenAndServe(":8080", nil)
}

目录结构:

src
|--main
|   |-main.go
|--template
|   |-css
|     |--admin.css
|   |-js
|     |--admin.js
|   |-html
|     |--404.html

以上运行结果是:找不到 template 这个路径。

其实我很纳闷,文章作者都可以成功运行起来这个demo,怎么到我这里,就启动不来了呢?

那么问题来了:

1.是什么原因导致程序起不来呢?
2.http.Dir()指向的是什么路径?

于是我追踪日志,如下

2018/01/07 11:09:28 open template/html/404.html: The system cannot find the path specified.

发现问题是出在 找不到路径 上。解决了第一个问题后,那么接下来就需要搞明白 http.Dir() 到底指向的是哪个路径。

我查看了官方例子:

log.Fatal(http.ListenAndServe(":8080", http.FileServer(http.Dir("/usr/share/doc"))))

从上面例子 http.Dir("/usr/share/doc") 可看出,该路径指向的是 linux系统 里的绝对路径。那么问题就解决了:我只需要将 http.Dir() 的路径改为运行时的相对路径,或者使用绝对路径就可以了。

另一个例子,使用http.StripPrefix()方法:

// To serve a directory on disk (/tmp) under an alternate URL
// path (/tmpfiles/), use StripPrefix to modify the request
// URL's path before the FileServer sees it:
http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))

可看出, tmpfilestmp 目录下的一个子目录。

既然问题都解决了,那么就修改一下代码,重新运行

func Template_dir() string {
    template_dir := "E:\\project\\gotest\\src\\template"
    return template_dir
}

func main() {
    http.Handle("/css/", http.FileServer(http.Dir(Template_dir())))
    http.Handle("/js/", http.FileServer(http.Dir(Template_dir())))

    http.ListenAndServe(":8080", nil)
}

编译运行后,在浏览器中输入 localhost:8080/css/ ,可成功看到 template/css/ 目录下的 admin.css 文件。

</article>


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK