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: ## Features:
* Each day, you get a text file. You have until 23:59 of that very day to finalise it. * 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 * 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 * 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?* * 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) * Docker support (in fact, that's probably the best way to run this)
* Optional Telegram notifications for failed login attempts * Optional Telegram notifications for failed login attempts
*only available through API, subject to change and removal
## Technical details ## Technical details
You can read a relevant entry in my blog [here](https://a71.su/notes/hibiscus/). 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. It provides some useful information and context for why this app exists in the first place.

View file

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

View file

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

View file

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