Split into packages only manually tested
This commit is contained in:
24
server/.notes/create.go
Executable file
24
server/.notes/create.go
Executable file
@@ -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)
|
||||
}
|
||||
1
server/.notes/create_test.go
Executable file
1
server/.notes/create_test.go
Executable file
@@ -0,0 +1 @@
|
||||
package notes
|
||||
@@ -1,4 +1,4 @@
|
||||
package server
|
||||
package notes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -1,4 +1,4 @@
|
||||
package server
|
||||
package notes
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
43
server/.notes/edit.go
Executable file
43
server/.notes/edit.go
Executable file
@@ -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, `
|
||||
<form action="/submit/%s" method="post" style="width:100%%; height: 90%%">
|
||||
<table style="width:100%%; height: 90%%">
|
||||
<textarea name="content" style="width:100%%; min-height:90%%">%s</textarea>
|
||||
</table>
|
||||
<button type="submit">Submit</button>
|
||||
</form>
|
||||
`, href, b)
|
||||
}
|
||||
1
server/.notes/edit_test.go
Executable file
1
server/.notes/edit_test.go
Executable file
@@ -0,0 +1 @@
|
||||
package notes
|
||||
@@ -1,4 +1,4 @@
|
||||
package server
|
||||
package notes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -1,4 +1,4 @@
|
||||
package server
|
||||
package notes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
13
server/.notes/notes.go
Normal file
13
server/.notes/notes.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package notes
|
||||
|
||||
import "local/notes-server/config"
|
||||
|
||||
type Notes struct {
|
||||
root string
|
||||
}
|
||||
|
||||
func New() *Notes {
|
||||
return &Notes{
|
||||
root: config.Root,
|
||||
}
|
||||
}
|
||||
27
server/.notes/rnotes.go
Executable file
27
server/.notes/rnotes.go
Executable file
@@ -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()))
|
||||
}
|
||||
1
server/.notes/rnotes_test.go
Executable file
1
server/.notes/rnotes_test.go
Executable file
@@ -0,0 +1 @@
|
||||
package notes
|
||||
31
server/.notes/submit.go
Executable file
31
server/.notes/submit.go
Executable file
@@ -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)
|
||||
}
|
||||
}
|
||||
1
server/.notes/submit_test.go
Executable file
1
server/.notes/submit_test.go
Executable file
@@ -0,0 +1 @@
|
||||
package notes
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
package server
|
||||
@@ -1,19 +0,0 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
)
|
||||
|
||||
type Dirs []Path
|
||||
|
||||
func newDirs() *Dirs {
|
||||
d := Dirs([]Path{})
|
||||
return &d
|
||||
}
|
||||
|
||||
func (d *Dirs) Push(p Path, f os.FileInfo) {
|
||||
if f.IsDir() {
|
||||
*d = append(*d, NewPathFromLocal(path.Join(p.Local, f.Name())))
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
package server
|
||||
@@ -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, `
|
||||
<form action="/submit/%s" method="post" style="width:100%%; height: 90%%">
|
||||
<table style="width:100%%; height: 90%%">
|
||||
<textarea name="content" style="width:100%%; min-height:90%%">%s</textarea>
|
||||
</table>
|
||||
<button type="submit">Submit</button>
|
||||
</form>
|
||||
`, href, b)
|
||||
}
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
package server
|
||||
@@ -1,19 +0,0 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
)
|
||||
|
||||
type Files []Path
|
||||
|
||||
func newFiles() *Files {
|
||||
d := Files([]Path{})
|
||||
return &d
|
||||
}
|
||||
|
||||
func (d *Files) Push(p Path, f os.FileInfo) {
|
||||
if !f.IsDir() {
|
||||
*d = append(*d, NewPathFromLocal(path.Join(p.Local, f.Name())))
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
package server
|
||||
43
server/html.go
Normal file
43
server/html.go
Normal file
@@ -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<div>\n%s\n</div>\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<h%s>\n%s\n</h%s>\n", level, content, level)
|
||||
}
|
||||
@@ -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("<div>.*hi.*<.div>", s); err != nil {
|
||||
t.Fatal(err, s)
|
||||
55
server/notes.go
Executable file → Normal file
55
server/notes.go
Executable file → Normal file
@@ -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, `
|
||||
<form action=%q method="get">
|
||||
<input type="text" name="base"></input>
|
||||
<button type="submit">Create</button>
|
||||
</form>
|
||||
`, 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, `
|
||||
<a href=%q><input type="button" value="Edit"></input></a>
|
||||
`, path.Join("/edit/", baseHREF))
|
||||
}
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
package server
|
||||
@@ -1,92 +0,0 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"local/notes-server/config"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Path struct {
|
||||
Local string
|
||||
HREF string
|
||||
Base string
|
||||
BaseHREF string
|
||||
}
|
||||
|
||||
func NewPathFromLocal(p string) Path {
|
||||
if !strings.HasPrefix(p, "/") {
|
||||
cwd, _ := os.Getwd()
|
||||
p = path.Clean(path.Join(cwd, p))
|
||||
}
|
||||
splits := strings.SplitN(p, path.Base(config.Root), 2)
|
||||
href := splits[0]
|
||||
if len(splits) > 1 && (splits[0] == "" || splits[0] == "/") {
|
||||
href = splits[1]
|
||||
} else {
|
||||
href = strings.Join(splits, path.Base(config.Root))
|
||||
}
|
||||
href = path.Join("/notes", href)
|
||||
return NewPathFromURL(href)
|
||||
}
|
||||
|
||||
func NewPathFromURL(p string) Path {
|
||||
p = path.Clean(p)
|
||||
base := path.Base(p)
|
||||
href := p
|
||||
local := strings.TrimPrefix(p, "/")
|
||||
locals := strings.SplitN(local, "/", 2)
|
||||
local = locals[0]
|
||||
if len(locals) > 1 {
|
||||
local = locals[1]
|
||||
}
|
||||
baseHREF := local
|
||||
local = path.Join(config.Root, local)
|
||||
if strings.Trim(p, "/") == base {
|
||||
local = config.Root
|
||||
baseHREF = "/"
|
||||
}
|
||||
return Path{
|
||||
Base: base,
|
||||
HREF: href,
|
||||
Local: local,
|
||||
BaseHREF: baseHREF,
|
||||
}
|
||||
}
|
||||
|
||||
func (p Path) MultiLink() string {
|
||||
href := p.BaseHREF
|
||||
href = strings.TrimPrefix(href, "/")
|
||||
href = strings.TrimSuffix(href, "/")
|
||||
href = path.Join("notes/", href)
|
||||
segments := strings.Split(href, "/")
|
||||
full := ""
|
||||
for i := range segments {
|
||||
href := "/" + strings.Join(segments[:i], "/") + "/"
|
||||
href += segments[i]
|
||||
href = path.Clean(href)
|
||||
base := segments[i]
|
||||
add := fmt.Sprintf(`/<a href=%q>%s</a>`, href, base)
|
||||
full += add
|
||||
}
|
||||
return full
|
||||
}
|
||||
|
||||
func (p Path) LI() string {
|
||||
return fmt.Sprintf(`<li><a href=%q>%s</a></li>`, p.HREF, p.Base)
|
||||
}
|
||||
|
||||
func (p Path) IsDir() bool {
|
||||
stat, err := os.Stat(p.Local)
|
||||
return err == nil && stat.IsDir()
|
||||
}
|
||||
|
||||
func (p Path) IsFile() bool {
|
||||
stat, err := os.Stat(p.Local)
|
||||
return err == nil && !stat.IsDir()
|
||||
}
|
||||
|
||||
func (p Path) String() string {
|
||||
return fmt.Sprintf(`[Local:%s HREF:%s Base:%s]`, p.Local, p.HREF, p.Base)
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"local/notes-server/config"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPathIs(t *testing.T) {
|
||||
p := Path{Local: "/dev/null"}
|
||||
if ok := p.IsDir(); ok {
|
||||
t.Fatal(ok, p)
|
||||
}
|
||||
|
||||
if ok := p.IsFile(); !ok {
|
||||
t.Fatal(ok, p)
|
||||
}
|
||||
|
||||
p = Path{Local: "/tmp"}
|
||||
if ok := p.IsDir(); !ok {
|
||||
t.Fatal(ok, p)
|
||||
}
|
||||
|
||||
if ok := p.IsFile(); ok {
|
||||
t.Fatal(ok, p)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewPathFromLocal(t *testing.T) {
|
||||
cases := []struct {
|
||||
in string
|
||||
root string
|
||||
href string
|
||||
local string
|
||||
}{
|
||||
{
|
||||
in: "/wiki/b/a.md",
|
||||
root: "/wiki",
|
||||
href: "/notes/b/a.md",
|
||||
local: "/wiki/b/a.md",
|
||||
},
|
||||
{
|
||||
in: "/wiki/a.md",
|
||||
root: "/wiki",
|
||||
href: "/notes/a.md",
|
||||
local: "/wiki/a.md",
|
||||
},
|
||||
{
|
||||
in: "/b/a.md",
|
||||
root: "/",
|
||||
href: "/notes/b/a.md",
|
||||
local: "/b/a.md",
|
||||
},
|
||||
{
|
||||
in: "/a.md",
|
||||
root: "/",
|
||||
href: "/notes/a.md",
|
||||
local: "/a.md",
|
||||
},
|
||||
}
|
||||
|
||||
for i, c := range cases {
|
||||
config.Root = c.root
|
||||
p := NewPathFromLocal(c.in)
|
||||
if p.HREF != c.href {
|
||||
t.Fatal(i, "href", p.HREF, c.href, c, p)
|
||||
}
|
||||
if p.Local != c.local {
|
||||
t.Fatal(i, "local", p.Local, c.local, c, p)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package server
|
||||
|
||||
type Paths []Path
|
||||
|
||||
func (p Paths) List() string {
|
||||
content := "<ul>\n"
|
||||
for _, path := range p {
|
||||
content += path.LI() + "\n"
|
||||
}
|
||||
content += "</ul>\n"
|
||||
return content
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
package server
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<div>\n%s\n</div>\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<h%s>\n%s\n</h%s>\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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
package server
|
||||
Reference in New Issue
Block a user