From bbdd4919ba41d4db46c954439d12022400cba586 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Thu, 23 Jul 2020 23:21:43 -0600 Subject: [PATCH] Get text files from __files__ dir --- view/files.go | 29 ++++++++++++++++++++++ view/files_test.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++ view/json.go | 6 ++++- 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 view/files.go create mode 100644 view/files_test.go diff --git a/view/files.go b/view/files.go new file mode 100644 index 0000000..b382dcd --- /dev/null +++ b/view/files.go @@ -0,0 +1,29 @@ +package view + +import ( + "local/dndex/config" + "local/dndex/storage" + "net/http" + "path" + "strings" +) + +func files(_ storage.Graph, w http.ResponseWriter, r *http.Request) error { + r.URL.Path = strings.TrimPrefix(r.URL.Path, config.New().FilePrefix) + if len(r.URL.Path) < 2 { + http.NotFound(w, r) + return nil + } + switch r.Method { + case http.MethodGet: + return filesGet(w, r) + default: + http.NotFound(w, r) + return nil + } +} + +func filesGet(w http.ResponseWriter, r *http.Request) error { + http.ServeFile(w, r, path.Join(config.New().FileRoot, r.URL.Path)) + return nil +} diff --git a/view/files_test.go b/view/files_test.go new file mode 100644 index 0000000..8074252 --- /dev/null +++ b/view/files_test.go @@ -0,0 +1,60 @@ +package view + +import ( + "fmt" + "io/ioutil" + "local/dndex/config" + "local/dndex/storage" + "net/http" + "net/http/httptest" + "os" + "path" + "testing" +) + +func TestFiles(t *testing.T) { + os.Args = os.Args[:1] + f, err := ioutil.TempFile(os.TempDir(), "pattern*") + if err != nil { + t.Fatal(err) + } + f.Close() + defer os.Remove(f.Name()) + os.Setenv("DBURI", f.Name()) + + d, err := ioutil.TempDir(os.TempDir(), "pattern*") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(d) + os.Setenv("FILEROOT", d) + + t.Logf("config: %+v", config.New()) + handler := jsonHandler(storage.Graph{}) + + t.Run("get fake file 404", func(t *testing.T) { + r := httptest.NewRequest(http.MethodGet, fmt.Sprintf("%s/fake", config.New().FilePrefix), nil) + w := httptest.NewRecorder() + handler.ServeHTTP(w, r) + if w.Code != http.StatusNotFound { + t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) + } + }) + + t.Run("get text file", func(t *testing.T) { + f, err := ioutil.TempFile(d, "*.txt") + t.Logf("tempFile(%q, *.txt) = %q", d, f.Name()) + if err != nil { + t.Fatal(err) + } + f.Write([]byte("hello, world")) + f.Close() + r := httptest.NewRequest(http.MethodGet, path.Join(config.New().FilePrefix, path.Base(f.Name())), nil) + w := httptest.NewRecorder() + t.Logf("URL = %q", r.URL.String()) + handler.ServeHTTP(w, r) + if w.Code != http.StatusOK { + t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) + } + }) +} diff --git a/view/json.go b/view/json.go index 3e533c5..437c87f 100644 --- a/view/json.go +++ b/view/json.go @@ -29,11 +29,15 @@ func jsonHandler(g storage.Graph) http.Handler { path: "/who/", foo: who, }, + { + path: config.New().FilePrefix, + foo: files, + }, } for _, route := range routes { - path := route.path nopath := strings.TrimRight(route.path, "/") + path := nopath + "/" foo := route.foo mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { if err := foo(g, w, r); err != nil {