Commits

Miki Tebeka committed 4834e02

working

Comments (0)

Files changed (6)

 
 README.html
 nrsc
+playground/nrsc
+package main
+
+const iface = `
+package nrsc
+
+import (
+	"bytes"
+	"fmt"
+	"io"
+	"net/http"
+	"time"
+)
+
+type Resource interface {
+	Open() io.Reader
+	Size() int64
+	MimeType() string
+	ModTime() time.Time
+}
+
+type resource struct {
+	size  int64
+	mtime time.Time
+	mtype string
+	data  []byte
+}
+
+func (rsc *resource) Open() io.Reader {
+	return bytes.NewReader(rsc.data)
+}
+
+func (rsc *resource) Size() int64 {
+	return rsc.size
+}
+
+func (rsc *resource) MimeType() string {
+	return rsc.mtype
+}
+
+func (rsc *resource) ModTime() time.Time {
+	return rsc.mtime
+}
+
+type handler int
+
+func (h handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
+	rsc := resources[req.URL.Path]
+	if rsc == nil {
+		http.NotFound(w, req)
+		return
+	}
+
+	if len(rsc.MimeType()) != 0 {
+		w.Header().Set("Content-Type", rsc.MimeType())
+	}
+	w.Header().Set("Content-Size", fmt.Sprintf("%d", rsc.Size()))
+	w.Header().Set("Last-Modified", rsc.ModTime().UTC().Format(http.TimeFormat))
+
+	io.Copy(w, rsc.Open())
+}
+
+func Get(path string) Resource {
+	return resources[path]
+}
+
+func Handle(prefix string) {
+	var h handler
+	http.Handle(prefix, http.StripPrefix(prefix, h))
+}
+`
 	fmt.Fprintf(out, "\t\tmtime: time.Unix(%d, 0),\n", info.ModTime().Unix())
 	mtype := mime.TypeByExtension(filepath.Ext(path))
 	fmt.Fprintf(out, "\t\tmtype: \"%s\",\n", mtype)
-	fmt.Fprintf(out, "\t\tdata: {")
+	fmt.Fprintf(out, "\t\tdata: []byte{")
 	for _, b := range data {
 		fmt.Fprintf(out, "%d, ", b)
 	}
 	return false
 }
 
+func writeResources(root string, out io.Writer) error {
+	prefix := len(root)
+	if root[len(root)-1] != '/' {
+		prefix += 1
+	}
+
+	fmt.Fprintf(out, "package nrsc\nimport \"time\"\n")
+	fmt.Fprintf(out, "var resources = map[string]Resource {\n")
+
+	for file := range iterfiles(root) {
+		if err := writeResource(prefix, file, out); err != nil {
+			return fmt.Errorf("can't write %s - %s", file.path, err)
+		}
+	}
+
+	fmt.Fprintf(out, "\n}")
+
+	return nil
+}
+
 func main() {
 	var root string
+	outdir := "nrsc"
 
 	flag.StringVar(&root, "root", "", "root direcotry")
 	flag.Parse()
 		die("%s is not a directory", root)
 	}
 
-	if !dirExists("nrsc") {
-		if err := os.Mkdir("nrsc", 0700); err != nil {
+	if !dirExists(outdir) {
+		if err := os.Mkdir(outdir, 0700); err != nil {
 			die("can't create nrsc directory - %s", err)
 		}
 	}
 
 	defer func() {
 		if !ok {
-			os.RemoveAll("nrsc")
+			fmt.Printf("cleaning %s\n", outdir)
+			os.RemoveAll(outdir)
 		}
 	}()
 
-	out, err := os.Create("nrsc/nrsc.go")
+	outfile := fmt.Sprintf("%s/nrsc.go", outdir)
+	err := ioutil.WriteFile(outfile, []byte(iface), 0666)
 	if err != nil {
-		die("can't create nrsc/nrsc.go - %s", err)
+		die("can't create %s - %s", outfile, err)
 	}
 
-	prefix := len(root)-1
-	for file := range iterfiles(root) {
-		if err := writeResource(prefix, file, out); err != nil {
-			die("can't write %s - %s", file.path, err)
-		}
+	outfile = fmt.Sprintf("%s/data.go", outdir)
+	out, err := os.Create(outfile)
+	if err != nil {
+		die("can't create %s - %s", outfile, err)
 	}
+
+	if err := writeResources(root, out); err != nil {
+		die("%s", err)
+	}
+
 	ok = true
 }

playground/nrsc/nrsc.go

-package nrsc
-
-import (
-	"bytes"
-	"fmt"
-	"io"
-	"net/http"
-	"time"
-)
-
-type Resource interface {
-	Open() io.Reader
-	Size() int
-	Type() string
-	Time() time.Time
-}
-
-var resources map[string]Resource
-
-type resource struct {
-	data  []byte
-	ctype string
-	time  time.Time
-}
-
-func (rsc *resource) Open() io.Reader {
-	return bytes.NewReader(rsc.data)
-}
-
-func (rsc *resource) Size() int {
-	return len(rsc.data)
-}
-
-func (rsc *resource) Type() string {
-	return rsc.ctype
-}
-
-func (rsc *resource) Time() time.Time {
-	return rsc.time
-}
-
-type handler int
-
-func (h handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
-	rsc := resources[req.URL.Path]
-	if rsc == nil {
-		http.NotFound(w, req)
-		return
-	}
-
-	w.Header().Set("Content-Type", rsc.Type())
-	w.Header().Set("Content-Size", fmt.Sprintf("%d", rsc.Size()))
-	w.Header().Set("Last-Modified", rsc.Time().UTC().Format(http.TimeFormat))
-
-	io.Copy(w, rsc.Open())
-}
-
-func Get(path string) Resource {
-	return resources[path]
-}
-
-func Handle(prefix string) {
-	var h handler
-	http.Handle(prefix, http.StripPrefix(prefix, h))
-}
-
-func init() {
-	resources = make(map[string]Resource)
-	resources["hello"] = &resource{
-		[]byte{
-			0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x74,
-			0x68, 0x65, 0x72, 0x65, 0xa,
-		},
-		"text/plain",
-		time.Unix(0, 0),
-	}
-}

playground/resources/ht.txt

+hello there

playground/resources/i.gif

Added
New image