mirror of
https://github.com/akyaiy/GoSally-mvp.git
synced 2026-01-03 08:32:24 +00:00
Compare commits
4 Commits
3cbea14e84
...
2f071c25b2
| Author | SHA1 | Date | |
|---|---|---|---|
| 2f071c25b2 | |||
| d23fd32e84 | |||
| 86d35a9ede | |||
| c77d51a95c |
@@ -4,44 +4,51 @@
|
|||||||
---@alias AnyTable table<string, Any>
|
---@alias AnyTable table<string, Any>
|
||||||
|
|
||||||
--- Global session module interface
|
--- Global session module interface
|
||||||
|
---@class SessionIn
|
||||||
|
---@field params AnyTable Request parameters
|
||||||
|
|
||||||
|
---@class SessionOut
|
||||||
|
---@field result Any|string? Result payload (table or primitive)
|
||||||
|
---@field error { code: integer, message: string, data: Any }? Optional error info
|
||||||
|
|
||||||
---@class SessionModule
|
---@class SessionModule
|
||||||
---@field request AnyTable Input context (read-only)
|
---@field request SessionIn Input context (read-only)
|
||||||
---@field request.params AnyTable Request parameters
|
---@field response SessionOut Output context (write results/errors)
|
||||||
---@field response AnyTable Output context (write results/errors)
|
|
||||||
---@field response.result Any|string? Result payload (table or primitive)
|
|
||||||
---@field response.error { code: integer, message: string, data: any }? Optional error info
|
|
||||||
|
|
||||||
--- Global log module interface
|
--- Global log module interface
|
||||||
---@class LogModule
|
---@class LogModule
|
||||||
---@field info fun(msg: string) Log informational message
|
---@field info fun(msg: string) Log informational message
|
||||||
---@field debug fun(msg: string) Log debug message
|
---@field debug fun(msg: string) Log debug message
|
||||||
---@field error fun(msg: string) Log error message
|
---@field error fun(msg: string) Log error message
|
||||||
---@field warn fun(msg: string) Log warning message
|
---@field warn fun(msg: string) Log warning message
|
||||||
---@field event fun(msg: string) Log event (generic)
|
---@field event fun(msg: string) Log event (generic)
|
||||||
---@field event_error fun(msg: string) Log event error
|
---@field event_error fun(msg: string) Log event error
|
||||||
---@field event_warn fun(msg: string) Log event warning
|
---@field event_warn fun(msg: string) Log event warning
|
||||||
|
|
||||||
--- Global net module interface
|
--- Global net module interface
|
||||||
---@class HttpResponse
|
---@class HttpResponse
|
||||||
---@field status integer HTTP status code
|
---@field status integer HTTP status code
|
||||||
---@field status_text string HTTP status text
|
---@field status_text string HTTP status text
|
||||||
---@field body string Response body
|
---@field body string Response body
|
||||||
---@field content_length integer Content length
|
---@field content_length integer Content length
|
||||||
---@field headers AnyTable Map of headers
|
---@field headers AnyTable Map of headers
|
||||||
|
|
||||||
---@class HttpModule
|
---@class HttpModule
|
||||||
---@field get fun(log: boolean, url: string): HttpResponse, string? Perform GET
|
---@field get fun(log: boolean, url: string): HttpResponse, string? Perform GET
|
||||||
---@field post fun(log: boolean, url: string, content_type: string, payload: string): HttpResponse, string? Perform POST
|
---@field post fun(log: boolean, url: string, content_type: string, payload: string): HttpResponse, string? Perform POST
|
||||||
|
|
||||||
---@class NetModule
|
---@class NetModule
|
||||||
---@field http HttpModule HTTP client functions
|
---@field http HttpModule HTTP client functions
|
||||||
|
|
||||||
--- Exposed globals
|
--- Global variables declaration
|
||||||
|
---@global
|
||||||
---@type SessionModule
|
---@type SessionModule
|
||||||
session = session or {}
|
_G.session = session or {}
|
||||||
|
|
||||||
|
---@global
|
||||||
---@type LogModule
|
---@type LogModule
|
||||||
log = log or {}
|
_G.log = log or {}
|
||||||
|
|
||||||
|
---@global
|
||||||
---@type NetModule
|
---@type NetModule
|
||||||
net = net or {}
|
_G.net = net or {}
|
||||||
@@ -193,8 +193,10 @@ func InitConfigReplHook(_ context.Context, cs *corestate.CoreState, x *app.AppX)
|
|||||||
replacements := map[string]any{
|
replacements := map[string]any{
|
||||||
"%tmp%": filepath.Clean(run_manager.RuntimeDir()),
|
"%tmp%": filepath.Clean(run_manager.RuntimeDir()),
|
||||||
"%path%": *x.Config.Env.NodePath,
|
"%path%": *x.Config.Env.NodePath,
|
||||||
"%stdout%": os.Stdout,
|
"%stdout%": "_1STDout",
|
||||||
"%stderr%": os.Stderr,
|
"%stderr%": "_2STDerr",
|
||||||
|
"%1%": "_1STDout",
|
||||||
|
"%2%": "_2STDerr",
|
||||||
}
|
}
|
||||||
|
|
||||||
processConfig(&x.Config.Conf, replacements)
|
processConfig(&x.Config.Conf, replacements)
|
||||||
@@ -301,10 +303,32 @@ func processConfig(conf any, replacements map[string]any) error {
|
|||||||
|
|
||||||
case reflect.Ptr:
|
case reflect.Ptr:
|
||||||
if !val.IsNil() {
|
if !val.IsNil() {
|
||||||
return processConfig(val.Interface(), replacements)
|
elem := val.Elem()
|
||||||
|
if elem.Kind() == reflect.String {
|
||||||
|
str := elem.String()
|
||||||
|
if replacement, exists := replacements[str]; exists {
|
||||||
|
strVal, err := toString(replacement)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("cannot convert replacement to string: %v", err)
|
||||||
|
}
|
||||||
|
elem.SetString(strVal)
|
||||||
|
} else {
|
||||||
|
for placeholder, replacement := range replacements {
|
||||||
|
if strings.Contains(str, placeholder) {
|
||||||
|
replacementStr, err := toString(replacement)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("invalid replacement for %q: %v", placeholder, err)
|
||||||
|
}
|
||||||
|
newStr := strings.ReplaceAll(str, placeholder, replacementStr)
|
||||||
|
elem.SetString(newStr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return processConfig(elem.Addr().Interface(), replacements)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ func (c *Compositor) LoadConf(path string) error {
|
|||||||
v.SetDefault("updates.wanted_version", "latest-stable")
|
v.SetDefault("updates.wanted_version", "latest-stable")
|
||||||
v.SetDefault("log.json_format", "false")
|
v.SetDefault("log.json_format", "false")
|
||||||
v.SetDefault("log.level", "info")
|
v.SetDefault("log.level", "info")
|
||||||
v.SetDefault("log.output", "%stdout%")
|
v.SetDefault("log.output", "%1%")
|
||||||
|
|
||||||
if err := v.ReadInConfig(); err != nil {
|
if err := v.ReadInConfig(); err != nil {
|
||||||
return fmt.Errorf("error reading config: %w", err)
|
return fmt.Errorf("error reading config: %w", err)
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ type Updates struct {
|
|||||||
type Log struct {
|
type Log struct {
|
||||||
JSON *bool `mapstructure:"json_format"`
|
JSON *bool `mapstructure:"json_format"`
|
||||||
Level *string `mapstructure:"level"`
|
Level *string `mapstructure:"level"`
|
||||||
OutPath any `mapstructure:"output"`
|
OutPath *string `mapstructure:"output"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConfigEnv structure for environment variables
|
// ConfigEnv structure for environment variables
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ package logs
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
@@ -58,30 +57,14 @@ func SetupLogger(o *config.Log) (*slog.Logger, error) {
|
|||||||
handlerOpts.Level = slog.LevelInfo
|
handlerOpts.Level = slog.LevelInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
switch o.OutPath {
|
switch *o.OutPath {
|
||||||
case 1:
|
case "_1STDout":
|
||||||
writer = os.Stdout
|
writer = os.Stdout
|
||||||
case 2:
|
case "_2STDerr":
|
||||||
writer = os.Stderr
|
|
||||||
case os.Stdout:
|
|
||||||
writer = os.Stdout
|
|
||||||
case os.Stderr:
|
|
||||||
writer = os.Stderr
|
writer = os.Stderr
|
||||||
default:
|
default:
|
||||||
var path string
|
|
||||||
switch v := o.OutPath.(type) {
|
|
||||||
case string:
|
|
||||||
path = v
|
|
||||||
case int, int64, float64:
|
|
||||||
path = fmt.Sprint(v)
|
|
||||||
case fmt.Stringer:
|
|
||||||
path = v.String()
|
|
||||||
default:
|
|
||||||
path = fmt.Sprint(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
logFile := &lumberjack.Logger{
|
logFile := &lumberjack.Logger{
|
||||||
Filename: filepath.Join(path, "event.log"),
|
Filename: filepath.Join(*o.OutPath, "event.log"),
|
||||||
MaxSize: 10,
|
MaxSize: 10,
|
||||||
MaxBackups: 5,
|
MaxBackups: 5,
|
||||||
MaxAge: 28,
|
MaxAge: 28,
|
||||||
|
|||||||
Reference in New Issue
Block a user