Improve file closing

This commit is contained in:
Andrew-71 2024-05-04 21:36:43 +03:00
parent f7eecb7b98
commit 78837baad5
4 changed files with 12 additions and 15 deletions

View file

@ -8,15 +8,12 @@ As a result, I can't guarantee that it's either secure or stable.
## Features:
* Each day, you get a text file. You have until 23:59 of that very day to finalise it.
* You can save named notes to document milestones, big events, or just a nice game you played this month
* There is also a readme.txt file (just like this one, except you get to write it!)*
* You can easily export entire `data` dir in a `.zip` archive for backups
* Everything is plain(text) and simple. No databases, encryption, OAuth, or anything fancy. Even the password is plainte- *wait is this a feature?*
* Docker support (in fact, that's probably the best way to run this)
* Optional Telegram notifications for failed login attempts
*only available through API, subject to change and removal
## Technical details
You can read a relevant entry in my blog [here](https://a71.su/notes/hibiscus/).
It provides some useful information and context for why this app exists in the first place.

View file

@ -1,12 +1,8 @@
# TODO
List of things to add to this project
* Fix the weird issue with compose and mounts (absolute path something) when updating!!!
* CI/CD pipeline
* Better docs in case others want to use ths for some reason
* Github/Codeberg/whatever mirror for when `faye` (my server) is offline
* API revamp
* GitHub/Codeberg/whatever mirror for when `faye` (my server) is offline
* Check export function for improvements
* More slog.Debug
* Consider less clunky auth method
* *Go* dependency-less? <-- this is a terrible idea

View file

@ -64,7 +64,6 @@ func (c *Config) Reload() error {
if err != nil {
return err
}
defer file.Close()
options := map[string]string{}
scanner := bufio.NewScanner(file)
@ -78,6 +77,10 @@ func (c *Config) Reload() error {
if err := scanner.Err(); err != nil {
return err
}
err = file.Close()
if err != nil {
return err
}
timezone := "Local" // Timezone is handled separately because reflection
refStruct := reflect.ValueOf(*c)

View file

@ -18,11 +18,8 @@ func Export(filename string) error {
slog.Error("error creating export archive", "error", err)
return err
}
defer file.Close()
w := zip.NewWriter(file)
defer w.Close()
walker := func(path string, info os.FileInfo, err error) error {
if path == filename || filepath.Ext(path) == ".zip" { //Ignore export file itself and .zip archives
return nil
@ -38,7 +35,6 @@ func Export(filename string) error {
if err != nil {
return err
}
defer file.Close()
f, err := w.Create(path)
if err != nil {
@ -50,7 +46,7 @@ func Export(filename string) error {
return err
}
return nil
return file.Close()
}
err = filepath.Walk("data/", walker)
if err != nil {
@ -58,13 +54,18 @@ func Export(filename string) error {
return err
}
return nil
err = file.Close()
if err != nil {
return err
}
return w.Close()
}
// GetExport returns a .zip archive with contents of the data folder
func GetExport(w http.ResponseWriter, r *http.Request) {
err := Export(ExportPath)
if err != nil {
slog.Error("error getting export archive", "error", err)
http.Error(w, "could not export", http.StatusInternalServerError)
return
}