Created
November 13, 2016 03:22
-
-
Save Ladicle/f5ff2a06e12960cd2d97f24ff788f468 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"bufio" | |
"io" | |
"log" | |
"net/http" | |
"os" | |
"path/filepath" | |
) | |
func main() { | |
http.HandleFunc("/", staticFileHandler) | |
http.ListenAndServe(":8085", nil) | |
} | |
// staticファイルを転送するハンドラ | |
func staticFileHandler(w http.ResponseWriter, r *http.Request) { | |
fname, _ := filepath.Abs("data/install-coreos.sh") | |
file, ferr := os.OpenFile(fname, os.O_RDONLY, 0600) | |
defer file.Close() | |
if ferr != nil { | |
log.Println("not found file:" + fname) | |
w.WriteHeader(http.StatusNotFound) | |
return | |
} | |
reader := bufio.NewReader(file) | |
buf := make([]byte, 1024, 1024) | |
LOOP: | |
for { | |
n, err := reader.Read(buf) | |
switch { | |
case err == nil: | |
w.Write(buf[:n]) | |
case err == io.EOF: | |
log.Println("static file=" + fname) | |
break LOOP | |
case err != nil: | |
log.Printf("file error occured. err=%s\n", err) | |
break LOOP | |
} | |
} | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment