golang实现http服务器处理静态文件示例
程序员文章站
2022-08-29 08:34:03
本文实例讲述了golang实现http服务器处理静态文件的方法。分享给大家供大家参考,具体如下:
新版本更精简:
复制代码 代码如下:package main
imp...
本文实例讲述了golang实现http服务器处理静态文件的方法。分享给大家供大家参考,具体如下:
新版本更精简:
复制代码 代码如下:
package main
import (
"flag"
"log"
"net/http"
"os"
"io"
"path"
"strconv"
)
var dir string
var port int
var statichandler http.handler
// 初始化参数
func init() {
dir = path.dir(os.args[0])
flag.intvar(&port, "port", 80, "服务器端口")
flag.parse()
statichandler = http.fileserver(http.dir(dir))
}
func main() {
http.handlefunc("/", staticserver)
err := http.listenandserve(":"+strconv.itoa(port), nil)
if err != nil {
log.fatal("listenandserve: ", err)
}
}
// 静态文件处理
func staticserver(w http.responsewriter, req *http.request) {
if req.url.path != "/" {
statichandler.servehttp(w, req)
return
}
io.writestring(w, "hello, world!\n")
}
import (
"flag"
"log"
"net/http"
"os"
"io"
"path"
"strconv"
)
var dir string
var port int
var statichandler http.handler
// 初始化参数
func init() {
dir = path.dir(os.args[0])
flag.intvar(&port, "port", 80, "服务器端口")
flag.parse()
statichandler = http.fileserver(http.dir(dir))
}
func main() {
http.handlefunc("/", staticserver)
err := http.listenandserve(":"+strconv.itoa(port), nil)
if err != nil {
log.fatal("listenandserve: ", err)
}
}
// 静态文件处理
func staticserver(w http.responsewriter, req *http.request) {
if req.url.path != "/" {
statichandler.servehttp(w, req)
return
}
io.writestring(w, "hello, world!\n")
}
老版本:
复制代码 代码如下:
package main
import (
"flag"
"log"
"net/http"
"os"
"path"
"strconv"
)
var dir string
var port int
var indexs []string
// 初始化参数
func init() {
dir = path.dir(os.args[0])
flag.intvar(&port, "port", 80, "服务器端口")
flag.parse()
indexs = []string{"index.html", "index.htm"}
}
func main() {
http.handlefunc("/", staticserver)
err := http.listenandserve(":"+strconv.itoa(port), nil)
if err != nil {
log.fatal("listenandserve: ", err)
}
}
// 静态文件处理
func staticserver(w http.responsewriter, req *http.request) {
file := dir + req.url.path
fi, err := os.stat(file)
if os.isnotexist(err) {
http.notfound(w, req)
return
}
if err != nil {
http.error(w, err.error(), 500)
return
}
if fi.isdir() {
if req.url.path[len(req.url.path)-1] != '/' {
http.redirect(w, req, req.url.path+"/", 301)
return
}
for _, index := range indexs {
fi, err = os.stat(file + index)
if err != nil {
continue
}
http.servefile(w, req, file+index)
return
}
http.notfound(w, req)
return
}
http.servefile(w, req, file)
}
import (
"flag"
"log"
"net/http"
"os"
"path"
"strconv"
)
var dir string
var port int
var indexs []string
// 初始化参数
func init() {
dir = path.dir(os.args[0])
flag.intvar(&port, "port", 80, "服务器端口")
flag.parse()
indexs = []string{"index.html", "index.htm"}
}
func main() {
http.handlefunc("/", staticserver)
err := http.listenandserve(":"+strconv.itoa(port), nil)
if err != nil {
log.fatal("listenandserve: ", err)
}
}
// 静态文件处理
func staticserver(w http.responsewriter, req *http.request) {
file := dir + req.url.path
fi, err := os.stat(file)
if os.isnotexist(err) {
http.notfound(w, req)
return
}
if err != nil {
http.error(w, err.error(), 500)
return
}
if fi.isdir() {
if req.url.path[len(req.url.path)-1] != '/' {
http.redirect(w, req, req.url.path+"/", 301)
return
}
for _, index := range indexs {
fi, err = os.stat(file + index)
if err != nil {
continue
}
http.servefile(w, req, file+index)
return
}
http.notfound(w, req)
return
}
http.servefile(w, req, file)
}
希望本文所述对大家go语言程序设计有所帮助。