chore: tunnel recorder & slight refactor
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2025-09-27 17:49:59 -04:00
parent 20c1388cf4
commit 0722e5f032
17 changed files with 725 additions and 285 deletions

View File

@@ -23,6 +23,8 @@ type ConfigDef struct {
type BaseConfig struct {
ServerAddress string `json:"server" description:"Conduit server address" default:"http://localhost:8080"`
APIKey string `json:"api_key" description:"API Key for the conduit API"`
LogLevel string `json:"log_level" default:"info" description:"Log level"`
LogFormat string `json:"log_format" default:"text" description:"Log format - text or json"`
}
func (c *BaseConfig) Validate() error {
@@ -35,6 +37,9 @@ func (c *BaseConfig) Validate() error {
if _, err := url.Parse(c.ServerAddress); err != nil {
return fmt.Errorf("server is invalid: %w", err)
}
if c.LogFormat != "text" && c.LogFormat != "json" {
return fmt.Errorf("log format must be 'text' or 'json'")
}
return nil
}
@@ -68,13 +73,13 @@ func GetServerConfig(cmdFlags *pflag.FlagSet) (*ServerConfig, error) {
}
cfg := &ServerConfig{
BaseConfig: BaseConfig{
ServerAddress: cfgValues["server"],
APIKey: cfgValues["api_key"],
},
BaseConfig: getBaseConfig(cfgValues),
BindAddress: cfgValues["bind"],
}
// Initialize Logger
initLogger(cfg.BaseConfig)
return cfg, cfg.Validate()
}
@@ -87,14 +92,14 @@ func GetClientConfig(cmdFlags *pflag.FlagSet) (*ClientConfig, error) {
}
cfg := &ClientConfig{
BaseConfig: BaseConfig{
ServerAddress: cfgValues["server"],
APIKey: cfgValues["api_key"],
},
BaseConfig: getBaseConfig(cfgValues),
TunnelName: cfgValues["name"],
TunnelTarget: cfgValues["target"],
}
// Initialize Logger
initLogger(cfg.BaseConfig)
return cfg, cfg.Validate()
}
@@ -108,6 +113,15 @@ func GetVersion() string {
return version
}
func getBaseConfig(cfgValues map[string]string) BaseConfig {
return BaseConfig{
ServerAddress: cfgValues["server"],
APIKey: cfgValues["api_key"],
LogLevel: cfgValues["log_level"],
LogFormat: cfgValues["log_format"],
}
}
func getConfigValue(cmdFlags *pflag.FlagSet, def ConfigDef) string {
// 1. Get Flags First
if cmdFlags != nil {

61
config/logging.go Normal file
View File

@@ -0,0 +1,61 @@
package config
import (
"fmt"
"runtime"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
func initLogger(cfg BaseConfig) {
// Parse Log Level
logLevel, err := log.ParseLevel(cfg.LogLevel)
if err != nil {
logLevel = log.InfoLevel
}
log.SetLevel(logLevel)
// Create Log Formatter
var logFormatter log.Formatter
switch cfg.LogFormat {
case "json":
log.SetReportCaller(true)
logFormatter = &log.JSONFormatter{
TimestampFormat: time.RFC3339,
CallerPrettyfier: prettyCaller,
}
case "text":
logFormatter = &log.TextFormatter{
TimestampFormat: time.RFC3339,
FullTimestamp: true,
}
}
log.SetFormatter(&utcFormatter{logFormatter})
}
func prettyCaller(f *runtime.Frame) (function string, file string) {
purgePrefix := "reichard.io/conduit/"
pathName := strings.Replace(f.Func.Name(), purgePrefix, "", 1)
parts := strings.Split(pathName, ".")
filepath, line := f.Func.FileLine(f.PC)
splitFilePath := strings.Split(filepath, "/")
fileName := fmt.Sprintf("%s/%s@%d", parts[0], splitFilePath[len(splitFilePath)-1], line)
functionName := strings.Replace(pathName, parts[0]+".", "", 1)
return functionName, fileName
}
type utcFormatter struct {
log.Formatter
}
func (cf utcFormatter) Format(e *log.Entry) ([]byte, error) {
e.Time = e.Time.UTC()
return cf.Formatter.Format(e)
}