Created
December 26, 2013 01:34
-
-
Save congjf/8128658 to your computer and use it in GitHub Desktop.
Golang Static File Server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Golang Static File Server |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func main(){ | |
log.Fatal(http.ListenAndServe(":8080", http.FileServer(http.Dir("/usr/share/doc")))) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func main() { | |
var router Router | |
server := &http.Server{ | |
Addr: ":8888", | |
Handler: router, | |
ReadTimeout: 10 * time.Second, | |
WriteTimeout: 10 * time.Second, | |
MaxHeaderBytes: 1 << 20, | |
} | |
log.Fatal(server.ListenAndServe()) | |
} | |
func (router Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) { | |
// Other HandlerFunc | |
FILEHandler(rw, req) | |
} | |
func FILEHandler(rw http.ResponseWriter, req *http.Request) { | |
h := http.FileServer(http.Dir(".")) | |
h.ServeHTTP(rw, req) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment