Compare commits
32 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
563eb7bb61 | ||
|
|
b7f13bf33d | ||
|
|
d73cbe9e0c | ||
|
|
f582410c40 | ||
|
|
f53fc80f68 | ||
|
|
16335d796b | ||
|
|
a7df97aae5 | ||
|
|
4a7efd4016 | ||
|
|
5be556a4cf | ||
|
|
72894cd5cc | ||
|
|
f0a1c21678 | ||
|
|
807072a77f | ||
|
|
081d50328f | ||
|
|
4e2b7b3c85 | ||
|
|
e4632d36ba | ||
|
|
fecd343f1b | ||
|
|
217a221cf4 | ||
|
|
b73a962556 | ||
|
|
c88fed0929 | ||
|
|
a140d0eade | ||
|
|
3079cd163f | ||
|
|
9fc4e63a34 | ||
|
|
1b9067a72d | ||
|
|
e6f63a578f | ||
|
|
7964518d36 | ||
|
|
259a8efc70 | ||
|
|
60dc6bc876 | ||
|
|
0d497f4fa8 | ||
|
|
c6d43699ef | ||
|
|
618b9ed513 | ||
|
|
0595c49fc2 | ||
|
|
da6eaca26f |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,4 +1,5 @@
|
||||
gollum
|
||||
public
|
||||
**.sw*
|
||||
**/**.sw*
|
||||
*.sw*
|
||||
|
||||
38
TODO
38
TODO
@@ -1,7 +1,39 @@
|
||||
x edit page
|
||||
x create page
|
||||
x create dir
|
||||
x 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
|
||||
|
||||
TOC levels
|
||||
delete pages
|
||||
search
|
||||
x TOC levels
|
||||
x delete pages
|
||||
x search
|
||||
x FTS
|
||||
https://stackoverflow.com/questions/26709971/could-this-be-more-efficient-in-go
|
||||
x move auth as flag in router
|
||||
x . and ../** as roots cause bugs in listing and loading and linking
|
||||
x `create` at root is a 400, base= in URL (when `create` input is empty)
|
||||
x versioning
|
||||
delete top-level pages
|
||||
|
||||
@@ -7,7 +7,9 @@ import (
|
||||
"local/args"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -15,6 +17,8 @@ var (
|
||||
Port string
|
||||
Head string
|
||||
Foot string
|
||||
OAuthServer string
|
||||
VersionInterval time.Duration
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -22,31 +26,76 @@ func init() {
|
||||
}
|
||||
|
||||
func Refresh() {
|
||||
if strings.Contains(fmt.Sprint(os.Args), "-test") {
|
||||
if strings.Contains(fmt.Sprint(os.Args), " -test") {
|
||||
return
|
||||
}
|
||||
|
||||
as := args.NewArgSet()
|
||||
as.Append(args.STRING, "root", "root dir path", "./public")
|
||||
as.Append(args.STRING, "port", "port to listen on", "39909")
|
||||
as.Append(args.STRING, "wrap", "file with http header/footer", "./wrapper.html")
|
||||
as.Append(args.STRING, "port", "port to listen on", "49909")
|
||||
as.Append(args.STRING, "wrap", "file with http header/footer", "")
|
||||
as.Append(args.STRING, "oauth", "oauth URL", "")
|
||||
as.Append(args.DURATION, "version", "duration to mark versions", "0s")
|
||||
if err := as.Parse(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
wrap := as.Get("wrap").GetString()
|
||||
var b []byte
|
||||
if len(wrap) > 0 {
|
||||
log.Printf("reading %v (%T)", wrap, wrap)
|
||||
b, err := ioutil.ReadFile(wrap)
|
||||
var err error
|
||||
b, err = ioutil.ReadFile(wrap)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else {
|
||||
b = []byte(defaultWrapper)
|
||||
}
|
||||
bs := bytes.Split(b, []byte("{{{}}}"))
|
||||
if len(bs) != 2 {
|
||||
panic(len(bs))
|
||||
}
|
||||
|
||||
Root = as.Get("root").GetString()
|
||||
Root = strings.TrimSuffix(as.Get("root").GetString(), "/")
|
||||
Root, _ = filepath.Abs(Root)
|
||||
Port = ":" + strings.TrimPrefix(as.Get("port").GetString(), ":")
|
||||
Head = string(bs[0])
|
||||
Foot = string(bs[1])
|
||||
OAuthServer = as.Get("oauth").GetString()
|
||||
VersionInterval = as.Get("version").GetDuration()
|
||||
}
|
||||
|
||||
const defaultWrapper = `
|
||||
<html>
|
||||
<header>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<!-- https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/dark.min.css -->
|
||||
<style>
|
||||
@charset "UTF-8";body{font-family:system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu,Cantarell,Fira Sans,Droid Sans,Helvetica Neue,sans-serif;line-height:1.4;max-width:800px;margin:20px auto;padding:0 10px;color:#dbdbdb;background:#202b38;text-rendering:optimizeLegibility}button,input,textarea{transition:background-color .1s linear,border-color .1s linear,color .1s linear,box-shadow .1s linear,transform .1s ease}h1{font-size:2.2em;margin-top:0}h1,h2,h3,h4,h5,h6{margin-bottom:12px}h1,h2,h3,h4,h5,h6,strong{color:#fff}b,h1,h2,h3,h4,h5,h6,strong,th{font-weight:600}blockquote{border-left:4px solid rgba(0,150,191,.67);margin:1.5em 0;padding:.5em 1em;font-style:italic}blockquote>footer{margin-top:10px;font-style:normal}address,blockquote cite{font-style:normal}a[href^=mailto]:before{content:"📧 "}a[href^=tel]:before{content:"📞 "}a[href^=sms]:before{content:"💬 "}button,input[type=button],input[type=checkbox],input[type=submit]{cursor:pointer}input:not([type=checkbox]):not([type=radio]),select{display:block}button,input,select,textarea{color:#fff;background-color:#161f27;font-family:inherit;font-size:inherit;margin-right:6px;margin-bottom:6px;padding:10px;border:none;border-radius:6px;outline:none}button,input:not([type=checkbox]):not([type=radio]),select,textarea{-webkit-appearance:none}textarea{margin-right:0;width:100%;box-sizing:border-box;resize:vertical}button,input[type=button],input[type=submit]{padding-right:30px;padding-left:30px}button:hover,input[type=button]:hover,input[type=submit]:hover{background:#324759}button:focus,input:focus,select:focus,textarea:focus{box-shadow:0 0 0 2px rgba(0,150,191,.67)}button:active,input[type=button]:active,input[type=checkbox]:active,input[type=radio]:active,input[type=submit]:active{transform:translateY(2px)}button:disabled,input:disabled,select:disabled,textarea:disabled{cursor:not-allowed;opacity:.5}::-webkit-input-placeholder{color:#a9a9a9}:-ms-input-placeholder{color:#a9a9a9}::-ms-input-placeholder{color:#a9a9a9}::placeholder{color:#a9a9a9}a{text-decoration:none;color:#41adff}a:hover{text-decoration:underline}code,kbd{background:#161f27;color:#ffbe85;padding:5px;border-radius:6px}pre>code{padding:10px;display:block;overflow-x:auto}img{max-width:100%}hr{border:none;border-top:1px solid #dbdbdb}table{border-collapse:collapse;margin-bottom:10px;width:100%}td,th{padding:6px;text-align:left}th{border-bottom:1px solid #dbdbdb}tbody tr:nth-child(2n){background-color:#161f27}::-webkit-scrollbar{height:10px;width:10px}::-webkit-scrollbar-track{background:#161f27;border-radius:6px}::-webkit-scrollbar-thumb{background:#324759;border-radius:6px}::-webkit-scrollbar-thumb:hover{background:#415c73}
|
||||
</style>
|
||||
<style>
|
||||
nav {
|
||||
display: block;
|
||||
background: #161f27;
|
||||
padding: .5pt;
|
||||
border-radius: 6px;
|
||||
}
|
||||
nav li li li li {
|
||||
display: none;
|
||||
}
|
||||
img {
|
||||
max-height: 400px;
|
||||
}
|
||||
body {
|
||||
font-size: 125%;
|
||||
}
|
||||
</style>
|
||||
</header>
|
||||
<body height="100%">
|
||||
{{{}}}
|
||||
</body>
|
||||
<footer>
|
||||
</footer>
|
||||
</html>
|
||||
`
|
||||
|
||||
@@ -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
|
||||
}
|
||||
28
filetree/dirs_test.go
Executable file
28
filetree/dirs_test.go
Executable file
@@ -0,0 +1,28 @@
|
||||
package filetree
|
||||
|
||||
import (
|
||||
"local/notes-server/config"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDirs(t *testing.T) {
|
||||
config.Root = "/"
|
||||
dirs := NewDirs()
|
||||
info, err := os.Stat("/usr/local")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p := NewPathFromLocal("/usr")
|
||||
dirs.Push(p, info)
|
||||
if len([]Path(*dirs)) != 1 {
|
||||
t.Error(dirs)
|
||||
}
|
||||
first := []Path(*dirs)[0]
|
||||
if first.Base != "local" {
|
||||
t.Error(first)
|
||||
}
|
||||
if first.Local != "/usr/local" {
|
||||
t.Error(first)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
28
filetree/files_test.go
Executable file
28
filetree/files_test.go
Executable file
@@ -0,0 +1,28 @@
|
||||
package filetree
|
||||
|
||||
import (
|
||||
"local/notes-server/config"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFiles(t *testing.T) {
|
||||
config.Root = "/"
|
||||
files := NewFiles()
|
||||
info, err := os.Stat("/etc/hosts")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p := NewPathFromLocal("/etc")
|
||||
files.Push(p, info)
|
||||
if len([]Path(*files)) != 1 {
|
||||
t.Error(files)
|
||||
}
|
||||
first := []Path(*files)[0]
|
||||
if first.Base != "hosts" {
|
||||
t.Error(first)
|
||||
}
|
||||
if first.Local != "/etc/hosts" {
|
||||
t.Error(first)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package server
|
||||
package filetree
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -16,9 +16,24 @@ type Path struct {
|
||||
}
|
||||
|
||||
func NewPathFromLocal(p string) Path {
|
||||
splits := strings.SplitN(p, path.Base(config.Root), 2)
|
||||
root := config.Root + "/"
|
||||
if strings.HasPrefix(root, "./") {
|
||||
root = root[2:]
|
||||
}
|
||||
if strings.HasSuffix(root, "/") {
|
||||
root = root[:len(root)-1]
|
||||
}
|
||||
splits := strings.SplitN(p, root, 2)
|
||||
for len(splits) > 0 && splits[0] == "" {
|
||||
splits = splits[1:]
|
||||
}
|
||||
if len(splits) == 0 {
|
||||
splits = []string{"", ""}
|
||||
}
|
||||
href := splits[0]
|
||||
if len(splits) > 1 {
|
||||
if len(splits) == 1 && (splits[0] == root || splits[0] == config.Root) {
|
||||
href = ""
|
||||
} else if splits[0] == "" || splits[0] == "/" {
|
||||
href = splits[1]
|
||||
}
|
||||
href = path.Join("/notes", href)
|
||||
@@ -67,6 +82,10 @@ func (p Path) MultiLink() string {
|
||||
return full
|
||||
}
|
||||
|
||||
func (p Path) FullLI() string {
|
||||
return fmt.Sprintf(`<li><a href=%q>%s</a></li>`, p.HREF, p.HREF)
|
||||
}
|
||||
|
||||
func (p Path) LI() string {
|
||||
return fmt.Sprintf(`<li><a href=%q>%s</a></li>`, p.HREF, p.Base)
|
||||
}
|
||||
77
filetree/path_test.go
Executable file
77
filetree/path_test.go
Executable file
@@ -0,0 +1,77 @@
|
||||
package filetree
|
||||
|
||||
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/wiki/b/a.md",
|
||||
root: "/wiki/wiki",
|
||||
href: "/notes/b/a.md",
|
||||
local: "/wiki/wiki/b/a.md",
|
||||
},
|
||||
{
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
19
filetree/paths.go
Executable file
19
filetree/paths.go
Executable file
@@ -0,0 +1,19 @@
|
||||
package filetree
|
||||
|
||||
type Paths []Path
|
||||
|
||||
func (p Paths) List(full ...bool) string {
|
||||
content := "<ul>\n"
|
||||
for _, path := range p {
|
||||
if len(path.Base) > 0 && path.Base[0] == '.' {
|
||||
continue
|
||||
}
|
||||
if len(full) > 0 && full[0] {
|
||||
content += path.FullLI() + "\n"
|
||||
} else {
|
||||
content += path.LI() + "\n"
|
||||
}
|
||||
}
|
||||
content += "</ul>\n"
|
||||
return content
|
||||
}
|
||||
30
filetree/paths_test.go
Executable file
30
filetree/paths_test.go
Executable file
@@ -0,0 +1,30 @@
|
||||
package filetree
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPaths(t *testing.T) {
|
||||
paths := Paths([]Path{
|
||||
NewPathFromURL("/notes/a/b"),
|
||||
NewPathFromURL("/notes/c/d"),
|
||||
NewPathFromURL("/notes/e/f"),
|
||||
})
|
||||
|
||||
list := paths.List()
|
||||
if strings.Count(list, "<li>") != 3 {
|
||||
t.Error(list)
|
||||
}
|
||||
if !strings.Contains(list, ">f") {
|
||||
t.Error(list)
|
||||
}
|
||||
|
||||
list = paths.List(true)
|
||||
if strings.Count(list, "<li>") != 3 {
|
||||
t.Error(list)
|
||||
}
|
||||
if !strings.Contains(list, ">/notes/a") {
|
||||
t.Error(list)
|
||||
}
|
||||
}
|
||||
16
main.go
16
main.go
@@ -3,10 +3,12 @@ package main
|
||||
import (
|
||||
"local/notes-server/config"
|
||||
"local/notes-server/server"
|
||||
"local/notes-server/versions"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -15,6 +17,20 @@ func main() {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
if config.VersionInterval == 0 {
|
||||
log.Println("versions disabled")
|
||||
return
|
||||
}
|
||||
versions, err := versions.New()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for _ = range time.NewTicker(config.VersionInterval).C {
|
||||
log.Println(versions.Gitmmit())
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
log.Printf("Serving %q on %q", config.Root, config.Port)
|
||||
if err := http.ListenAndServe(config.Port, server); err != nil {
|
||||
|
||||
245
main_test.go
Executable file
245
main_test.go
Executable file
@@ -0,0 +1,245 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"local/notes-server/config"
|
||||
"local/notes-server/server"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAll(t *testing.T) {
|
||||
for _, basedir := range []string{os.TempDir(), "./tempDir"} {
|
||||
os.MkdirAll(basedir, os.ModePerm)
|
||||
makeFiles(t, basedir)
|
||||
if basedir[0] == '.' && config.Root[0] != '.' {
|
||||
config.Root = "./" + config.Root
|
||||
}
|
||||
defer os.RemoveAll(config.Root)
|
||||
log.Println(config.Root)
|
||||
t.Log("trying with root", config.Root)
|
||||
s := makeServer(t)
|
||||
defer s.Close()
|
||||
testServer(t, s.URL)
|
||||
if basedir[0] == '.' {
|
||||
os.RemoveAll(basedir)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func makeFiles(t *testing.T, basedir string) {
|
||||
d, err := ioutil.TempDir(basedir, "pattern*")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
config.Root = d
|
||||
for _, dir := range []string{"dirA", "dirB", "."} {
|
||||
if err := os.MkdirAll(path.Join(d, dir), os.ModePerm); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, file := range []string{"fileA", "fileB"} {
|
||||
content := fmt.Sprintf("hello from %s/%s/%s", d, dir, file)
|
||||
err := ioutil.WriteFile(
|
||||
path.Join(d, dir, file),
|
||||
[]byte(content),
|
||||
os.ModePerm,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func makeServer(t *testing.T) *httptest.Server {
|
||||
s := server.New()
|
||||
if err := s.Routes(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return httptest.NewServer(s)
|
||||
}
|
||||
|
||||
func testServer(t *testing.T, url string) {
|
||||
testCreate(t, url)
|
||||
testEdit(t, url)
|
||||
testDir(t, url)
|
||||
testFile(t, url)
|
||||
testNavRootDir(t, url)
|
||||
testNavRootFile(t, url)
|
||||
testNavDirFile(t, url)
|
||||
}
|
||||
|
||||
func testCreate(t *testing.T, url string) {
|
||||
for _, path := range []string{"dirX/fileX", "fileX"} {
|
||||
resp, err := http.Get(url + "/create/" + path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatal(resp.StatusCode)
|
||||
}
|
||||
b, _ := ioutil.ReadAll(resp.Body)
|
||||
s := string(b)
|
||||
if ok := assertHasMultilink(s, path); !ok {
|
||||
t.Error(ok)
|
||||
}
|
||||
if ok := assertHasForm(s, "/submit/"+path); !ok {
|
||||
t.Error(ok)
|
||||
}
|
||||
if ok := assertHasTextArea(s); !ok {
|
||||
t.Error(ok)
|
||||
}
|
||||
if ok := assertHasSubmit(s); !ok {
|
||||
t.Error(ok)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testEdit(t *testing.T, url string) {
|
||||
for _, path := range []string{"dirX/fileX", "fileX"} {
|
||||
resp, err := http.Get(url + "/edit/" + path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatal(resp.StatusCode)
|
||||
}
|
||||
b, _ := ioutil.ReadAll(resp.Body)
|
||||
s := string(b)
|
||||
if ok := assertHasMultilink(s, path); !ok {
|
||||
t.Error(ok)
|
||||
}
|
||||
if ok := assertHasForm(s); !ok {
|
||||
t.Error(ok)
|
||||
}
|
||||
if ok := assertHasTextArea(s); !ok {
|
||||
t.Error(ok)
|
||||
}
|
||||
if ok := assertHasSubmit(s); !ok {
|
||||
t.Error(ok)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testDir(t *testing.T, url string) {
|
||||
path := url + "/notes/dirA"
|
||||
resp, err := http.Get(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatal(resp.StatusCode)
|
||||
}
|
||||
b, _ := ioutil.ReadAll(resp.Body)
|
||||
s := string(b)
|
||||
if ok := assertHasMultilink(s, "/notes/dirA"); !ok {
|
||||
t.Error(ok)
|
||||
}
|
||||
if ok := assertHasForm(s, "/create/dirA"); !ok {
|
||||
t.Error(ok)
|
||||
}
|
||||
if ok := assertHasSubmit(s); !ok {
|
||||
t.Error(ok)
|
||||
}
|
||||
}
|
||||
|
||||
func testFile(t *testing.T, url string) {
|
||||
path := url + "/notes/dirA/fileA"
|
||||
resp, err := http.Get(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatal(resp.StatusCode)
|
||||
}
|
||||
b, _ := ioutil.ReadAll(resp.Body)
|
||||
s := string(b)
|
||||
if ok := assertHasMultilink(s, "/notes/dirA", "/notes/dirA/fileA"); !ok {
|
||||
t.Error(ok)
|
||||
}
|
||||
if ok := assertHasHref(s, "/edit/dirA/fileA"); !ok {
|
||||
t.Error(ok)
|
||||
}
|
||||
}
|
||||
|
||||
func testNavRootDir(t *testing.T, url string) {
|
||||
resp, err := http.Get(url + "/notes")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b, _ := ioutil.ReadAll(resp.Body)
|
||||
s := string(b)
|
||||
if ok := assertHasHref(s, "/notes/dirA"); !ok {
|
||||
t.Fatal(s)
|
||||
}
|
||||
}
|
||||
|
||||
func testNavRootFile(t *testing.T, url string) {
|
||||
resp, err := http.Get(url + "/notes")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b, _ := ioutil.ReadAll(resp.Body)
|
||||
s := string(b)
|
||||
if ok := assertHasHref(s, "/notes/fileA"); !ok {
|
||||
t.Fatal(s)
|
||||
}
|
||||
}
|
||||
|
||||
func testNavDirFile(t *testing.T, url string) {
|
||||
resp, err := http.Get(url + "/notes/dirA")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b, _ := ioutil.ReadAll(resp.Body)
|
||||
s := string(b)
|
||||
if ok := assertHasHref(s, "/notes/dirA/fileA"); !ok {
|
||||
t.Fatal(s)
|
||||
}
|
||||
}
|
||||
|
||||
func assertHasMultilink(body string, segments ...string) bool {
|
||||
if !strings.Contains(body, `/<a href="/notes">notes</a>/`) {
|
||||
return false
|
||||
}
|
||||
for i := range segments {
|
||||
segments[i] = "/notes/" + strings.TrimPrefix(segments[i], "/notes/")
|
||||
}
|
||||
return assertHasHref(body, segments...)
|
||||
}
|
||||
|
||||
func assertHasForm(body string, action ...string) bool {
|
||||
return strings.Contains(body, `<form`) && (len(action) == 0 || strings.Contains(body, `action="`))
|
||||
}
|
||||
|
||||
func assertHasTextArea(body string) bool {
|
||||
return strings.Contains(body, `<textarea`)
|
||||
}
|
||||
|
||||
func assertHasSubmit(body string) bool {
|
||||
return strings.Contains(body, `<button`) && strings.Contains(body, `type="submit"`)
|
||||
}
|
||||
|
||||
func assertHasHref(body string, segments ...string) bool {
|
||||
if !strings.Contains(body, `href="`) {
|
||||
return false
|
||||
}
|
||||
for _, segment := range segments {
|
||||
re := regexp.MustCompile(`a[^>]*href="` + segment + `"`)
|
||||
if !re.MatchString(body) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
15
notes/create.go
Executable file
15
notes/create.go
Executable file
@@ -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
|
||||
}
|
||||
18
notes/create_test.go
Executable file
18
notes/create_test.go
Executable file
@@ -0,0 +1,18 @@
|
||||
package notes
|
||||
|
||||
import (
|
||||
"local/notes-server/config"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
config.Root = "/tmp"
|
||||
n := &Notes{}
|
||||
resp, err := n.Create("/create/a")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if resp != "/edit/a" {
|
||||
t.Error(resp)
|
||||
}
|
||||
}
|
||||
11
notes/delete.go
Executable file
11
notes/delete.go
Executable file
@@ -0,0 +1,11 @@
|
||||
package notes
|
||||
|
||||
import (
|
||||
"local/notes-server/filetree"
|
||||
"os"
|
||||
)
|
||||
|
||||
func (n *Notes) Delete(urlPath string) error {
|
||||
p := filetree.NewPathFromURL(urlPath)
|
||||
return os.Remove(p.Local)
|
||||
}
|
||||
43
notes/delete_test.go
Executable file
43
notes/delete_test.go
Executable file
@@ -0,0 +1,43 @@
|
||||
package notes
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"local/notes-server/config"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
config.Root = "/tmp"
|
||||
ioutil.WriteFile("/tmp/a", []byte("hi"), os.ModePerm)
|
||||
n := &Notes{}
|
||||
if err := n.Delete("/notes/a"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if _, err := os.Stat("/tmp/a"); err == nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
d, err := ioutil.TempDir(os.TempDir(), "trydel*")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i := 0; i < 3; i++ {
|
||||
f, err := ioutil.TempFile(d, "file*")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.Close()
|
||||
}
|
||||
if err := n.Delete(d); err == nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
e, err := ioutil.TempDir(os.TempDir(), "trydel*")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := n.Delete(e); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
28
notes/dir.go
Executable file
28
notes/dir.go
Executable file
@@ -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)
|
||||
}
|
||||
36
notes/dir_test.go
Executable file
36
notes/dir_test.go
Executable file
@@ -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)
|
||||
}
|
||||
36
notes/edit.go
Executable file
36
notes/edit.go
Executable file
@@ -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(`
|
||||
<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%%; cursor:crosshair;">%s</textarea>
|
||||
</table>
|
||||
<button type="submit">Submit</button>
|
||||
</form>
|
||||
`, href, b)
|
||||
}
|
||||
21
notes/edit_test.go
Executable file
21
notes/edit_test.go
Executable file
@@ -0,0 +1,21 @@
|
||||
package notes
|
||||
|
||||
import (
|
||||
"local/notes-server/config"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEdit(t *testing.T) {
|
||||
config.Root = "/tmp"
|
||||
n := &Notes{}
|
||||
if body, err := n.Edit("/notes/a"); err != nil {
|
||||
t.Error(err)
|
||||
} else if !strings.Contains(body, "/submit/a") {
|
||||
t.Error(body)
|
||||
}
|
||||
config.Root = "/usr"
|
||||
if _, err := n.Edit("/notes/local"); err == nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
25
notes/file.go
Executable file
25
notes/file.go
Executable file
@@ -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
|
||||
}
|
||||
49
notes/file_test.go
Executable file
49
notes/file_test.go
Executable file
@@ -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)
|
||||
}
|
||||
13
notes/notes.go
Executable file
13
notes/notes.go
Executable 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,
|
||||
}
|
||||
}
|
||||
1
notes/notes_test.go
Executable file
1
notes/notes_test.go
Executable file
@@ -0,0 +1 @@
|
||||
package notes
|
||||
96
notes/search.go
Executable file
96
notes/search.go
Executable file
@@ -0,0 +1,96 @@
|
||||
package notes
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"local/notes-server/filetree"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type searcher struct {
|
||||
patterns []*regexp.Regexp
|
||||
}
|
||||
|
||||
func newSearcher(phrase string) (*searcher, error) {
|
||||
phrases := strings.Split(phrase, " ")
|
||||
patterns := make([]*regexp.Regexp, 0)
|
||||
for _, phrase := range phrases {
|
||||
if len(phrase) == 0 {
|
||||
continue
|
||||
}
|
||||
pattern, err := regexp.Compile("(?i)" + phrase)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
patterns = append(patterns, pattern)
|
||||
}
|
||||
if len(patterns) < 1 {
|
||||
return nil, errors.New("no search specified")
|
||||
}
|
||||
return &searcher{
|
||||
patterns: patterns,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *searcher) matches(input []byte) bool {
|
||||
for _, pattern := range s.patterns {
|
||||
if !pattern.Match(input) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (n *Notes) Search(phrase string) (string, error) {
|
||||
searcher, err := newSearcher(phrase)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
files := filetree.NewFiles()
|
||||
err = filepath.Walk(n.root,
|
||||
func(walked string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
if size := info.Size(); size < 1 || size > (5*1024*1024) {
|
||||
return nil
|
||||
}
|
||||
ok, err := grepFile(walked, searcher)
|
||||
if err != nil && err.Error() == "bufio.Scanner: token too long" {
|
||||
err = nil
|
||||
}
|
||||
if err == nil && ok {
|
||||
p := filetree.NewPathFromLocal(path.Dir(walked))
|
||||
files.Push(p, info)
|
||||
}
|
||||
if err != nil {
|
||||
log.Printf("failed to scan %v: %v", walked, err)
|
||||
}
|
||||
return err
|
||||
},
|
||||
)
|
||||
return filetree.Paths(*files).List(true), err
|
||||
}
|
||||
|
||||
func grepFile(file string, searcher *searcher) (bool, error) {
|
||||
f, err := os.Open(file)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer f.Close()
|
||||
scanner := bufio.NewScanner(f)
|
||||
for scanner.Scan() {
|
||||
if searcher.matches(scanner.Bytes()) {
|
||||
return true, scanner.Err()
|
||||
}
|
||||
}
|
||||
return false, scanner.Err()
|
||||
}
|
||||
54
notes/search_test.go
Executable file
54
notes/search_test.go
Executable file
@@ -0,0 +1,54 @@
|
||||
package notes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSearch(t *testing.T) {
|
||||
d, err := ioutil.TempDir(os.TempDir(), "pattern*")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(d)
|
||||
for i := 0; i < 5; i++ {
|
||||
f, err := ioutil.TempFile(d, fmt.Sprintf("file_%d", i))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fmt.Fprintf(f, "this file is number %d", i)
|
||||
f.Close()
|
||||
}
|
||||
|
||||
n := New()
|
||||
n.root = d
|
||||
|
||||
result, err := n.Search("this file")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if v := len(strings.Split(result, "\n")); v < 7 {
|
||||
t.Fatal(v, result)
|
||||
}
|
||||
|
||||
result, err = n.Search("4")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if v := len(strings.Split(result, "\n")); v > 4 {
|
||||
t.Fatal(v, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchBigFiles(t *testing.T) {
|
||||
n := New()
|
||||
n.root = "/usr/local/bin"
|
||||
|
||||
_, err := n.Search("this file")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
14
notes/submit.go
Executable file
14
notes/submit.go
Executable file
@@ -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)
|
||||
}
|
||||
22
notes/submit_test.go
Executable file
22
notes/submit_test.go
Executable file
@@ -0,0 +1,22 @@
|
||||
package notes
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"local/notes-server/config"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSubmit(t *testing.T) {
|
||||
config.Root = "/tmp"
|
||||
n := &Notes{}
|
||||
if err := n.Submit("/submit/a", "a"); err != nil {
|
||||
t.Error(err)
|
||||
} else if b, err := ioutil.ReadFile("/tmp/a"); err != nil {
|
||||
t.Error(err)
|
||||
} else if string(b) != "a" {
|
||||
t.Error(string(b))
|
||||
} else if err := os.Remove("/tmp/a"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
18
public/A/asdf
Executable file
18
public/A/asdf
Executable file
@@ -0,0 +1,18 @@
|
||||
# h1
|
||||
|
||||
hi
|
||||
|
||||
## h2
|
||||
|
||||
hi
|
||||
|
||||
### h3
|
||||
|
||||
hi
|
||||
|
||||
#### h4
|
||||
|
||||
hi
|
||||
|
||||
* bullet
|
||||
* 1
|
||||
5
public/B/y
Executable file
5
public/B/y
Executable file
@@ -0,0 +1,5 @@
|
||||
## B.y
|
||||
|
||||
| hello | world |
|
||||
|-------|-------|
|
||||
| cont | ent. |
|
||||
8
public/B/z
Executable file
8
public/B/z
Executable file
@@ -0,0 +1,8 @@
|
||||
## B.z
|
||||
|
||||
| hello | world |
|
||||
|-------|-------|
|
||||
| cont | ent. |
|
||||
|
||||
|
||||
HI
|
||||
3
public/D/E/F/g
Executable file
3
public/D/E/F/g
Executable file
@@ -0,0 +1,3 @@
|
||||
# Hello
|
||||
|
||||
## World
|
||||
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
Executable file
13
server/.notes/notes.go
Executable 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
|
||||
16
server/delete.go
Executable file
16
server/delete.go
Executable file
@@ -0,0 +1,16 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (s *Server) delete(w http.ResponseWriter, r *http.Request) {
|
||||
if err := s.Notes.Delete(r.URL.Path); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
r.URL.Path = strings.Replace(path.Dir(r.URL.Path), "delete", "notes", 1)
|
||||
http.Redirect(w, r, r.URL.String(), http.StatusPermanentRedirect)
|
||||
}
|
||||
@@ -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 +0,0 @@
|
||||
package server
|
||||
47
server/html.go
Executable file
47
server/html.go
Executable file
@@ -0,0 +1,47 @@
|
||||
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, style ...string) string {
|
||||
return h("2", content, style...)
|
||||
}
|
||||
|
||||
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, style ...string) string {
|
||||
s := ""
|
||||
if len(style) > 0 {
|
||||
s = fmt.Sprintf("style=%q", style[0])
|
||||
}
|
||||
return fmt.Sprintf("\n<h%s %s>\n%s\n</h%s>\n", level, s, 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)
|
||||
@@ -39,7 +39,7 @@ func TestBlock(t *testing.T) {
|
||||
|
||||
func TestH(t *testing.T) {
|
||||
s := strings.ReplaceAll(strings.TrimSpace(h2("hi")), "\n", ".")
|
||||
if ok, err := regexp.MatchString("<h2>.*hi.*<.h2>", s); err != nil {
|
||||
if ok, err := regexp.MatchString("<h2[ ]*>.*hi.*<.h2>", s); err != nil {
|
||||
t.Fatal(err, s)
|
||||
} else if !ok {
|
||||
t.Fatal(ok, s)
|
||||
@@ -2,26 +2,87 @@ 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) {
|
||||
fmt.Fprintln(w, h2(p.MultiLink()))
|
||||
func notesHead(w http.ResponseWriter, p filetree.Path) {
|
||||
fmt.Fprintln(w, h2(p.MultiLink(), "margin: 0; position: fixed; padding: .25em; background-color: #202b38; width: 100%; top: 0;"))
|
||||
htmlSearch(w)
|
||||
}
|
||||
|
||||
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) {
|
||||
htmlCreate(w, baseHREF)
|
||||
htmlDelete(w, 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) {
|
||||
htmlEdit(w, baseHREF)
|
||||
htmlDelete(w, baseHREF)
|
||||
}
|
||||
|
||||
func htmlEdit(w http.ResponseWriter, baseHREF string) {
|
||||
fmt.Fprintf(w, `<div style='display:inline-block'>
|
||||
<a href=%q><input type="button" value="Edit"></input></a>
|
||||
</div><br>`, path.Join("/edit/", baseHREF))
|
||||
}
|
||||
|
||||
func htmlDelete(w http.ResponseWriter, baseHREF string) {
|
||||
fmt.Fprintf(w, `<div style='display:inline-block'>
|
||||
<a href=%q><input type="button" value="Delete" onclick="return confirm('Delete?');"></input></a>
|
||||
</div><br>`, path.Join("/delete/", baseHREF))
|
||||
}
|
||||
|
||||
func htmlCreate(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 htmlSearch(w http.ResponseWriter) {
|
||||
fmt.Fprintf(w, `
|
||||
<form action=%q method="post" style="padding-top: 2.5em">
|
||||
<input type="text" name="keywords"></input>
|
||||
<button type="submit">Search</button>
|
||||
</form>
|
||||
`, "/search")
|
||||
}
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
package server
|
||||
@@ -1,23 +0,0 @@
|
||||
package server
|
||||
|
||||
import "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)
|
||||
}
|
||||
}
|
||||
@@ -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,8 +2,10 @@ package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"local/gziphttp"
|
||||
"local/router"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func (s *Server) Routes() error {
|
||||
@@ -12,21 +14,33 @@ func (s *Server) Routes() error {
|
||||
path string
|
||||
handler http.HandlerFunc
|
||||
}{
|
||||
{
|
||||
path: "/",
|
||||
handler: s.root,
|
||||
},
|
||||
{
|
||||
path: fmt.Sprintf("notes/%s%s", wildcard, wildcard),
|
||||
handler: s.notes,
|
||||
handler: s.gzip(s.authenticate(s.notes)),
|
||||
},
|
||||
{
|
||||
path: fmt.Sprintf("edit/%s%s", wildcard, wildcard),
|
||||
handler: s.edit,
|
||||
handler: s.gzip(s.authenticate(s.edit)),
|
||||
},
|
||||
{
|
||||
path: fmt.Sprintf("delete/%s%s", wildcard, wildcard),
|
||||
handler: s.gzip(s.authenticate(s.delete)),
|
||||
},
|
||||
{
|
||||
path: fmt.Sprintf("submit/%s%s", wildcard, wildcard),
|
||||
handler: s.submit,
|
||||
handler: s.gzip(s.authenticate(s.submit)),
|
||||
},
|
||||
{
|
||||
path: fmt.Sprintf("create/%s%s", wildcard, wildcard),
|
||||
handler: s.create,
|
||||
handler: s.gzip(s.authenticate(s.create)),
|
||||
},
|
||||
{
|
||||
path: fmt.Sprintf("search"),
|
||||
handler: s.gzip(s.authenticate(s.search)),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -37,3 +51,22 @@ func (s *Server) Routes() error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) root(w http.ResponseWriter, r *http.Request) {
|
||||
r.URL.Path = "/notes"
|
||||
http.Redirect(w, r, r.URL.String(), http.StatusPermanentRedirect)
|
||||
}
|
||||
|
||||
func (s *Server) gzip(h http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if gziphttp.Can(r) {
|
||||
gz := gziphttp.New(w)
|
||||
defer gz.Close()
|
||||
w = gz
|
||||
}
|
||||
if filepath.Ext(r.URL.Path) == ".css" {
|
||||
w.Header().Set("Content-Type", "text/css; charset=utf-8")
|
||||
}
|
||||
h(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ func TestServerNotes(t *testing.T) {
|
||||
t.Logf("serve %s: %s", r.URL.Path, w.Body.Bytes())
|
||||
}
|
||||
|
||||
func TestServerNotes(t *testing.T) {
|
||||
func TestServerNotesB(t *testing.T) {
|
||||
s := New()
|
||||
w := httptest.NewRecorder()
|
||||
r := &http.Request{
|
||||
|
||||
26
server/search.go
Executable file
26
server/search.go
Executable file
@@ -0,0 +1,26 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html"
|
||||
"local/notes-server/filetree"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (s *Server) search(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
keywords := r.FormValue("keywords")
|
||||
keywords = html.UnescapeString(keywords)
|
||||
results, err := s.Notes.Search(keywords)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
head(w, r)
|
||||
fmt.Fprintln(w, h2(filetree.NewPathFromURL("/notes").MultiLink()))
|
||||
fmt.Fprintln(w, h1(keywords), results)
|
||||
foot(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
|
||||
67
versions/.versions.go
Executable file
67
versions/.versions.go
Executable file
@@ -0,0 +1,67 @@
|
||||
package versions
|
||||
|
||||
import (
|
||||
"local/notes-server/config"
|
||||
"time"
|
||||
|
||||
git "gopkg.in/src-d/go-git.v4"
|
||||
"gopkg.in/src-d/go-git.v4/plumbing/object"
|
||||
)
|
||||
|
||||
type Versions struct {
|
||||
repo *git.Repository
|
||||
}
|
||||
|
||||
func New() (*Versions, error) {
|
||||
repo, err := git.PlainInit(config.Root, false)
|
||||
if err != nil {
|
||||
repo, err = git.PlainOpen(config.Root)
|
||||
}
|
||||
return &Versions{
|
||||
repo: repo,
|
||||
}, err
|
||||
}
|
||||
|
||||
func (v *Versions) Gitmmit() error {
|
||||
if err := v.AddAll(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := v.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *Versions) AddAll() error {
|
||||
worktree, err := v.worktree()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, path := range []string{".", "./*", "./**", "/", "/**", "/*"} {
|
||||
if err := worktree.AddGlob(path); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *Versions) Commit() error {
|
||||
worktree, err := v.worktree()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
opts := &git.CommitOptions{Author: &object.Signature{}}
|
||||
_, err = worktree.Commit(time.Now().String(), opts)
|
||||
return err
|
||||
}
|
||||
|
||||
func (v *Versions) worktree() (*git.Worktree, error) {
|
||||
worktree, err := v.repo.Worktree()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return worktree, nil
|
||||
}
|
||||
65
versions/.versions_test.go
Executable file
65
versions/.versions_test.go
Executable file
@@ -0,0 +1,65 @@
|
||||
package versions
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"local/notes-server/config"
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestVersionsHappy(t *testing.T) {
|
||||
d, err := ioutil.TempDir(os.TempDir(), "prefix")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(d)
|
||||
if err := ioutil.WriteFile(path.Join(d, "a.md"), []byte("# Hello"), os.ModePerm); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ioutil.WriteFile(path.Join(d, "b.md"), []byte("# World"), os.ModePerm); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
config.Root = d
|
||||
v, err := New()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if err := v.AddAll(); err != nil {
|
||||
t.Error("failed add", err)
|
||||
}
|
||||
if err := v.Commit(); err != nil {
|
||||
t.Error("failed commit", err)
|
||||
}
|
||||
if err := v.Gitmmit(); err != nil {
|
||||
t.Error("failed gitmmit", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVersionsBad(t *testing.T) {
|
||||
config.Root = "/not/a/real/path"
|
||||
if _, err := New(); err == nil {
|
||||
t.Error("passed new from nil path")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVersionsDirty(t *testing.T) {
|
||||
if os.Getenv("DIRTY") == "" {
|
||||
return
|
||||
}
|
||||
config.Root = "/tmp/foo"
|
||||
v, err := New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ioutil.WriteFile(path.Join(config.Root, "file.md"), []byte(`
|
||||
# Hello
|
||||
## World
|
||||
I'm a doc
|
||||
`), os.ModePerm)
|
||||
if err := v.Gitmmit(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(v)
|
||||
}
|
||||
44
versions/versions.go
Executable file
44
versions/versions.go
Executable file
@@ -0,0 +1,44 @@
|
||||
package versions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"local/notes-server/config"
|
||||
"os/exec"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Versions struct {
|
||||
}
|
||||
|
||||
func New() (*Versions, error) {
|
||||
v := &Versions{}
|
||||
v.cmd("git", "init")
|
||||
v.cmd("git", "config", "user.email", "user@user.user")
|
||||
v.cmd("git", "config", "user.name", "user")
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func (v *Versions) Gitmmit() error {
|
||||
if err := v.AddAll(); err != nil {
|
||||
return fmt.Errorf("cannot add all: %v", err)
|
||||
}
|
||||
if err := v.Commit(); err != nil {
|
||||
return fmt.Errorf("cannot commit: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *Versions) AddAll() error {
|
||||
return v.cmd("git", "add", "-A", ":/")
|
||||
}
|
||||
|
||||
func (v *Versions) Commit() error {
|
||||
return v.cmd("git", "commit", "-m", time.Now().String())
|
||||
}
|
||||
|
||||
func (v *Versions) cmd(cmd string, args ...string) error {
|
||||
command := exec.Command(cmd, args...)
|
||||
command.Dir = config.Root
|
||||
_, err := command.CombinedOutput()
|
||||
return err
|
||||
}
|
||||
42
versions/versions_test.go
Executable file
42
versions/versions_test.go
Executable file
@@ -0,0 +1,42 @@
|
||||
package versions
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"local/notes-server/config"
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestVersions(t *testing.T) {
|
||||
d, err := ioutil.TempDir(os.TempDir(), "prefix")
|
||||
config.Root = d
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, f := range []string{"a", "b"} {
|
||||
ioutil.WriteFile(path.Join(d, f+".md"), []byte(f), os.ModePerm)
|
||||
}
|
||||
|
||||
v, err := New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := v.AddAll(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := v.Commit(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if err := v.Gitmmit(); err == nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
for _, f := range []string{"c", "b"} {
|
||||
ioutil.WriteFile(path.Join(d, f+".md"), []byte("d"), os.ModePerm)
|
||||
}
|
||||
if err := v.Gitmmit(); err != nil {
|
||||
t.Error(config.Root, err)
|
||||
}
|
||||
}
|
||||
@@ -12,9 +12,15 @@
|
||||
padding: .5pt;
|
||||
border-radius: 6px;
|
||||
}
|
||||
nav li li li li {
|
||||
display: none;
|
||||
}
|
||||
img {
|
||||
max-height: 400px;
|
||||
}
|
||||
body {
|
||||
font-size: 125%;
|
||||
}
|
||||
</style>
|
||||
</header>
|
||||
<body height="100%">
|
||||
|
||||
Reference in New Issue
Block a user