89 lines
2.3 KiB
Go
Executable File
89 lines
2.3 KiB
Go
Executable File
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"local/notes-server/filetree"
|
|
"net/http"
|
|
"path"
|
|
)
|
|
|
|
func (s *Server) notes(w http.ResponseWriter, r *http.Request) {
|
|
p := filetree.NewPathFromURL(r.URL.Path)
|
|
head(w, r)
|
|
notesHead(w, p)
|
|
defer foot(w, r)
|
|
if p.IsDir() {
|
|
s.dir(w, r)
|
|
} else if p.IsFile() {
|
|
s.file(w, r)
|
|
} else {
|
|
http.NotFound(w, r)
|
|
}
|
|
}
|
|
|
|
func notesHead(w http.ResponseWriter, p filetree.Path) {
|
|
fmt.Fprintln(w, h2(p.MultiLink(), "margin: 0; position: fixed; padding: .25em; background-color: #202b38; width: 100%; top: 0;"))
|
|
htmlSearch(w)
|
|
}
|
|
|
|
func (s *Server) dir(w http.ResponseWriter, r *http.Request) {
|
|
dirs, files, err := s.Notes.Dir(r.URL.Path)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
dirHead(w, filetree.NewPathFromURL(r.URL.Path).BaseHREF)
|
|
block(w, dirs)
|
|
block(w, files)
|
|
}
|
|
|
|
func dirHead(w http.ResponseWriter, baseHREF string) {
|
|
htmlCreate(w, baseHREF)
|
|
htmlDelete(w, baseHREF)
|
|
}
|
|
|
|
func (s *Server) file(w http.ResponseWriter, r *http.Request) {
|
|
file, err := s.Notes.File(r.URL.Path)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
fileHead(w, filetree.NewPathFromURL(r.URL.Path).BaseHREF)
|
|
fmt.Fprintln(w, file)
|
|
}
|
|
|
|
func fileHead(w http.ResponseWriter, baseHREF string) {
|
|
htmlEdit(w, baseHREF)
|
|
htmlDelete(w, baseHREF)
|
|
}
|
|
|
|
func htmlEdit(w http.ResponseWriter, baseHREF string) {
|
|
fmt.Fprintf(w, `<div style='display:inline-block'>
|
|
<a href=%q><input type="button" value="Edit"></input></a>
|
|
</div><br>`, path.Join("/edit/", baseHREF))
|
|
}
|
|
|
|
func htmlDelete(w http.ResponseWriter, baseHREF string) {
|
|
fmt.Fprintf(w, `<div style='display:inline-block'>
|
|
<a href=%q><input type="button" value="Delete" onclick="return confirm('Delete?');"></input></a>
|
|
</div><br>`, path.Join("/delete/", baseHREF))
|
|
}
|
|
|
|
func htmlCreate(w http.ResponseWriter, baseHREF string) {
|
|
fmt.Fprintf(w, `
|
|
<form action=%q method="get">
|
|
<input type="text" name="base"></input>
|
|
<button type="submit">Create</button>
|
|
</form>
|
|
`, path.Join("/create/", baseHREF))
|
|
}
|
|
|
|
func htmlSearch(w http.ResponseWriter) {
|
|
fmt.Fprintf(w, `
|
|
<form action=%q method="post" style="padding-top: 2.5em">
|
|
<input type="text" name="keywords"></input>
|
|
<button type="submit">Search</button>
|
|
</form>
|
|
`, "/search")
|
|
}
|