golang实现http服务器处理静态文件示例
本文实例讲述了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语言程序设计有所帮助。
标题名称:golang实现http服务器处理静态文件示例
网站网址:http://lswzjz.com/article/pgcghh.html