Converting to objects

master
scratch 2019-10-19 05:00:05 +00:00
parent 3132ce1a1d
commit a8ee907a0f
6 changed files with 82 additions and 80 deletions

View File

@ -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])
}

71
main.go
View File

@ -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, `<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
}

View File

@ -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())
}

View File

@ -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()))
}

View File

@ -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

10
wrapper.html Normal file
View File

@ -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>