diff --git a/TODO b/TODO index 0627d34..79aa09e 100755 --- a/TODO +++ b/TODO @@ -8,3 +8,32 @@ search move auth as flag in router . and ../** as roots cause bugs in listing and loading and linking `create` at root is a 400, base= in URL (when `create` input is empty) + +FTS + https://stackoverflow.com/questions/26709971/could-this-be-more-efficient-in-go + +main test - + - create, + - header + - text box + - submit + - submit target + - edit, + - header + - text box + - submit + - submit target + - dir, + - header + - create + - create target + - list + - note, + - header + - edit + - edit target + - content + - root-> dir, + - root->file, + - dir->dir, + - dir->file diff --git a/server/dirs.go b/filetree/dirs.go similarity index 84% rename from server/dirs.go rename to filetree/dirs.go index f781e89..53972f6 100755 --- a/server/dirs.go +++ b/filetree/dirs.go @@ -1,4 +1,4 @@ -package server +package filetree import ( "os" @@ -7,7 +7,7 @@ import ( type Dirs []Path -func newDirs() *Dirs { +func NewDirs() *Dirs { d := Dirs([]Path{}) return &d } diff --git a/filetree/dirs_test.go b/filetree/dirs_test.go new file mode 100755 index 0000000..ed5c654 --- /dev/null +++ b/filetree/dirs_test.go @@ -0,0 +1 @@ +package filetree diff --git a/server/files.go b/filetree/files.go similarity index 83% rename from server/files.go rename to filetree/files.go index 029d302..045825a 100755 --- a/server/files.go +++ b/filetree/files.go @@ -1,4 +1,4 @@ -package server +package filetree import ( "os" @@ -7,7 +7,7 @@ import ( type Files []Path -func newFiles() *Files { +func NewFiles() *Files { d := Files([]Path{}) return &d } diff --git a/filetree/files_test.go b/filetree/files_test.go new file mode 100755 index 0000000..ed5c654 --- /dev/null +++ b/filetree/files_test.go @@ -0,0 +1 @@ +package filetree diff --git a/server/path.go b/filetree/path.go similarity index 99% rename from server/path.go rename to filetree/path.go index 34ccdca..44292d3 100755 --- a/server/path.go +++ b/filetree/path.go @@ -1,4 +1,4 @@ -package server +package filetree import ( "fmt" diff --git a/server/path_test.go b/filetree/path_test.go similarity index 98% rename from server/path_test.go rename to filetree/path_test.go index 4b4994b..b08a52b 100755 --- a/server/path_test.go +++ b/filetree/path_test.go @@ -1,4 +1,4 @@ -package server +package filetree import ( "local/notes-server/config" diff --git a/server/paths.go b/filetree/paths.go similarity index 90% rename from server/paths.go rename to filetree/paths.go index 022ea5d..5a34cc7 100755 --- a/server/paths.go +++ b/filetree/paths.go @@ -1,4 +1,4 @@ -package server +package filetree type Paths []Path diff --git a/filetree/paths_test.go b/filetree/paths_test.go new file mode 100755 index 0000000..ed5c654 --- /dev/null +++ b/filetree/paths_test.go @@ -0,0 +1 @@ +package filetree diff --git a/notes/create.go b/notes/create.go new file mode 100755 index 0000000..4835569 --- /dev/null +++ b/notes/create.go @@ -0,0 +1,15 @@ +package notes + +import ( + "errors" + "local/notes-server/filetree" + "path" +) + +func (n *Notes) Create(urlPath string) (string, error) { + p := filetree.NewPathFromURL(urlPath) + if p.IsDir() { + return "", errors.New("directory exists") + } + return path.Join("/edit/", p.BaseHREF), nil +} diff --git a/notes/create_test.go b/notes/create_test.go new file mode 100755 index 0000000..6e8cef9 --- /dev/null +++ b/notes/create_test.go @@ -0,0 +1 @@ +package notes diff --git a/notes/dir.go b/notes/dir.go new file mode 100755 index 0000000..b511815 --- /dev/null +++ b/notes/dir.go @@ -0,0 +1,28 @@ +package notes + +import ( + "errors" + "io/ioutil" + "local/notes-server/filetree" +) + +func (n *Notes) Dir(urlPath string) (string, string, error) { + p := filetree.NewPathFromURL(urlPath) + if !p.IsDir() { + return "", "", errors.New("not a dir") + } + dirs, files := n.lsDir(p) + return dirs.List(), files.List(), nil +} + +func (n *Notes) lsDir(path filetree.Path) (filetree.Paths, filetree.Paths) { + dirs := filetree.NewDirs() + files := filetree.NewFiles() + + found, _ := ioutil.ReadDir(path.Local) + for _, f := range found { + dirs.Push(path, f) + files.Push(path, f) + } + return filetree.Paths(*dirs), filetree.Paths(*files) +} diff --git a/notes/dir_test.go b/notes/dir_test.go new file mode 100755 index 0000000..f90332c --- /dev/null +++ b/notes/dir_test.go @@ -0,0 +1,36 @@ +package notes + +import ( + "local/notes-server/config" + "testing" +) + +func TestDir(t *testing.T) { + n := &Notes{} + config.Root = "/" + dirs, files, err := n.Dir("/notes/usr/local") + if err != nil { + t.Fatal(err) + } + if len(dirs) == 0 { + t.Fatal(len(dirs)) + } + if len(files) == 0 { + t.Fatal(len(files)) + } + t.Log(dirs) + t.Log(files) +} + +func TestNotesDir(t *testing.T) { + n := &Notes{} + body, body2, err := n.Dir("/notes/usr/local") + if err != nil { + t.Fatal(err) + } + if body == "" || body2 == "" { + t.Fatal(body, body2) + } + t.Logf("%s", body) + t.Logf("%s", body2) +} diff --git a/notes/edit.go b/notes/edit.go new file mode 100755 index 0000000..0f4e918 --- /dev/null +++ b/notes/edit.go @@ -0,0 +1,36 @@ +package notes + +import ( + "errors" + "fmt" + "io/ioutil" + "local/notes-server/filetree" + "strings" +) + +func (n *Notes) Edit(urlPath string) (string, error) { + p := filetree.NewPathFromURL(urlPath) + if p.IsDir() { + return "", errors.New("path is dir") + } + return editFile(p), nil +} + +func editFile(p filetree.Path) string { + href := p.HREF + href = strings.TrimPrefix(href, "/") + hrefs := strings.SplitN(href, "/", 2) + href = hrefs[0] + if len(hrefs) > 1 { + href = hrefs[1] + } + b, _ := ioutil.ReadFile(p.Local) + return fmt.Sprintf(` +
+ + +
+ +
+ `, href, b) +} diff --git a/notes/edit_test.go b/notes/edit_test.go new file mode 100755 index 0000000..6e8cef9 --- /dev/null +++ b/notes/edit_test.go @@ -0,0 +1 @@ +package notes diff --git a/notes/file.go b/notes/file.go new file mode 100755 index 0000000..f3a6490 --- /dev/null +++ b/notes/file.go @@ -0,0 +1,25 @@ +package notes + +import ( + "errors" + "io/ioutil" + "local/notes-server/filetree" + + "github.com/gomarkdown/markdown" + "github.com/gomarkdown/markdown/html" + "github.com/gomarkdown/markdown/parser" +) + +func (n *Notes) File(urlPath string) (string, error) { + p := filetree.NewPathFromURL(urlPath) + if p.IsDir() { + return "", errors.New("path is dir") + } + b, _ := ioutil.ReadFile(p.Local) + renderer := html.NewRenderer(html.RendererOptions{ + Flags: html.CommonFlags | html.TOC, + }) + parser := parser.NewWithExtensions(parser.CommonExtensions | parser.HeadingIDs | parser.AutoHeadingIDs | parser.Titleblock) + content := markdown.ToHTML(b, parser, renderer) + return string(content) + "\n", nil +} diff --git a/notes/file_test.go b/notes/file_test.go new file mode 100755 index 0000000..7b9ff28 --- /dev/null +++ b/notes/file_test.go @@ -0,0 +1,49 @@ +package notes + +import ( + "fmt" + "io/ioutil" + "local/notes-server/config" + "os" + "path" + "strings" + "testing" +) + +func TestFile(t *testing.T) { + f, err := ioutil.TempFile(os.TempDir(), "until*") + if err != nil { + t.Fatal(err) + } + defer os.Remove(f.Name()) + fmt.Fprintln(f, ` +# Hello +## World +* This +* is +* bullets + +| My | table | goes | +|----|-------|------| +| h | e | n | + + `) + f.Close() + n := &Notes{} + config.Root = "/" + s, err := n.File(path.Join("notes", f.Name())) + if err != nil { + t.Fatal(err) + } + shouldContain := []string{ + "tbody", + "h1", + "h2", + } + for _, should := range shouldContain { + if !strings.Contains(s, should) { + t.Fatalf("%s: %s", should, s) + } + } + t.Logf("%s", s) +} diff --git a/notes/notes.go b/notes/notes.go new file mode 100644 index 0000000..7a676ab --- /dev/null +++ b/notes/notes.go @@ -0,0 +1,13 @@ +package notes + +import "local/notes-server/config" + +type Notes struct { + root string +} + +func New() *Notes { + return &Notes{ + root: config.Root, + } +} diff --git a/notes/submit.go b/notes/submit.go new file mode 100755 index 0000000..178ac54 --- /dev/null +++ b/notes/submit.go @@ -0,0 +1,14 @@ +package notes + +import ( + "io/ioutil" + "local/notes-server/filetree" + "os" + "path" +) + +func (n *Notes) Submit(urlPath, content string) error { + p := filetree.NewPathFromURL(urlPath) + os.MkdirAll(path.Dir(p.Local), os.ModePerm) + return ioutil.WriteFile(p.Local, []byte(content), os.ModePerm) +} diff --git a/notes/submit_test.go b/notes/submit_test.go new file mode 100755 index 0000000..6e8cef9 --- /dev/null +++ b/notes/submit_test.go @@ -0,0 +1 @@ +package notes diff --git a/server/.notes/create.go b/server/.notes/create.go new file mode 100755 index 0000000..ebf6ec1 --- /dev/null +++ b/server/.notes/create.go @@ -0,0 +1,24 @@ +package notes + +import ( + "html" + "local/notes-server/filetree" + "net/http" + "path" + "strings" +) + +func (n *Notes) Create(w http.ResponseWriter, r *http.Request) { + content := r.FormValue("base") + content = html.UnescapeString(content) + content = strings.ReplaceAll(content, "\r", "") + urlPath := path.Join(r.URL.Path, content) + p := filetree.NewPathFromURL(urlPath) + if p.IsDir() { + w.WriteHeader(http.StatusBadRequest) + return + } + url := *r.URL + url.Path = path.Join("/edit/", p.BaseHREF) + http.Redirect(w, r, url.String(), http.StatusSeeOther) +} diff --git a/server/.notes/create_test.go b/server/.notes/create_test.go new file mode 100755 index 0000000..6e8cef9 --- /dev/null +++ b/server/.notes/create_test.go @@ -0,0 +1 @@ +package notes diff --git a/server/dir.go b/server/.notes/dir.go similarity index 97% rename from server/dir.go rename to server/.notes/dir.go index 11dfd52..9812e0c 100755 --- a/server/dir.go +++ b/server/.notes/dir.go @@ -1,4 +1,4 @@ -package server +package notes import ( "fmt" diff --git a/server/dir_test.go b/server/.notes/dir_test.go similarity index 96% rename from server/dir_test.go rename to server/.notes/dir_test.go index dfdacf2..867b686 100755 --- a/server/dir_test.go +++ b/server/.notes/dir_test.go @@ -1,4 +1,4 @@ -package server +package notes import ( "net/http/httptest" diff --git a/server/.notes/edit.go b/server/.notes/edit.go new file mode 100755 index 0000000..794bbdd --- /dev/null +++ b/server/.notes/edit.go @@ -0,0 +1,43 @@ +package notes + +import ( + "fmt" + "io/ioutil" + "net/http" + "strings" +) + +func (s *Server) edit(w http.ResponseWriter, r *http.Request) { + p := NewPathFromURL(r.URL.Path) + if p.IsDir() { + http.NotFound(w, r) + return + } + head(w, r) + editHead(w, p) + editFile(w, p) + foot(w, r) +} + +func editHead(w http.ResponseWriter, p Path) { + fmt.Fprintln(w, h2(p.MultiLink())) +} + +func editFile(w http.ResponseWriter, p Path) { + href := p.HREF + href = strings.TrimPrefix(href, "/") + hrefs := strings.SplitN(href, "/", 2) + href = hrefs[0] + if len(hrefs) > 1 { + href = hrefs[1] + } + b, _ := ioutil.ReadFile(p.Local) + fmt.Fprintf(w, ` +
+ + +
+ +
+ `, href, b) +} diff --git a/server/.notes/edit_test.go b/server/.notes/edit_test.go new file mode 100755 index 0000000..6e8cef9 --- /dev/null +++ b/server/.notes/edit_test.go @@ -0,0 +1 @@ +package notes diff --git a/server/file.go b/server/.notes/file.go similarity index 98% rename from server/file.go rename to server/.notes/file.go index 813d2f2..c372640 100755 --- a/server/file.go +++ b/server/.notes/file.go @@ -1,4 +1,4 @@ -package server +package notes import ( "fmt" diff --git a/server/file_test.go b/server/.notes/file_test.go similarity index 97% rename from server/file_test.go rename to server/.notes/file_test.go index 659579e..3d22e3e 100755 --- a/server/file_test.go +++ b/server/.notes/file_test.go @@ -1,4 +1,4 @@ -package server +package notes import ( "fmt" diff --git a/server/.notes/notes.go b/server/.notes/notes.go new file mode 100644 index 0000000..7a676ab --- /dev/null +++ b/server/.notes/notes.go @@ -0,0 +1,13 @@ +package notes + +import "local/notes-server/config" + +type Notes struct { + root string +} + +func New() *Notes { + return &Notes{ + root: config.Root, + } +} diff --git a/server/.notes/rnotes.go b/server/.notes/rnotes.go new file mode 100755 index 0000000..7518bfd --- /dev/null +++ b/server/.notes/rnotes.go @@ -0,0 +1,27 @@ +package notes + +import ( + "fmt" + "net/http" +) + +func (s *Server) notes(w http.ResponseWriter, r *http.Request) { + p := NewPathFromURL(r.URL.Path) + if p.IsDir() { + head(w, r) + notesHead(w, p) + notesDir(p, w, r) + foot(w, r) + } else if p.IsFile() { + head(w, r) + notesHead(w, p) + notesFile(p, w, r) + foot(w, r) + } else { + http.NotFound(w, r) + } +} + +func notesHead(w http.ResponseWriter, p Path) { + fmt.Fprintln(w, h2(p.MultiLink())) +} diff --git a/server/.notes/rnotes_test.go b/server/.notes/rnotes_test.go new file mode 100755 index 0000000..6e8cef9 --- /dev/null +++ b/server/.notes/rnotes_test.go @@ -0,0 +1 @@ +package notes diff --git a/server/.notes/submit.go b/server/.notes/submit.go new file mode 100755 index 0000000..fc53014 --- /dev/null +++ b/server/.notes/submit.go @@ -0,0 +1,31 @@ +package notes + +import ( + "fmt" + "html" + "io/ioutil" + "net/http" + "os" + "path" + "strings" +) + +func (s *Server) submit(w http.ResponseWriter, r *http.Request) { + if r.Method != "POST" { + http.NotFound(w, r) + return + } + content := r.FormValue("content") + content = html.UnescapeString(content) + content = strings.ReplaceAll(content, "\r", "") + p := NewPathFromURL(r.URL.Path) + os.MkdirAll(path.Dir(p.Local), os.ModePerm) + if err := ioutil.WriteFile(p.Local, []byte(content), os.ModePerm); err != nil { + w.WriteHeader(http.StatusInternalServerError) + fmt.Fprintln(w, err) + } else { + url := *r.URL + url.Path = path.Join("/notes/", p.BaseHREF) + http.Redirect(w, r, url.String(), http.StatusSeeOther) + } +} diff --git a/server/.notes/submit_test.go b/server/.notes/submit_test.go new file mode 100755 index 0000000..6e8cef9 --- /dev/null +++ b/server/.notes/submit_test.go @@ -0,0 +1 @@ +package notes diff --git a/server/create.go b/server/create.go index fb9898d..e9da5aa 100755 --- a/server/create.go +++ b/server/create.go @@ -12,12 +12,12 @@ func (s *Server) create(w http.ResponseWriter, r *http.Request) { content = html.UnescapeString(content) content = strings.ReplaceAll(content, "\r", "") urlPath := path.Join(r.URL.Path, content) - p := NewPathFromURL(urlPath) - if p.IsDir() { - w.WriteHeader(http.StatusBadRequest) + url := *r.URL + path, err := s.Notes.Create(urlPath) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) return } - url := *r.URL - url.Path = path.Join("/edit/", p.BaseHREF) + url.Path = path http.Redirect(w, r, url.String(), http.StatusSeeOther) } diff --git a/server/create_test.go b/server/create_test.go deleted file mode 100755 index abb4e43..0000000 --- a/server/create_test.go +++ /dev/null @@ -1 +0,0 @@ -package server diff --git a/server/dirs_test.go b/server/dirs_test.go deleted file mode 100755 index abb4e43..0000000 --- a/server/dirs_test.go +++ /dev/null @@ -1 +0,0 @@ -package server diff --git a/server/edit.go b/server/edit.go index ddecd98..d86731b 100755 --- a/server/edit.go +++ b/server/edit.go @@ -2,42 +2,22 @@ package server import ( "fmt" - "io/ioutil" + "local/notes-server/filetree" "net/http" - "strings" ) func (s *Server) edit(w http.ResponseWriter, r *http.Request) { - p := NewPathFromURL(r.URL.Path) - if p.IsDir() { - http.NotFound(w, r) + head(w, r) + editHead(w, filetree.NewPathFromURL(r.URL.Path)) + edit, err := s.Notes.Edit(r.URL.Path) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) return } - head(w, r) - editHead(w, p) - editFile(w, p) + fmt.Fprintln(w, edit) foot(w, r) } -func editHead(w http.ResponseWriter, p Path) { +func editHead(w http.ResponseWriter, p filetree.Path) { fmt.Fprintln(w, h2(p.MultiLink())) } - -func editFile(w http.ResponseWriter, p Path) { - href := p.HREF - href = strings.TrimPrefix(href, "/") - hrefs := strings.SplitN(href, "/", 2) - href = hrefs[0] - if len(hrefs) > 1 { - href = hrefs[1] - } - b, _ := ioutil.ReadFile(p.Local) - fmt.Fprintf(w, ` -
- - -
- -
- `, href, b) -} diff --git a/server/edit_test.go b/server/edit_test.go deleted file mode 100755 index abb4e43..0000000 --- a/server/edit_test.go +++ /dev/null @@ -1 +0,0 @@ -package server diff --git a/server/files_test.go b/server/files_test.go deleted file mode 100755 index abb4e43..0000000 --- a/server/files_test.go +++ /dev/null @@ -1 +0,0 @@ -package server diff --git a/server/html.go b/server/html.go new file mode 100644 index 0000000..1bb8f68 --- /dev/null +++ b/server/html.go @@ -0,0 +1,43 @@ +package server + +import ( + "fmt" + "local/notes-server/config" + "net/http" +) + +func head(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(config.Head + "\n")) +} + +func foot(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(config.Foot + "\n")) +} + +func block(w http.ResponseWriter, content string) { + fmt.Fprintf(w, "\n
\n%s\n
\n", content) +} + +func h1(content string) string { + return h("1", content) +} + +func h2(content string) string { + return h("2", content) +} + +func h3(content string) string { + return h("3", content) +} + +func h4(content string) string { + return h("4", content) +} + +func h5(content string) string { + return h("5", content) +} + +func h(level, content string) string { + return fmt.Sprintf("\n\n%s\n\n", level, content, level) +} diff --git a/server/server_test.go b/server/html_test.go similarity index 98% rename from server/server_test.go rename to server/html_test.go index 50afffa..5f32bd1 100755 --- a/server/server_test.go +++ b/server/html_test.go @@ -28,7 +28,7 @@ func TestFoot(t *testing.T) { func TestBlock(t *testing.T) { w := httptest.NewRecorder() - block("hi", w) + block(w, "hi") s := strings.ReplaceAll(strings.TrimSpace(string(w.Body.Bytes())), "\n", ".") if ok, err := regexp.MatchString("
.*hi.*<.div>", s); err != nil { t.Fatal(err, s) diff --git a/server/notes.go b/server/notes.go old mode 100755 new mode 100644 index b0b811f..a76de72 --- a/server/notes.go +++ b/server/notes.go @@ -2,26 +2,61 @@ package server import ( "fmt" + "local/notes-server/filetree" "net/http" + "path" ) func (s *Server) notes(w http.ResponseWriter, r *http.Request) { - p := NewPathFromURL(r.URL.Path) + p := filetree.NewPathFromURL(r.URL.Path) + head(w, r) + notesHead(w, p) + defer foot(w, r) if p.IsDir() { - head(w, r) - notesHead(w, p) - notesDir(p, w, r) - foot(w, r) + s.dir(w, r) } else if p.IsFile() { - head(w, r) - notesHead(w, p) - notesFile(p, w, r) - foot(w, r) + s.file(w, r) } else { http.NotFound(w, r) } } -func notesHead(w http.ResponseWriter, p Path) { +func notesHead(w http.ResponseWriter, p filetree.Path) { fmt.Fprintln(w, h2(p.MultiLink())) } + +func (s *Server) dir(w http.ResponseWriter, r *http.Request) { + dirs, files, err := s.Notes.Dir(r.URL.Path) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + dirHead(w, filetree.NewPathFromURL(r.URL.Path).BaseHREF) + block(w, dirs) + block(w, files) +} + +func dirHead(w http.ResponseWriter, baseHREF string) { + fmt.Fprintf(w, ` +
+ + +
+ `, path.Join("/create/", baseHREF)) +} + +func (s *Server) file(w http.ResponseWriter, r *http.Request) { + file, err := s.Notes.File(r.URL.Path) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + fileHead(w, filetree.NewPathFromURL(r.URL.Path).BaseHREF) + fmt.Fprintln(w, file) +} + +func fileHead(w http.ResponseWriter, baseHREF string) { + fmt.Fprintf(w, ` + + `, path.Join("/edit/", baseHREF)) +} diff --git a/server/notes_test.go b/server/notes_test.go deleted file mode 100755 index abb4e43..0000000 --- a/server/notes_test.go +++ /dev/null @@ -1 +0,0 @@ -package server diff --git a/server/paths_test.go b/server/paths_test.go deleted file mode 100755 index abb4e43..0000000 --- a/server/paths_test.go +++ /dev/null @@ -1 +0,0 @@ -package server diff --git a/server/routes.go b/server/routes.go index 8b853ee..8496fdc 100755 --- a/server/routes.go +++ b/server/routes.go @@ -2,10 +2,7 @@ package server import ( "fmt" - "local/notes-server/config" - "local/oauth2/oauth2client" "local/router" - "log" "net/http" ) @@ -40,16 +37,3 @@ func (s *Server) Routes() error { } return nil } - -func (s *Server) authenticate(foo http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - if config.OAuthServer != "" { - err := oauth2client.Authenticate(config.OAuthServer, "notes-server", w, r) - if err != nil { - log.Println(err) - return - } - } - foo(w, r) - } -} diff --git a/server/server.go b/server/server.go index efd72ff..f2a60e9 100755 --- a/server/server.go +++ b/server/server.go @@ -1,54 +1,35 @@ package server import ( - "fmt" "local/notes-server/config" + "local/notes-server/notes" + "local/oauth2/oauth2client" "local/router" + "log" "net/http" ) type Server struct { *router.Router + Notes *notes.Notes } func New() *Server { return &Server{ Router: router.New(), + Notes: notes.New(), } } -func head(w http.ResponseWriter, r *http.Request) { - w.Write([]byte(config.Head + "\n")) -} - -func foot(w http.ResponseWriter, r *http.Request) { - w.Write([]byte(config.Foot + "\n")) -} - -func block(content string, w http.ResponseWriter) { - fmt.Fprintf(w, "\n
\n%s\n
\n", content) -} - -func h1(content string) string { - return h("1", content) -} - -func h2(content string) string { - return h("2", content) -} - -func h3(content string) string { - return h("3", content) -} - -func h4(content string) string { - return h("4", content) -} - -func h5(content string) string { - return h("5", content) -} - -func h(level, content string) string { - return fmt.Sprintf("\n\n%s\n\n", level, content, level) +func (s *Server) authenticate(foo http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if config.OAuthServer != "" { + err := oauth2client.Authenticate(config.OAuthServer, "notes-server", w, r) + if err != nil { + log.Println(err) + return + } + } + foo(w, r) + } } diff --git a/server/submit.go b/server/submit.go index a7f3cff..4bb7c29 100755 --- a/server/submit.go +++ b/server/submit.go @@ -1,11 +1,9 @@ package server import ( - "fmt" "html" - "io/ioutil" + "local/notes-server/filetree" "net/http" - "os" "path" "strings" ) @@ -18,14 +16,12 @@ func (s *Server) submit(w http.ResponseWriter, r *http.Request) { content := r.FormValue("content") content = html.UnescapeString(content) content = strings.ReplaceAll(content, "\r", "") - p := NewPathFromURL(r.URL.Path) - os.MkdirAll(path.Dir(p.Local), os.ModePerm) - if err := ioutil.WriteFile(p.Local, []byte(content), os.ModePerm); err != nil { - w.WriteHeader(http.StatusInternalServerError) - fmt.Fprintln(w, err) - } else { - url := *r.URL - url.Path = path.Join("/notes/", p.BaseHREF) - http.Redirect(w, r, url.String(), http.StatusSeeOther) + err := s.Notes.Submit(r.URL.Path, content) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return } + url := *r.URL + url.Path = path.Join("/notes/", filetree.NewPathFromURL(r.URL.Path).BaseHREF) + http.Redirect(w, r, url.String(), http.StatusSeeOther) } diff --git a/server/submit_test.go b/server/submit_test.go deleted file mode 100755 index abb4e43..0000000 --- a/server/submit_test.go +++ /dev/null @@ -1 +0,0 @@ -package server