Golang implementation HTTP Server processing static file example _golang

Source: Internet
Author: User
Tags file handling server port

The example in this article describes the Golang method for implementing HTTP server processing static files. Share to everyone for your reference, specific as follows:

The new version is more streamlined:

Copy Code code as follows:
Package Main
Import (
"Flag"
"Log"
"Net/http"
"OS"
"IO"
"Path"
"StrConv"
)
var dir string
var Port int
var Statichandler http. Handler
Initialization parameters
Func init () {
dir = path. Dir (OS. Args[0])
Flag. Intvar (&port, "Port", 80, "Server port")
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)
}
}
Static file Handling
Func staticserver (w http. Responsewriter, req *http. Request) {
If req. Url. Path!= "/" {
Statichandler.servehttp (W, req)
Return
}
Io. WriteString (W, "Hello, world!\n")
}

Old version:

Copy Code code as follows:
Package Main
Import (
"Flag"
"Log"
"Net/http"
"OS"
"Path"
"StrConv"
)
var dir string
var Port int
var Indexs []string
Initialization parameters
Func init () {
dir = path. Dir (OS. Args[0])
Flag. Intvar (&port, "Port", 80, "Server port")
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)
}
}
Static file Handling
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)
}

I hope this article will help you with your go language program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.