Converting to objects
This commit is contained in:
@@ -14,9 +14,7 @@ func isDir(path string) bool {
|
||||
|
||||
func notesDir(path string, w http.ResponseWriter, r *http.Request) {
|
||||
dirs, files := lsDir(path)
|
||||
content := ""
|
||||
content += h1(path)
|
||||
content += dirs.List()
|
||||
content := dirs.List()
|
||||
block(content, w)
|
||||
fmt.Fprintln(w, files.List())
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"local/notes-server/config"
|
||||
"net/http"
|
||||
"path"
|
||||
@@ -8,14 +9,16 @@ import (
|
||||
)
|
||||
|
||||
func (s *Server) notes(w http.ResponseWriter, r *http.Request) {
|
||||
path := resolvePath(r.URL.Path)
|
||||
if isDir(path) {
|
||||
p := resolvePath(r.URL.Path)
|
||||
if isDir(p) {
|
||||
head(w, r)
|
||||
notesDir(path, w, r)
|
||||
notesHead(w, p)
|
||||
notesDir(p, w, r)
|
||||
foot(w, r)
|
||||
} else if isFile(path) {
|
||||
} else if isFile(p) {
|
||||
head(w, r)
|
||||
notesFile(path, w, r)
|
||||
notesHead(w, p)
|
||||
notesFile(p, w, r)
|
||||
foot(w, r)
|
||||
} else {
|
||||
http.NotFound(w, r)
|
||||
@@ -23,7 +26,17 @@ func (s *Server) notes(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func resolvePath(p string) string {
|
||||
p = strings.TrimPrefix(p, "/notes/")
|
||||
p = strings.TrimPrefix(p, "/")
|
||||
p = strings.TrimPrefix(p, "notes")
|
||||
p = strings.TrimPrefix(p, "/")
|
||||
p = path.Join(config.Root, p)
|
||||
return p
|
||||
}
|
||||
|
||||
func notesHead(w http.ResponseWriter, p string) {
|
||||
path := Path{
|
||||
dir: path.Dir(p),
|
||||
base: path.Base(p),
|
||||
}
|
||||
fmt.Fprintln(w, h2(path.MultiLink()))
|
||||
}
|
||||
|
||||
@@ -2,20 +2,56 @@ package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"local/notes-server/config"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Path struct {
|
||||
dir string
|
||||
base string
|
||||
Local string
|
||||
HREF string
|
||||
Base string
|
||||
}
|
||||
|
||||
func NewPath(p string) Path {
|
||||
base := path.Base(p)
|
||||
href := path.Join("/notes", p)
|
||||
local := strings.TrimPrefix(p, "/")
|
||||
local = strings.TrimPrefix(p, "notes")
|
||||
local = strings.TrimPrefix(p, "/")
|
||||
local = path.Join(config.Root, local)
|
||||
return Path{
|
||||
Base: base,
|
||||
HREF: href,
|
||||
Local: local,
|
||||
}
|
||||
}
|
||||
|
||||
func (p Path) MultiLink() string {
|
||||
pa := path.Join("/notes", p.String())
|
||||
full := ""
|
||||
for pa != "/" {
|
||||
base := path.Base(pa)
|
||||
full = fmt.Sprintf(`/<a href=%q>%s</a>`, pa, base) + full
|
||||
pa = path.Dir(pa)
|
||||
}
|
||||
return full
|
||||
}
|
||||
|
||||
func (p Path) String() string {
|
||||
return path.Join(p.dir, p.base)
|
||||
root := path.Base(config.Root)
|
||||
dir := p.dir
|
||||
dirs := strings.SplitN(dir, root, 2)
|
||||
dir = dirs[0]
|
||||
if len(dirs) > 1 {
|
||||
dir = dirs[1]
|
||||
}
|
||||
base := p.base
|
||||
return path.Join(dir, base)
|
||||
}
|
||||
|
||||
func (p Path) LI() string {
|
||||
content := fmt.Sprintf("<li><a href=%q>", p.String())
|
||||
content := fmt.Sprintf(`<li><a href="%s">`, path.Join("/notes", p.String()))
|
||||
content += p.base
|
||||
content += "</li>"
|
||||
return content
|
||||
|
||||
Reference in New Issue
Block a user