diff --git a/config/config.go b/config/config.go index 5d85275..66385b4 100644 --- a/config/config.go +++ b/config/config.go @@ -5,6 +5,7 @@ import ( "fmt" "io/ioutil" "local/args" + "log" "os" "strings" ) @@ -33,7 +34,8 @@ func Refresh() { panic(err) } - wrap := as.Get("wrap").String() + wrap := as.Get("wrap").GetString() + log.Printf("reading %v (%T)", wrap, wrap) b, err := ioutil.ReadFile(wrap) if err != nil { panic(err) @@ -43,8 +45,8 @@ func Refresh() { panic(len(bs)) } - Root = as.Get("root").String() - Port = ":" + strings.TrimPrefix(as.Get("port").String(), ":") + Root = as.Get("root").GetString() + Port = ":" + strings.TrimPrefix(as.Get("port").GetString(), ":") Head = string(bs[0]) Foot = string(bs[1]) } diff --git a/main.go b/main.go index 6547759..24d41ec 100644 --- a/main.go +++ b/main.go @@ -1,47 +1,23 @@ package main import ( - "flag" - "fmt" - "io/ioutil" + "local/notes-server/config" + "local/notes-server/server" "log" "net/http" "os" "os/signal" - "path" - "strings" - - "github.com/gomarkdown/markdown" - "github.com/miekg/mmark" - "gopkg.in/russross/blackfriday.v2" ) -const ( - GOMARKDOWN = "gomarkdown" - RUSSROSS = "russross" - MIEKG = "miekg" -) - -func envOrDefault(key, def string) string { - if v := os.Getenv(key); v != "" { - return v - } - return def -} - func main() { - port := flag.String("p", envOrDefault("PORT", ":41913"), "port to run on") - root := flag.String("root", envOrDefault("ROOT", "."), "root dir to serve") - render := flag.String("render", envOrDefault("RENDER", GOMARKDOWN), "renderer to use") - flag.Parse() - - if !strings.HasPrefix(*port, ":") { - *port = ":" + *port + server := server.New() + if err := server.Routes(); err != nil { + panic(err) } go func() { - log.Printf("Serving %q on %q", *root, *port) - if err := http.ListenAndServe(*port, handler(*render, *root)); err != nil { + log.Printf("Serving %q on %q", config.Root, config.Port) + if err := http.ListenAndServe(config.Port, server); err != nil { panic(err) } }() @@ -51,36 +27,3 @@ func main() { signal.Notify(stop, os.Interrupt) <-stop } - -func handler(code, root string) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - path := path.Join(root, path.Clean(r.URL.Path)) - content, err := ioutil.ReadFile(path) - if err != nil { - http.NotFound(w, r) - return - } - fmt.Fprintln(w, ``) - fmt.Fprintln(w, `
`) - fmt.Fprintln(w, ` - - `) - fmt.Fprintln(w, ``) - fmt.Fprintln(w, ``) - w.Write(render(code, content)) - fmt.Fprintln(w, ``) - fmt.Fprintln(w, ``) - }) -} - -func render(code string, content []byte) []byte { - switch code { - case GOMARKDOWN: - return markdown.ToHTML(content, nil, nil) - case RUSSROSS: - return blackfriday.Run(content) //, blackfriday.WithExtensions(blackfriday.Extensions(blackfriday.TOC))) - case MIEKG: - return mmark.Parse(content, mmark.HtmlRenderer(0, ``, ``), mmark.EXTENSION_TABLES|int(blackfriday.TOC)).Bytes() - } - return nil -} diff --git a/server/dir.go b/server/dir.go index dc0541c..ac0055a 100644 --- a/server/dir.go +++ b/server/dir.go @@ -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()) } diff --git a/server/notes.go b/server/notes.go index d302a8e..89406bd 100644 --- a/server/notes.go +++ b/server/notes.go @@ -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())) +} diff --git a/server/path.go b/server/path.go index 4ce1b0d..4f9fbda 100644 --- a/server/path.go +++ b/server/path.go @@ -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(`/%s`, 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("