Files
filestash/server/ctrl/config.go
Mickael Kerjean 9171b6fb5e refactoring (config): separate config loading and saving
The idea is that we can now plug and play different logic for loading
and saving configuration data via a go generator built from plugins
2022-02-04 00:51:57 +11:00

68 lines
1.5 KiB
Go

package ctrl
import (
. "github.com/mickael-kerjean/filestash/server/common"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strconv"
)
var (
logpath = filepath.Join(GetCurrentDir(), LOG_PATH, "access.log")
configpath = filepath.Join(GetCurrentDir(), CONFIG_PATH, "config.json")
)
func FetchLogHandler(ctx App, res http.ResponseWriter, req *http.Request) {
file, err := os.OpenFile(logpath, os.O_RDONLY, os.ModePerm)
if err != nil {
SendErrorResult(res, err)
return
}
defer file.Close()
maxSize := req.URL.Query().Get("maxSize")
if maxSize != "" {
cursor := func() int64 {
tmp, err := strconv.Atoi(maxSize)
if err != nil {
return 0
}
return int64(tmp)
}()
for cursor >= 0 {
if _, err := file.Seek(-cursor, io.SeekEnd); err != nil {
break
}
char := make([]byte, 1)
file.Read(char)
if char[0] == 10 || char[0] == 13 { // stop if we find a line
break
}
cursor += 1
}
}
res.Header().Set("Content-Type", "text/plain")
io.Copy(res, file)
}
func PrivateConfigHandler(ctx App, res http.ResponseWriter, req *http.Request) {
SendSuccessResult(res, Config)
}
func PrivateConfigUpdateHandler(ctx App, res http.ResponseWriter, req *http.Request) {
b, _ := ioutil.ReadAll(req.Body)
if err := SaveConfig(b); err != nil {
SendErrorResult(res, err)
return
}
Config.Load()
SendSuccessResult(res, nil)
}
func PublicConfigHandler(ctx App, res http.ResponseWriter, req *http.Request) {
cfg := Config.Export()
SendSuccessResultWithEtagAndGzip(res, req, cfg)
}