Remove agenda, become opinionated on file extensions

This commit is contained in:
Andrew-71 2024-03-15 18:48:24 +03:00
parent d796f9e08a
commit c5a5dbd80b
4 changed files with 18 additions and 39 deletions

2
.gitignore vendored
View file

@ -19,3 +19,5 @@ go.work
# Executable # Executable
hibiscus hibiscus
.idea/

View file

@ -1,6 +1,6 @@
# 🌺 Hibiscus.txt # 🌺 Hibiscus.txt
Simple plaintext journaling server. Simple plaintext journaling server. One file a day.
## Features: ## Features:
* Lack of features. No, really. * Lack of features. No, really.

View file

@ -2,33 +2,24 @@ package main
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
) )
var DEFAULT_EXTENSION = "txt" var DEFAULT_EXTENSION = "txt"
func GetFile(filename string, w http.ResponseWriter, r *http.Request) { func GetFile(filename string, w http.ResponseWriter, r *http.Request) {
filenames, err := filepath.Glob("data/" + filename + ".*") // .txt, .md, anything if _, err := os.Stat("data/" + filename + ".txt"); errors.Is(err, os.ErrNotExist) {
if err != nil { NotFound(w, r)
http.Error(w, "error finding file", http.StatusInternalServerError)
return return
} }
if len(filenames) == 0 { fileContents, err := os.ReadFile("data/" + filename + ".txt")
http.Error(w, "no matching files found", http.StatusNotFound)
return
} else if len(filenames) > 1 {
http.Error(w, "several matching files found ("+strconv.Itoa(len(filenames))+")", http.StatusInternalServerError) // TODO: Better handling, duh
return
}
fileContents, err := os.ReadFile(filenames[0])
if err != nil { if err != nil {
http.Error(w, "error reading file", http.StatusInternalServerError) http.Error(w, "error reading file", http.StatusInternalServerError)
return return
@ -49,39 +40,27 @@ func PostFile(filename string, w http.ResponseWriter, r *http.Request) {
return return
} }
filenames, err := filepath.Glob("data/" + filename + ".*") // .txt, .md, anything f, err := os.OpenFile("data/"+filename+".txt", os.O_CREATE|os.O_WRONLY, 0644)
if err != nil { if err != nil {
http.Error(w, "error searching for file", http.StatusInternalServerError) fmt.Println("error opening/making file")
return w.Write([]byte("error opening or creating file"))
} w.WriteHeader(http.StatusInternalServerError)
var filenameFinal string
if len(filenames) == 0 {
// Create new file and write
filenameFinal = "data/" + filename + "." + DEFAULT_EXTENSION
} else if len(filenames) > 1 {
http.Error(w, "several matching files found ("+strconv.Itoa(len(filenames))+")", http.StatusInternalServerError) // TODO: Better handling, duh
return
} else {
filenameFinal = filenames[0]
fmt.Println(filenameFinal)
fmt.Println(filenames)
}
f, err := os.OpenFile(filenameFinal, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println("Error opening/making file")
return return
} }
if _, err := f.Write(body); err != nil { if _, err := f.Write(body); err != nil {
fmt.Println("Error writing to the file") fmt.Println("Error writing to the file")
w.Write([]byte("error writing to file"))
w.WriteHeader(http.StatusInternalServerError)
return
} }
w.Write([]byte("wrote to file"))
w.WriteHeader(http.StatusOK)
} }
// ListFiles returns JSON of filenames in a directory without extensions or path // ListFiles returns JSON 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, r *http.Request) {
filenames, err := filepath.Glob("data/" + directory + "/*") // .txt, .md, anything 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)
return return

View file

@ -25,10 +25,8 @@ func Serve() {
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, 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, r) })
apiRouter.Get("/agenda", func(w http.ResponseWriter, r *http.Request) { GetFile("agenda", w, r) })
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.Post("/agenda", func(w http.ResponseWriter, r *http.Request) { PostFile("agenda", 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, r) })
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) })