Skip to content
Newer
Older
100644 51 lines (44 sloc) 1 KB
e180c26 @wjlroe Add http fileserver for testing the site
authored
1 package main
2
3 import (
4 "http"
5 "fmt"
6 "os"
7 "strconv"
8 "path/filepath"
9 "io"
10 )
11
12 var content_types = map[string] string {
13 "jpg" : "image/jpeg",
14 "png" : "image/png",
15 "gif" : "image/gif",
16 "js" : "text/javascript",
17 "css" : "text/css",
18 "html" : "text/html",
19 }
20
21 func Serve() {
22 // http.HandleFunc("/", FileRequest)
23 http.Handle("/", http.FileServer("./", "/"))
24 http.ListenAndServe(":6565", nil)
25 }
26
27 func FileRequest(w http.ResponseWriter, r *http.Request) {
28 filename := r.URL.Path
29 fmt.Println("Filename requested: ", filename)
30 filename_stat, err := os.Stat(filename)
31 if err != nil {
32 http.Error(w, err.String(), http.StatusNotFound)
33 return
34 }
35
36 if filename_stat.IsDirectory() {
37 return
38 }
39
40 fd, err := os.Open(filename)
41 if err != nil {
42 http.Error(w, err.String(), http.StatusInternalServerError)
43 return
44 }
45 defer fd.Close()
46
47 content_type := content_types[filepath.Ext(filename)]
48 w.Header().Set("Content-Type", content_type)
49 w.Header().Set("Content-Length", strconv.Itoa64(filename_stat.Size))
50 io.Copy(w, fd)
51 }
Something went wrong with that request. Please try again.