From f69639836bc8e043f6f840a61bb0ce05b0e2db66 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 8 Jun 2017 00:22:57 +0900 Subject: [PATCH] examples: List applications when accessing the top page --- examples/_server/main.go | 54 ++++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/examples/_server/main.go b/examples/_server/main.go index 68ea6a1bb..e1f8de024 100644 --- a/examples/_server/main.go +++ b/examples/_server/main.go @@ -86,30 +86,62 @@ func serveFile(w http.ResponseWriter, path, mime string) error { return nil } -func appName(r *http.Request) (string, error) { - u, err := url.Parse(r.Referer()) +func serveFileHandle(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/" { + http.ServeFile(w, r, filepath.Join(rootPath, r.URL.Path[1:])) + return + } + if r.URL.RawQuery != "" { + if err := serveFile(w, filepath.Join(rootPath, "index.html"), "text/html"); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + return + } + apps := []string{} + fs, err := ioutil.ReadDir(rootPath) if err != nil { - return "", err + http.Error(w, err.Error(), http.StatusInternalServerError) + return } - q := u.RawQuery - if q == "" { - q = "blocks" + for _, f := range fs { + if !f.IsDir() { + continue + } + n := f.Name() + if n[0] == '_' { + continue + } + if n == "common" { + continue + } + apps = append(apps, n) } - return q, nil + w.Header().Set("Content-Type", "text/html") + fmt.Fprintf(w, "") } func serveMainJS(w http.ResponseWriter, r *http.Request) { - name, err := appName(r) + u, err := url.Parse(r.Referer()) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } + name := u.RawQuery + if name == "" { + http.NotFound(w, r) + return + } out, err := createJSIfNeeded(name) if err != nil { t := template.JSEscapeString(template.HTMLEscapeString(err.Error())) js := ` window.onload = function() { - document.body.innerHTML="
` + t + `
"; + document.body.innerHTML="
` + t + `
"; }` w.Header().Set("Content-Type", "text/javascript") fmt.Fprintf(w, js) @@ -128,7 +160,7 @@ func serveMainJSMap(w http.ResponseWriter, r *http.Request) { func main() { http.HandleFunc("/main.js", serveMainJS) http.HandleFunc("/main.js.map", serveMainJSMap) - http.Handle("/", http.FileServer(http.Dir(rootPath))) - fmt.Printf("http://localhost:%d/?blocks\n", *port) + http.HandleFunc("/", serveFileHandle) + fmt.Printf("http://localhost:%d/\n", *port) log.Fatal(http.ListenAndServe(":"+strconv.Itoa(*port), nil)) }