Add config reload option

This commit is contained in:
Andrew-71 2024-05-07 14:55:11 +03:00
parent fa292154f6
commit 806822a9a8
8 changed files with 27 additions and 3 deletions

View file

@ -1,6 +1,11 @@
# Changelog # Changelog
This file keeps track of changes in more human-readable fashion This file keeps track of changes in more human-readable fashion
## v0.2.0
* Added config reload
* Can be reloaded in info page
* Can be reloaded with new `reload` api method (be aware of the redirect if referer is present)
## 7 May 2024 - v0.1.0 ## 7 May 2024 - v0.1.0
* Began move towards [semantic versioning](https://semver.org/) * Began move towards [semantic versioning](https://semver.org/)
* Current version is now 0.1.0 * Current version is now 0.1.0

View file

@ -98,4 +98,5 @@ POST /readme - save request body into readme.txt
GET /export - get .zip archive of entire data directory GET /export - get .zip archive of entire data directory
GET /grace - "true" if grace period is active, otherwise "false" GET /grace - "true" if grace period is active, otherwise "false"
GET /version - get app's version GET /version - get app's version
GET /reload - reload app config
``` ```

14
api.go
View file

@ -122,3 +122,17 @@ func GetVersionApi(w http.ResponseWriter, r *http.Request) {
HandleWrite(w.Write([]byte(Info.Version))) HandleWrite(w.Write([]byte(Info.Version)))
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
// ConfigReloadApi reloads the config
func ConfigReloadApi(w http.ResponseWriter, r *http.Request) {
err := Cfg.Reload()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
HandleWrite(w.Write([]byte(err.Error())))
}
if r.Referer() != "" {
http.Redirect(w, r, r.Header.Get("Referer"), 302)
return
}
w.WriteHeader(http.StatusOK)
}

View file

@ -20,5 +20,6 @@
"info.version": "Version", "info.version": "Version",
"info.version.link": "source and changelog", "info.version.link": "source and changelog",
"info.export": "Export data", "info.export": "Export data",
"info.readme": "Edit readme.txt" "info.readme": "Edit readme.txt",
"info.reload": "Reload config"
} }

View file

@ -20,5 +20,6 @@
"info.version": "Версия", "info.version": "Версия",
"info.version.link": "исходный код", "info.version.link": "исходный код",
"info.export": "Экспорт данных", "info.export": "Экспорт данных",
"info.readme": "Редактировать readme.txt" "info.readme": "Редактировать readme.txt",
"info.reload": "Перезагрузить конфиг"
} }

View file

@ -15,7 +15,7 @@ type HibiscusInfo struct {
// Info contains app information // Info contains app information
var Info = HibiscusInfo{ var Info = HibiscusInfo{
Version: "0.1.0", Version: "0.2.0",
SourceLink: "https://git.a71.su/Andrew71/hibiscus", SourceLink: "https://git.a71.su/Andrew71/hibiscus",
} }

View file

@ -4,5 +4,6 @@
<li>{{ translatableText "info.version" }} - {{ .Version }} (<a href="{{ .SourceLink }}">{{ translatableText "info.version.link" }}</a>)</li> <li>{{ translatableText "info.version" }} - {{ .Version }} (<a href="{{ .SourceLink }}">{{ translatableText "info.version.link" }}</a>)</li>
<li><a href="/api/export" download="hibiscus">{{ translatableText "info.export" }}</a></li> <li><a href="/api/export" download="hibiscus">{{ translatableText "info.export" }}</a></li>
<li><a href="/readme">{{ translatableText "info.readme" }}</a></li> <li><a href="/readme">{{ translatableText "info.readme" }}</a></li>
<li><a href="/api/reload">{{ translatableText "info.reload" }}</a></li>
</ul> </ul>
{{end}} {{end}}

View file

@ -45,6 +45,7 @@ func Serve() {
apiRouter.Get("/export", GetExport) apiRouter.Get("/export", GetExport)
apiRouter.Get("/grace", GraceActiveApi) apiRouter.Get("/grace", GraceActiveApi)
apiRouter.Get("/version", GetVersionApi) apiRouter.Get("/version", GetVersionApi)
apiRouter.Get("/reload", ConfigReloadApi)
r.Mount("/api", apiRouter) r.Mount("/api", apiRouter)
// Static files // Static files