Add ability to write notes to api
This commit is contained in:
parent
fd9c2a1b95
commit
fb3a64af33
3 changed files with 37 additions and 22 deletions
1
TODO.md
1
TODO.md
|
@ -2,6 +2,7 @@
|
||||||
List of things to add to this project
|
List of things to add to this project
|
||||||
|
|
||||||
## Crucial
|
## Crucial
|
||||||
|
* Handle all the w.Write errors somehow
|
||||||
* Add export feature
|
* Add export feature
|
||||||
* Add missing frontend pages
|
* Add missing frontend pages
|
||||||
|
|
||||||
|
|
45
files.go
45
files.go
|
@ -8,21 +8,22 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetFile returns raw contents of a txt file in data directory
|
// GetFile returns raw contents of a txt file in data directory
|
||||||
func GetFile(filename string, w http.ResponseWriter, r *http.Request) {
|
func GetFile(filename string, w http.ResponseWriter) {
|
||||||
path := "data/" + filename + ".txt" // Can we and should we sanitize this?
|
filename = "data/" + path.Clean(filename) + ".txt" // Does this sanitize the path?
|
||||||
|
|
||||||
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
|
if _, err := os.Stat(filename); errors.Is(err, os.ErrNotExist) {
|
||||||
http.Error(w, "file not found", http.StatusNotFound)
|
http.Error(w, "file not found", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fileContents, err := os.ReadFile(path)
|
fileContents, err := os.ReadFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "error reading file", http.StatusInternalServerError)
|
http.Error(w, "error reading file", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
@ -63,7 +64,7 @@ func PostFile(filename string, w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListFiles returns JSON list of filenames in a directory without extensions or path
|
// ListFiles returns JSON list of filenames in a directory without extensions or path
|
||||||
func ListFiles(directory string, w http.ResponseWriter, r *http.Request) {
|
func ListFiles(directory string, w http.ResponseWriter) {
|
||||||
filenames, err := filepath.Glob("data/" + directory + "/*.txt")
|
filenames, err := filepath.Glob("data/" + directory + "/*.txt")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "error searching for files", http.StatusInternalServerError)
|
http.Error(w, "error searching for files", http.StatusInternalServerError)
|
||||||
|
@ -77,17 +78,28 @@ func ListFiles(directory string, w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write(filenamesJson)
|
w.Write(filenamesJson)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetDay returns a day specified in URL
|
||||||
func GetDay(w http.ResponseWriter, r *http.Request) {
|
func GetDay(w http.ResponseWriter, r *http.Request) {
|
||||||
// TODO: This will be different if I move away from chi
|
|
||||||
dayString := chi.URLParam(r, "day")
|
dayString := chi.URLParam(r, "day")
|
||||||
if dayString == "" {
|
if dayString == "" {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
w.Write([]byte("day not specified"))
|
w.Write([]byte("day not specified"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
GetFile("day/"+dayString, w, r)
|
GetFile("day/"+dayString, w)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetToday runs GetFile with today's daily txt
|
||||||
|
func GetToday(w http.ResponseWriter) {
|
||||||
|
GetFile("day/"+time.Now().Format("2006-01-02"), w)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PostToday runs PostFile with today's daily txt
|
||||||
|
func PostToday(w http.ResponseWriter, r *http.Request) {
|
||||||
|
PostFile("day/"+time.Now().Format("2006-01-02"), w, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetNote returns a note specified in URL
|
||||||
func GetNote(w http.ResponseWriter, r *http.Request) {
|
func GetNote(w http.ResponseWriter, r *http.Request) {
|
||||||
noteString := chi.URLParam(r, "note")
|
noteString := chi.URLParam(r, "note")
|
||||||
if noteString == "" {
|
if noteString == "" {
|
||||||
|
@ -95,15 +107,16 @@ func GetNote(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("note not specified"))
|
w.Write([]byte("note not specified"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
GetFile("notes/"+noteString, w, r)
|
GetFile("notes/"+noteString, w)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetToday runs GetFile with today's daily txt
|
// PostNote writes request's contents to a note specified in URL
|
||||||
func GetToday(w http.ResponseWriter, r *http.Request) {
|
func PostNote(w http.ResponseWriter, r *http.Request) {
|
||||||
GetFile("day/"+time.Now().Format("2006-01-02"), w, r)
|
noteString := chi.URLParam(r, "note")
|
||||||
}
|
if noteString == "" {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
// PostToday runs PostFile with today's daily txt
|
w.Write([]byte("note not specified"))
|
||||||
func PostToday(w http.ResponseWriter, r *http.Request) {
|
return
|
||||||
PostFile("day/"+time.Now().Format("2006-01-02"), w, r)
|
}
|
||||||
|
PostFile("notes/"+noteString, w, r)
|
||||||
}
|
}
|
||||||
|
|
13
serve.go
13
serve.go
|
@ -26,25 +26,26 @@ func Serve() {
|
||||||
// API =============
|
// API =============
|
||||||
apiRouter := chi.NewRouter()
|
apiRouter := chi.NewRouter()
|
||||||
|
|
||||||
apiRouter.Get("/readme", func(w http.ResponseWriter, r *http.Request) { GetFile("readme", w, r) })
|
apiRouter.Get("/readme", func(w http.ResponseWriter, r *http.Request) { GetFile("readme", w) })
|
||||||
apiRouter.Post("/readme", func(w http.ResponseWriter, r *http.Request) { PostFile("readme", w, r) })
|
apiRouter.Post("/readme", func(w http.ResponseWriter, r *http.Request) { PostFile("readme", w, r) })
|
||||||
apiRouter.Get("/log", func(w http.ResponseWriter, r *http.Request) { GetFile("log", w, r) })
|
apiRouter.Get("/log", func(w http.ResponseWriter, r *http.Request) { GetFile("log", w) })
|
||||||
apiRouter.Post("/log", func(w http.ResponseWriter, r *http.Request) { PostLog(w, r) })
|
apiRouter.Post("/log", func(w http.ResponseWriter, r *http.Request) { PostLog(w, r) })
|
||||||
|
|
||||||
apiRouter.Get("/day", func(w http.ResponseWriter, r *http.Request) { ListFiles("day", w, r) })
|
apiRouter.Get("/day", func(w http.ResponseWriter, r *http.Request) { ListFiles("day", w) })
|
||||||
apiRouter.Get("/day/{day}", func(w http.ResponseWriter, r *http.Request) { GetDay(w, r) })
|
apiRouter.Get("/day/{day}", func(w http.ResponseWriter, r *http.Request) { GetDay(w, r) })
|
||||||
|
|
||||||
apiRouter.Get("/notes", func(w http.ResponseWriter, r *http.Request) { ListFiles("notes", w, r) })
|
apiRouter.Get("/notes", func(w http.ResponseWriter, r *http.Request) { ListFiles("notes", w) })
|
||||||
apiRouter.Get("/notes/{note}", func(w http.ResponseWriter, r *http.Request) { GetNote(w, r) })
|
apiRouter.Get("/notes/{note}", func(w http.ResponseWriter, r *http.Request) { GetNote(w, r) })
|
||||||
|
apiRouter.Post("/notes/{note}", func(w http.ResponseWriter, r *http.Request) { PostNote(w, r) })
|
||||||
|
|
||||||
apiRouter.Get("/today", func(w http.ResponseWriter, r *http.Request) { GetToday(w, r) })
|
apiRouter.Get("/today", func(w http.ResponseWriter, r *http.Request) { GetToday(w) })
|
||||||
apiRouter.Post("/today", func(w http.ResponseWriter, r *http.Request) { PostToday(w, r) })
|
apiRouter.Post("/today", func(w http.ResponseWriter, r *http.Request) { PostToday(w, r) })
|
||||||
|
|
||||||
r.Mount("/api", apiRouter)
|
r.Mount("/api", apiRouter)
|
||||||
|
|
||||||
// Static files
|
// Static files
|
||||||
fs := http.FileServer(http.Dir("public"))
|
fs := http.FileServer(http.Dir("public"))
|
||||||
r.Handle("/public/", http.StripPrefix("/public/", fs))
|
r.Handle("/public/*", http.StripPrefix("/public/", fs))
|
||||||
|
|
||||||
fmt.Println("Website working on port: ", Cfg.Port)
|
fmt.Println("Website working on port: ", Cfg.Port)
|
||||||
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(Cfg.Port), r))
|
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(Cfg.Port), r))
|
||||||
|
|
Loading…
Reference in a new issue