2024-10-21 16:46:25 +03:00
|
|
|
package server
|
2024-03-28 10:21:04 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"log/slog"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2024-10-21 16:46:25 +03:00
|
|
|
|
|
|
|
"git.a71.su/Andrew71/hibiscus-txt/internal/config"
|
|
|
|
"git.a71.su/Andrew71/hibiscus-txt/internal/files"
|
|
|
|
"github.com/go-chi/chi/v5"
|
2024-03-28 10:21:04 +03:00
|
|
|
)
|
|
|
|
|
2024-06-17 00:54:55 +03:00
|
|
|
// HandleWrite handles error in output of ResponseWriter.Write.
|
2024-03-28 10:21:04 +03:00
|
|
|
func HandleWrite(_ int, err error) {
|
|
|
|
if err != nil {
|
|
|
|
slog.Error("error writing response", "error", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-17 00:54:55 +03:00
|
|
|
// GetFileApi returns raw contents of a file.
|
|
|
|
func GetFileApi(filename string, w http.ResponseWriter) {
|
2024-10-21 16:46:25 +03:00
|
|
|
fileContents, err := files.Read(filename)
|
2024-03-28 10:21:04 +03:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
http.Error(w, "file not found", http.StatusNotFound)
|
|
|
|
} else {
|
|
|
|
http.Error(w, "error reading found", http.StatusNotFound)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
HandleWrite(w.Write(fileContents))
|
|
|
|
}
|
|
|
|
|
2024-06-17 00:54:55 +03:00
|
|
|
// PostFileApi writes contents of Request.Body to a file.
|
|
|
|
func PostFileApi(filename string, w http.ResponseWriter, r *http.Request) {
|
2024-03-28 10:21:04 +03:00
|
|
|
body, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
HandleWrite(w.Write([]byte("error reading body")))
|
|
|
|
return
|
|
|
|
}
|
2024-10-21 16:46:25 +03:00
|
|
|
err = files.Save(filename, body)
|
2024-03-28 10:21:04 +03:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
HandleWrite(w.Write([]byte("error saving file")))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
HandleWrite(w.Write([]byte("wrote to file")))
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2024-06-17 00:54:55 +03:00
|
|
|
// GetFileList returns JSON list of filenames in a directory without extensions or path.
|
2024-03-28 10:21:04 +03:00
|
|
|
func GetFileList(directory string, w http.ResponseWriter) {
|
2024-10-21 16:46:25 +03:00
|
|
|
filenames, err := files.List(directory)
|
2024-03-28 10:21:04 +03:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "error searching for files", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
filenamesJson, err := json.Marshal(filenames)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "error marshaling json", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
HandleWrite(w.Write(filenamesJson))
|
|
|
|
}
|
|
|
|
|
2024-06-17 00:54:55 +03:00
|
|
|
// GetDayApi returns raw contents of a daily file specified in URL.
|
2024-03-28 10:21:04 +03:00
|
|
|
func GetDayApi(w http.ResponseWriter, r *http.Request) {
|
|
|
|
dayString := chi.URLParam(r, "day")
|
|
|
|
if dayString == "" {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
HandleWrite(w.Write([]byte("day not specified")))
|
|
|
|
return
|
|
|
|
}
|
2024-10-21 16:46:25 +03:00
|
|
|
GetFileApi(files.DataFile("day/"+dayString), w)
|
2024-03-28 10:21:04 +03:00
|
|
|
}
|
|
|
|
|
2024-06-17 00:54:55 +03:00
|
|
|
// GetNoteApi returns contents of a note specified in URL.
|
2024-03-30 14:22:03 +03:00
|
|
|
func GetNoteApi(w http.ResponseWriter, r *http.Request) {
|
2024-03-28 10:21:04 +03:00
|
|
|
noteString := chi.URLParam(r, "note")
|
|
|
|
if noteString == "" {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
HandleWrite(w.Write([]byte("note not specified")))
|
|
|
|
return
|
|
|
|
}
|
2024-10-21 16:46:25 +03:00
|
|
|
GetFileApi(files.DataFile("notes/"+noteString), w)
|
2024-03-28 10:21:04 +03:00
|
|
|
}
|
|
|
|
|
2024-06-17 00:54:55 +03:00
|
|
|
// PostNoteApi writes contents of Request.Body to a note specified in URL.
|
2024-03-30 14:22:03 +03:00
|
|
|
func PostNoteApi(w http.ResponseWriter, r *http.Request) {
|
2024-03-28 10:21:04 +03:00
|
|
|
noteString := chi.URLParam(r, "note")
|
|
|
|
if noteString == "" {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
HandleWrite(w.Write([]byte("note not specified")))
|
|
|
|
return
|
|
|
|
}
|
2024-10-21 16:46:25 +03:00
|
|
|
PostFileApi(files.DataFile("notes/"+noteString), w, r)
|
2024-03-28 10:21:04 +03:00
|
|
|
}
|
2024-05-06 14:53:18 +03:00
|
|
|
|
2024-06-17 00:54:55 +03:00
|
|
|
// GraceActiveApi returns "true" if grace period is active, and "false" otherwise.
|
2024-05-06 14:53:18 +03:00
|
|
|
func GraceActiveApi(w http.ResponseWriter, r *http.Request) {
|
|
|
|
value := "false"
|
2024-10-21 16:46:25 +03:00
|
|
|
if config.Cfg.Grace() {
|
2024-05-06 14:53:18 +03:00
|
|
|
value = "true"
|
|
|
|
}
|
|
|
|
HandleWrite(w.Write([]byte(value)))
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
2024-10-21 16:46:25 +03:00
|
|
|
|