Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 119 lines (104 sloc) 2.988 kb
e180c26a »
2011-05-17 Add http fileserver for testing the site
1 package main
2
3 import (
4 "http"
5 "fmt"
6 "os"
7 "strconv"
8 "path/filepath"
9 "io"
675f34e6 »
2011-05-18 experimental Dropbox serving support. WIP
10 "log"
d2921d6c »
2011-06-12 Fix for new weekly Go release
11 "regexp"
12 "strings"
e180c26a »
2011-05-17 Add http fileserver for testing the site
13 )
14
15 var content_types = map[string] string {
16 "jpg" : "image/jpeg",
17 "png" : "image/png",
18 "gif" : "image/gif",
19 "js" : "text/javascript",
20 "css" : "text/css",
21 "html" : "text/html",
22 }
23
d2921d6c »
2011-06-12 Fix for new weekly Go release
24 var href_regex = regexp.MustCompile("(href=\"/[^\"]*\")")
25
675f34e6 »
2011-05-18 experimental Dropbox serving support. WIP
26 func Serve(root string) {
e180c26a »
2011-05-17 Add http fileserver for testing the site
27 // http.HandleFunc("/", FileRequest)
675f34e6 »
2011-05-18 experimental Dropbox serving support. WIP
28 http.Handle("/", http.FileServer(root, "/"))
e180c26a »
2011-05-17 Add http fileserver for testing the site
29 http.ListenAndServe(":6565", nil)
30 }
31
675f34e6 »
2011-05-18 experimental Dropbox serving support. WIP
32 func DropBoxServe() {
33 // TODO: Use actual HOME env
34 dropbox_root := "/Users/will/Dropbox/Sites"
35 fmt.Println("Going to serve directories in Dropbox/Sites")
36 dropbox_fd, err := os.Open(dropbox_root)
37 if err != nil {
38 log.Fatal("Cannot read your Dropbox/Sites folder - does it exist?")
39 }
40 files, err := dropbox_fd.Readdirnames(-1)
41 if err != nil {
42 log.Fatal("cannot list files in your Dropbox/Sites folder")
43 }
44 num_dirs := 0
45 for _,sitename := range files {
46 root := filepath.Join(dropbox_root, sitename)
47 site_info, err := os.Stat(root)
48 if err != nil {
49 fmt.Printf("Can't stat dir: %s\n", root)
50 }
51 if site_info.IsDirectory() {
d2921d6c »
2011-06-12 Fix for new weekly Go release
52 site_prefix := "/" + sitename + "/"
53 server := &DropboxServer{location: root, site_prefix: sitename, dropbox_location: dropbox_root}
54 fmt.Printf("Serving %s at http://localhost:6767/%s/ - site_prefix: %s\n", sitename, sitename, site_prefix)
55 //http.Handle(site_prefix, http.FileServer(root, site_prefix))
56 http.Handle(site_prefix, server)
675f34e6 »
2011-05-18 experimental Dropbox serving support. WIP
57 num_dirs++
58 }
59 }
60 if num_dirs > 0 {
61 fmt.Println("Listening on http://localhost:6767")
d2921d6c »
2011-06-12 Fix for new weekly Go release
62 http.HandleFunc("/", LogRequest)
675f34e6 »
2011-05-18 experimental Dropbox serving support. WIP
63 http.ListenAndServe(":6767", nil)
64 } else {
65 log.Fatal("No directories existed to serve")
66 }
67 }
68
d2921d6c »
2011-06-12 Fix for new weekly Go release
69 type DropboxServer struct {
70 location string
71 site_prefix string
72 dropbox_location string
73 }
74
75 func (server *DropboxServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
76 fmt.Println("Attempting dropbox file")
77 filename := r.URL.Path
78 full_filename := server.dropbox_location + filename
79 fmt.Println("Filename requested: ", full_filename)
80 //fmt.Fprintf(w, "hi\n")
81 FileRequest(full_filename, w, r)
82 }
675f34e6 »
2011-05-18 experimental Dropbox serving support. WIP
83
d2921d6c »
2011-06-12 Fix for new weekly Go release
84 func LogRequest(w http.ResponseWriter, r *http.Request) {
e180c26a »
2011-05-17 Add http fileserver for testing the site
85 filename := r.URL.Path
d2921d6c »
2011-06-12 Fix for new weekly Go release
86 fmt.Println("Default handler: ", filename)
87 fmt.Fprintf(w, "hi\n")
88 }
89
90 func FileRequest(filename string, w http.ResponseWriter, r *http.Request) {
e180c26a »
2011-05-17 Add http fileserver for testing the site
91 fmt.Println("Filename requested: ", filename)
92 filename_stat, err := os.Stat(filename)
93 if err != nil {
d2921d6c »
2011-06-12 Fix for new weekly Go release
94 fmt.Println("404 Not Found: ", filename)
e180c26a »
2011-05-17 Add http fileserver for testing the site
95 http.Error(w, err.String(), http.StatusNotFound)
96 return
97 }
98
99 if filename_stat.IsDirectory() {
100 return
101 }
102
103 fd, err := os.Open(filename)
104 if err != nil {
105 http.Error(w, err.String(), http.StatusInternalServerError)
106 return
107 }
108 defer fd.Close()
109
110 content_type := content_types[filepath.Ext(filename)]
d2921d6c »
2011-06-12 Fix for new weekly Go release
111
112 if strings.Contains(content_type, "text") {
113
114 }
115
e180c26a »
2011-05-17 Add http fileserver for testing the site
116 w.Header().Set("Content-Type", content_type)
117 w.Header().Set("Content-Length", strconv.Itoa64(filename_stat.Size))
118 io.Copy(w, fd)
119 }
Something went wrong with that request. Please try again.