-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilehttp.go
43 lines (37 loc) · 927 Bytes
/
filehttp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
import (
"fmt"
"os"
"net/http"
"io"
"path"
)
var basedir string
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: go run server.go [basedir]")
os.Exit(1)
}
basedir := os.Args[1]
fmt.Printf("serving content: %s\n", basedir)
http.HandleFunc("/", serveContent)
http.ListenAndServe(":7777", nil)
}
func serveContent(res http.ResponseWriter, req *http.Request) {
// TODO: req.URL.Path is the file you want to output as response
// Hint: Use io.Copy
fmt.Printf("serving content: %s\n", basedir) //空
p := path.Join(basedir, "index.html")
fmt.Printf("serving content: %s\n", p)
//index.html,应为public/index.html
f, err:= os.Open(p)
if err != nil {
res.WriteHeader(http.StatusInternalServerError)
res.Write([]byte(err.Error()))
return
}
_, err = io.Copy(res, f)
if err != nil {
fmt.Fprint(res, fmt.Sprint("error:",err))
}
}