7 Commits
v0.0 ... v0.3

Author SHA1 Message Date
bel
0d497f4fa8 Fix nonroot path 2019-11-06 17:44:04 -07:00
bel
c6d43699ef TODO 2019-11-06 17:19:57 -07:00
Bel LaPointe
618b9ed513 Fix serving . but absolute and parents still cause bugs 2019-10-21 09:52:05 -06:00
bel
0595c49fc2 todo 2019-10-20 17:34:02 -06:00
bel
da6eaca26f Pass things around via query params 2019-10-20 16:39:26 -06:00
bel
fb02cc994a Dockerfile 2019-10-20 02:27:28 +00:00
bel
a80f9613ef Prove dropdown and image max 2019-10-20 02:16:41 +00:00
9 changed files with 107 additions and 11 deletions

1
.gitignore vendored Normal file → Executable file
View File

@@ -4,3 +4,4 @@ gollum
*.sw* *.sw*
**/*.sw* **/*.sw*
notes-server notes-server
exec-notes-server

16
Dockerfile Executable file
View File

@@ -0,0 +1,16 @@
FROM golang:1.13-alpine as certs
RUN apk update && apk add --no-cache ca-certificates
FROM busybox:glibc
RUN mkdir -p /var/log
WORKDIR /main
COPY --from=certs /etc/ssl/certs /etc/ssl/certs
COPY . .
ENV GOPATH=""
ENV MNT="/mnt/"
ENTRYPOINT ["/main/exec-notes-server"]
CMD []

3
TODO
View File

@@ -5,3 +5,6 @@ x create dir
TOC levels TOC levels
delete pages delete pages
search 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)

View File

@@ -11,10 +11,11 @@ import (
) )
var ( var (
Root string Root string
Port string Port string
Head string Head string
Foot string Foot string
OAuthServer string
) )
func init() { func init() {
@@ -30,6 +31,7 @@ func Refresh() {
as.Append(args.STRING, "root", "root dir path", "./public") as.Append(args.STRING, "root", "root dir path", "./public")
as.Append(args.STRING, "port", "port to listen on", "39909") 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, "wrap", "file with http header/footer", "./wrapper.html")
as.Append(args.STRING, "oauth", "oauth URL", "")
if err := as.Parse(); err != nil { if err := as.Parse(); err != nil {
panic(err) panic(err)
} }
@@ -49,4 +51,5 @@ func Refresh() {
Port = ":" + strings.TrimPrefix(as.Get("port").GetString(), ":") Port = ":" + strings.TrimPrefix(as.Get("port").GetString(), ":")
Head = string(bs[0]) Head = string(bs[0])
Foot = string(bs[1]) Foot = string(bs[1])
OAuthServer = as.Get("oauth").GetString()
} }

View File

@@ -16,10 +16,16 @@ type Path struct {
} }
func NewPathFromLocal(p string) Path { 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) splits := strings.SplitN(p, path.Base(config.Root), 2)
href := splits[0] href := splits[0]
if len(splits) > 1 { if len(splits) > 1 && (splits[0] == "" || splits[0] == "/") {
href = splits[1] href = splits[1]
} else {
href = strings.Join(splits, path.Base(config.Root))
} }
href = path.Join("/notes", href) href = path.Join("/notes", href)
return NewPathFromURL(href) return NewPathFromURL(href)

View File

@@ -1,6 +1,9 @@
package server package server
import "testing" import (
"local/notes-server/config"
"testing"
)
func TestPathIs(t *testing.T) { func TestPathIs(t *testing.T) {
p := Path{Local: "/dev/null"} p := Path{Local: "/dev/null"}
@@ -21,3 +24,48 @@ func TestPathIs(t *testing.T) {
t.Fatal(ok, p) 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)
}
}
}

View File

@@ -2,7 +2,10 @@ package server
import ( import (
"fmt" "fmt"
"local/notes-server/config"
"local/oauth2/oauth2client"
"local/router" "local/router"
"log"
"net/http" "net/http"
) )
@@ -14,19 +17,19 @@ func (s *Server) Routes() error {
}{ }{
{ {
path: fmt.Sprintf("notes/%s%s", wildcard, wildcard), path: fmt.Sprintf("notes/%s%s", wildcard, wildcard),
handler: s.notes, handler: s.authenticate(s.notes),
}, },
{ {
path: fmt.Sprintf("edit/%s%s", wildcard, wildcard), path: fmt.Sprintf("edit/%s%s", wildcard, wildcard),
handler: s.edit, handler: s.authenticate(s.edit),
}, },
{ {
path: fmt.Sprintf("submit/%s%s", wildcard, wildcard), path: fmt.Sprintf("submit/%s%s", wildcard, wildcard),
handler: s.submit, handler: s.authenticate(s.submit),
}, },
{ {
path: fmt.Sprintf("create/%s%s", wildcard, wildcard), path: fmt.Sprintf("create/%s%s", wildcard, wildcard),
handler: s.create, handler: s.authenticate(s.create),
}, },
} }
@@ -37,3 +40,16 @@ func (s *Server) Routes() error {
} }
return nil 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)
}
}

View File

@@ -37,7 +37,7 @@ func TestServerNotes(t *testing.T) {
t.Logf("serve %s: %s", r.URL.Path, w.Body.Bytes()) t.Logf("serve %s: %s", r.URL.Path, w.Body.Bytes())
} }
func TestServerNotes(t *testing.T) { func TestServerNotesB(t *testing.T) {
s := New() s := New()
w := httptest.NewRecorder() w := httptest.NewRecorder()
r := &http.Request{ r := &http.Request{

View File

@@ -12,6 +12,9 @@
padding: .5pt; padding: .5pt;
border-radius: 6px; border-radius: 6px;
} }
img {
max-height: 400px;
}
</style> </style>
</header> </header>
<body height="100%"> <body height="100%">