Advertisement
teknoraver

httpd.go

Sep 2nd, 2016
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.44 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "io"
  5.     "log"
  6.     "net/http"
  7.     "os"
  8. )
  9.  
  10. func serveStatic(resp http.ResponseWriter, req *http.Request) {
  11.     path := req.URL.Path[1:]
  12.     if file, err := os.Open(path); err == nil {
  13.         io.Copy(resp, file)
  14.         file.Close()
  15.     } else {
  16.         resp.WriteHeader(http.StatusNotFound)
  17.         io.WriteString(resp, path + ": not found")
  18.     }
  19. }
  20.  
  21. func main() {
  22.     http.HandleFunc("/", serveStatic)
  23.     log.Fatal(http.ListenAndServe(":8080", nil))
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement