Skip to content

Commit 077695d

Browse files
author
Piotr
committed
saving and loading layout settings to/from logdy.config.json file
1 parent 933aa68 commit 077695d

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

http/client_handlers.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"net/http"
7+
"os"
78
"strconv"
89
"sync"
910
"time"
@@ -14,6 +15,8 @@ import (
1415
"github.com/sirupsen/logrus"
1516
)
1617

18+
const LOGDY_CONFIG_ENV_FILE = "logdy.config.json"
19+
1720
func handleCheckPass(uiPass string) func(w http.ResponseWriter, r *http.Request) {
1821
return func(w http.ResponseWriter, r *http.Request) {
1922
utils.Logger.Debug("/api/check-pass")
@@ -43,8 +46,15 @@ func handleStatus(config *Config) func(w http.ResponseWriter, r *http.Request) {
4346

4447
configStr := ""
4548
if config.ConfigFilePath != "" {
46-
utils.Logger.Debug("Reading config file")
49+
utils.Logger.WithFields(logrus.Fields{
50+
"file": config.ConfigFilePath,
51+
}).Debug("Reading config file")
4752
configStr = utils.LoadFile(config.ConfigFilePath)
53+
} else if utils.FileExists(LOGDY_CONFIG_ENV_FILE) {
54+
utils.Logger.WithFields(logrus.Fields{
55+
"file": LOGDY_CONFIG_ENV_FILE,
56+
}).Info("Loading local env file")
57+
configStr = utils.LoadFile(LOGDY_CONFIG_ENV_FILE)
4858
}
4959

5060
newVersion, _ := utils.IsNewVersionAvailable()
@@ -231,6 +241,40 @@ func handleWs(uiPass string, clients *ClientsStruct) func(w http.ResponseWriter,
231241
}
232242
}
233243

244+
func handleClientSettingsSave() func(w http.ResponseWriter, r *http.Request) {
245+
return func(w http.ResponseWriter, r *http.Request) {
246+
type Req struct {
247+
Layout string `json:"layout"`
248+
}
249+
250+
var p Req
251+
252+
err := json.NewDecoder(r.Body).Decode(&p)
253+
if err != nil {
254+
http.Error(w, err.Error(), http.StatusBadRequest)
255+
return
256+
}
257+
258+
err = os.WriteFile(LOGDY_CONFIG_ENV_FILE, []byte(p.Layout), 0644)
259+
if err != nil {
260+
http.Error(w, err.Error(), http.StatusInternalServerError)
261+
return
262+
}
263+
264+
path, err := os.Getwd()
265+
if err != nil {
266+
http.Error(w, err.Error(), http.StatusInternalServerError)
267+
return
268+
}
269+
270+
w.Header().Set("Content-Type", "application/json")
271+
w.WriteHeader(http.StatusOK)
272+
json.NewEncoder(w).Encode(map[string]string{
273+
"location": path + string(os.PathSeparator) + LOGDY_CONFIG_ENV_FILE,
274+
})
275+
}
276+
}
277+
234278
func handleClientStatus(clients *ClientsStruct) func(w http.ResponseWriter, r *http.Request) {
235279
return func(w http.ResponseWriter, r *http.Request) {
236280
cl := getClientOrErr(r, w, clients)

http/http.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ func HandleHttp(config *Config, clients *ClientsStruct, serveMux hand) {
9898
http.HandleFunc(config.HttpPathPrefix+"api/client/set-status", handleClientStatus(clients))
9999
http.HandleFunc(config.HttpPathPrefix+"api/client/load", handleClientLoad(clients))
100100
http.HandleFunc(config.HttpPathPrefix+"api/client/peek-log", handleClientPeek(clients))
101+
http.HandleFunc(config.HttpPathPrefix+"api/config/save", handleClientSettingsSave())
101102
http.HandleFunc(config.HttpPathPrefix+"ws", handleWs(config.UiPass, clients))
102103

103104
http.HandleFunc(config.HttpPathPrefix+"api/log", apiKeyMiddleware(config.ApiKey, handleLog(Ch)))
@@ -109,6 +110,7 @@ func HandleHttp(config *Config, clients *ClientsStruct, serveMux hand) {
109110
serveMux.HandleFunc(config.HttpPathPrefix+"api/client/set-status", handleClientStatus(clients))
110111
serveMux.HandleFunc(config.HttpPathPrefix+"api/client/load", handleClientLoad(clients))
111112
serveMux.HandleFunc(config.HttpPathPrefix+"api/client/peek-log", handleClientPeek(clients))
113+
http.HandleFunc(config.HttpPathPrefix+"api/config/save", handleClientSettingsSave())
112114
serveMux.HandleFunc(config.HttpPathPrefix+"ws", handleWs(config.UiPass, clients))
113115

114116
serveMux.HandleFunc(config.HttpPathPrefix+"api/log", apiKeyMiddleware(config.ApiKey, handleLog(Ch)))

utils/utils.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ func LoadFile(configFilePath string) string {
2727
return string(bytes)
2828
}
2929

30+
func FileExists(filePath string) bool {
31+
_, err := os.Stat(filePath)
32+
return err == nil
33+
}
34+
3035
func init() {
3136
rand.Seed(time.Now().UnixNano())
3237
}

0 commit comments

Comments
 (0)