Skip to content

Instantly share code, notes, and snippets.

@egonelbre
Created October 5, 2015 07:56
Show Gist options
  • Save egonelbre/2cf28c556061a5862c0c to your computer and use it in GitHub Desktop.
Save egonelbre/2cf28c556061a5862c0c to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"path"
"path/filepath"
"strings"
)
var (
addr = flag.String("listen", ":8000", "port to listen to")
tracingdir = flag.String("tracing", ".", "tracing directory")
)
func serve(at string, from string) {
dir := filepath.Join(*tracingdir, filepath.FromSlash(from))
fmt.Println("Serving ", at, " \t-> ", dir)
server := http.StripPrefix(at, http.FileServer(http.Dir(dir)))
http.Handle(at, server)
}
func servefile(at string, path string) {
http.HandleFunc(at, func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, path)
})
}
func istestfile(s string) bool {
return strings.HasSuffix(s, "_test.js") ||
strings.HasSuffix(s, "_test.html") ||
strings.HasSuffix(s, "_unittest.js") ||
strings.HasSuffix(s, "_unittest.html")
}
func main() {
flag.Parse()
fmt.Println("starting on ", *addr)
serve("/tracing/", "tracing")
serve("/tracing_examples/", "tracing_examples")
http.HandleFunc("/tracing/test_data/", func(w http.ResponseWriter, r *http.Request) {
filename := path.Base(r.URL.Path)
fmt.Println(filename)
http.ServeFile(w, r, filepath.Join(*tracingdir, "test_data", filename))
})
http.HandleFunc("/tracing/tests", serveTests)
http.HandleFunc("/tracing/test_data/__file_list__", serveTestData)
http.HandleFunc("/tracing/skp_data/__file_list__", serveSkpData)
servefile("/gl-matrix-min.js", path.Join(*tracingdir, "third_party/gl-matrix/dist/gl-matrix-min.js"))
servefile("/jszip.min.js", path.Join(*tracingdir, "third_party/jszip/jszip.min.js"))
servefile("/d3.min.js", path.Join(*tracingdir, "third_party/d3/d3.min.js"))
serve("/components/", "third_party/components")
serve("/css-element-queries/", "third_party/css-element-queries")
serve("/tvcm/", "third_party/tvcm")
serve("/vinn/", "third_party/vinn")
serve("/jszip/", "third_party/jszip")
serve("/gl-matrix/", "third_party/gl-matrix/dist")
serve("/d3/", "third_party/d3")
serve("/chai/", "third_party/chai")
serve("/mocha/", "third_party/mocha")
serve("/rjsmin/", "third_party/rjsmin")
serve("/rcssmin/", "third_party/rcssmin")
log.Fatal(http.ListenAndServe(*addr, nil))
}
func serveTests(w http.ResponseWriter, r *http.Request) {
var result struct {
Files []string `json:"test_relpaths"`
}
root := filepath.Join(*tracingdir, "tracing")
filepath.Walk(root,
func(name string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && istestfile(name) {
result.Files = append(result.Files, "/"+filepath.ToSlash(name))
}
return nil
})
json.NewEncoder(w).Encode(result)
}
func serveTestData(w http.ResponseWriter, r *http.Request) {
files := []string{}
root := filepath.Join(*tracingdir, "test_data")
filepath.Walk(root,
func(name string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
files = append(files, "/tracing/"+filepath.ToSlash(name))
return nil
})
json.NewEncoder(w).Encode(files)
}
func serveSkpData(w http.ResponseWriter, r *http.Request) {
files := []string{}
root := filepath.Join(*tracingdir, "skp_data")
filepath.Walk(root,
func(name string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
files = append(files, "/tracing/"+filepath.ToSlash(name))
return nil
})
// w.Write([]byte(`["` + files[0] + `"`))
json.NewEncoder(w).Encode(files)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment