Converting to objects
parent
3132ce1a1d
commit
a8ee907a0f
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"local/args"
|
"local/args"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
@ -33,7 +34,8 @@ func Refresh() {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
wrap := as.Get("wrap").String()
|
wrap := as.Get("wrap").GetString()
|
||||||
|
log.Printf("reading %v (%T)", wrap, wrap)
|
||||||
b, err := ioutil.ReadFile(wrap)
|
b, err := ioutil.ReadFile(wrap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|
@ -43,8 +45,8 @@ func Refresh() {
|
||||||
panic(len(bs))
|
panic(len(bs))
|
||||||
}
|
}
|
||||||
|
|
||||||
Root = as.Get("root").String()
|
Root = as.Get("root").GetString()
|
||||||
Port = ":" + strings.TrimPrefix(as.Get("port").String(), ":")
|
Port = ":" + strings.TrimPrefix(as.Get("port").GetString(), ":")
|
||||||
Head = string(bs[0])
|
Head = string(bs[0])
|
||||||
Foot = string(bs[1])
|
Foot = string(bs[1])
|
||||||
}
|
}
|
||||||
|
|
|
||||||
71
main.go
71
main.go
|
|
@ -1,47 +1,23 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"local/notes-server/config"
|
||||||
"fmt"
|
"local/notes-server/server"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"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() {
|
func main() {
|
||||||
port := flag.String("p", envOrDefault("PORT", ":41913"), "port to run on")
|
server := server.New()
|
||||||
root := flag.String("root", envOrDefault("ROOT", "."), "root dir to serve")
|
if err := server.Routes(); err != nil {
|
||||||
render := flag.String("render", envOrDefault("RENDER", GOMARKDOWN), "renderer to use")
|
panic(err)
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
if !strings.HasPrefix(*port, ":") {
|
|
||||||
*port = ":" + *port
|
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
log.Printf("Serving %q on %q", *root, *port)
|
log.Printf("Serving %q on %q", config.Root, config.Port)
|
||||||
if err := http.ListenAndServe(*port, handler(*render, *root)); err != nil {
|
if err := http.ListenAndServe(config.Port, server); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
@ -51,36 +27,3 @@ func main() {
|
||||||
signal.Notify(stop, os.Interrupt)
|
signal.Notify(stop, os.Interrupt)
|
||||||
<-stop
|
<-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, `<html>`)
|
|
||||||
fmt.Fprintln(w, `<head>`)
|
|
||||||
fmt.Fprintln(w, `
|
|
||||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/dark.min.css">
|
|
||||||
`)
|
|
||||||
fmt.Fprintln(w, `</head>`)
|
|
||||||
fmt.Fprintln(w, `<body>`)
|
|
||||||
w.Write(render(code, content))
|
|
||||||
fmt.Fprintln(w, `</body>`)
|
|
||||||
fmt.Fprintln(w, `</html>`)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -14,9 +14,7 @@ func isDir(path string) bool {
|
||||||
|
|
||||||
func notesDir(path string, w http.ResponseWriter, r *http.Request) {
|
func notesDir(path string, w http.ResponseWriter, r *http.Request) {
|
||||||
dirs, files := lsDir(path)
|
dirs, files := lsDir(path)
|
||||||
content := ""
|
content := dirs.List()
|
||||||
content += h1(path)
|
|
||||||
content += dirs.List()
|
|
||||||
block(content, w)
|
block(content, w)
|
||||||
fmt.Fprintln(w, files.List())
|
fmt.Fprintln(w, files.List())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"local/notes-server/config"
|
"local/notes-server/config"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
|
|
@ -8,14 +9,16 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *Server) notes(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) notes(w http.ResponseWriter, r *http.Request) {
|
||||||
path := resolvePath(r.URL.Path)
|
p := resolvePath(r.URL.Path)
|
||||||
if isDir(path) {
|
if isDir(p) {
|
||||||
head(w, r)
|
head(w, r)
|
||||||
notesDir(path, w, r)
|
notesHead(w, p)
|
||||||
|
notesDir(p, w, r)
|
||||||
foot(w, r)
|
foot(w, r)
|
||||||
} else if isFile(path) {
|
} else if isFile(p) {
|
||||||
head(w, r)
|
head(w, r)
|
||||||
notesFile(path, w, r)
|
notesHead(w, p)
|
||||||
|
notesFile(p, w, r)
|
||||||
foot(w, r)
|
foot(w, r)
|
||||||
} else {
|
} else {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
|
|
@ -23,7 +26,17 @@ func (s *Server) notes(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolvePath(p string) string {
|
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)
|
p = path.Join(config.Root, p)
|
||||||
return 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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"local/notes-server/config"
|
||||||
"path"
|
"path"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Path struct {
|
type Path struct {
|
||||||
dir string
|
Local string
|
||||||
base 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 {
|
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 {
|
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 += p.base
|
||||||
content += "</li>"
|
content += "</li>"
|
||||||
return content
|
return content
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
<html>
|
||||||
|
<header>
|
||||||
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/dark.min.css">
|
||||||
|
</header>
|
||||||
|
<body>
|
||||||
|
{{{}}}
|
||||||
|
</body>
|
||||||
|
<footer>
|
||||||
|
</footer>
|
||||||
|
</html>
|
||||||
Loading…
Reference in New Issue