2024-07-19 20:02:18 +08:00
package config
import (
"fmt"
"os"
"regexp"
"strings"
2024-07-21 23:09:29 +08:00
"time"
2024-07-19 20:02:18 +08:00
"github.com/spf13/viper"
)
var MyConfig * Config
type Config struct {
Nats NatsConfig
Subscription SubscriptionConfig
Timeouts TimeoutsConfig
2024-07-22 16:23:15 +08:00
Hasura HasuraConfig
2024-07-19 20:02:18 +08:00
}
type NatsConfig struct {
Client string
URL string
Cluster string
}
type SubscriptionConfig struct {
2024-07-20 00:01:04 +08:00
Topic string `mapstructure:"topic"`
2024-07-19 20:02:18 +08:00
QueueGroup string `mapstructure:"queue_group"`
}
type TimeoutsConfig struct {
2024-07-22 12:42:30 +08:00
Server time . Duration `mapstructure:"server"`
ReconnectWait time . Duration `mapstructure:"reconnect_wait"`
Close time . Duration `mapstructure:"close"`
AckWait time . Duration `mapstructure:"ack_wait"`
2024-07-19 20:02:18 +08:00
}
type BodyConfig struct {
Patterns [] PatternConfig
}
type PatternConfig struct {
Pattern string
Comments string
Expression * regexp . Regexp
}
2024-07-22 16:23:15 +08:00
type HasuraConfig struct {
Endpoint string
Secret string
}
2024-07-20 00:01:04 +08:00
const (
2024-07-22 12:42:30 +08:00
EnvProd = "prod"
// logConfigFile = "configs/log_config.json"
2024-07-20 01:17:58 +08:00
// arrPatternString represents the regular expression pattern used to match arrival patterns.
// The pattern matches strings in the format: "(TYPE-NUMBER-SSR-DEPARTURE-ARRIVAL)".
// The pattern captures the following named groups:
2024-07-21 00:25:16 +08:00
// - category: the three-letter category code
2024-07-20 01:17:58 +08:00
// - number: the alphanumeric flight number
// - ssr: the alphanumeric SSR code
// - departure: the four-letter departure airport code
// - arrival: the four-letter arrival airport code
2024-08-06 12:44:32 +08:00
// arrPatternString = `^\((?P<category>[A-Z]{3})` +
// `-(?P<number>[A-Z0-9]+)(\/?(?P<ssr>[A-Z0-9]+))?` +
// `-(?P<dep>[A-Z]{4})` +
// `-(?P<arr>[A-Z]{4})(?P<arr_time>\d{4})\)$`
2024-07-20 01:17:58 +08:00
// depPatternString represents the regular expression pattern used to match departure patterns.
// The pattern matches strings in the format: "(TYPE-NUMBER-SSR-DEPARTURE-DEPARTURE_TIME-ARRIVAL)".
// The pattern captures the following named groups:
2024-07-21 00:25:16 +08:00
// - category: the three-letter category code
2024-07-20 01:17:58 +08:00
// - number: the alphanumeric flight number
// - ssr: the alphanumeric SSR code
// - departure: the four-letter departure airport code
// - departure_time: the four-digit departure time
// - arrival: the four-letter arrival airport code
2024-08-06 12:44:32 +08:00
// depPatternString = `^\((?P<category>[A-Z]{3})-(?P<number>[A-Z0-9]+)(\/(?P<ssr>[A-Z0-9]+))?-(?P<dep>[A-Z]{4})(?P<dep_time>\d{4})-(?P<arr>[A-Z]{4})\)$`
2024-07-20 01:17:58 +08:00
2024-07-24 15:29:48 +08:00
// fplPatternString represents the regular expression pattern used to match flight plan patterns.
// The pattern matches strings in the format: "(CATEGORY-NUMBER-INDICATOR-AIRCRAFT-SURVE-DEPARTURE-DEPARTURE_TIME-SPEED-LEVEL-ROUTE-DESTINATION-ESTT-ALTER-OTHER)".
// The pattern captures the following named groups:
// - category: the three-letter category code
// - number: the alphanumeric flight number
// - indicator: the two-letter indicator
// - aircraft: the alphanumeric aircraft type, optionally followed by a slash and an uppercase letter
// - surve: any character sequence (surveillance information)
// - departure: the four-letter departure airport code
// - departure_time: the four-digit departure time
// - speed: one or more uppercase letters followed by one or more digits (e.g., N123)
// - level: one or more alphanumeric characters (e.g., FL350)
// - route: one or more characters (including newline) representing the flight route
// - destination: the four-letter destination airport code
// - estt: the four-digit estimated time of arrival
// - alter: one or more sequences of a whitespace character followed by exactly four uppercase letters
// - other: any other relevant information, starting with three uppercase letters followed by a forward slash and zero or more characters (including newline)
2024-08-06 12:44:32 +08:00
// fplPatternString = `\((?P<category>[A-Z]{3})-(?P<number>[A-Z]+\d+)-(?P<indicator>[A-Z]{2})\n-(?P<aircraft>[A-Z]+\d+\/?[A-Z]?)\n?-(?P<surve>.*)\n?-(?P<dep>[A-Z]{4})(?P<dep_time>\d{4})\n?-(?P<speed>[A-Z]+\d+)(?P<level>[A-Z0-9]+)\s+(?P<route>(.|\n)+)\n-(?P<dest>[A-Z]{4})(?P<estt>\d{4})\s?(?P<alter>(\s[A-Z]{4})+)\n?-([A-Z]{3}\/(?:[A-Z]{4}\d{4}\s?)+)?(?P<other>(?m)[A-Z]{3}\/(.|\n)*)\)$`
2024-07-24 17:15:35 +08:00
2024-08-06 12:44:32 +08:00
// cnlPatternString = `^\((?P<category>[A-Z]{3})-(?P<number>\w+\d+)-?(?P<dep>[A-Z]{4})?-?(?<arr>[A-Z]{4})\)$`
2024-07-24 17:38:39 +08:00
2024-08-06 12:44:32 +08:00
// dlaPatternString = `^\((?P<category>[A-Z]{3})-(?P<number>\w+\d+)-?(?P<dep>[A-Z]{4})(?P<dep_time>\d{4})?-?(?<arr>[A-Z]{4})(?<arr_time>\d{4})?\)$`
2024-07-20 00:01:04 +08:00
)
// Initialize the bodyPatterns map
2024-08-06 12:44:32 +08:00
// var bodyPatterns = map[string]BodyConfig{
// "ARR": {
// Patterns: []PatternConfig{
// {
// Pattern: arrPatternString,
// Comments: "Pattern for ARR message",
// Expression: regexp.MustCompile(arrPatternString),
// },
// },
// },
// "DEP": {
// Patterns: []PatternConfig{
// {
// Pattern: depPatternString,
// Comments: "Pattern for DEP message",
// Expression: regexp.MustCompile(depPatternString),
// },
// },
// },
// "FPL": {
// Patterns: []PatternConfig{
// {
// Pattern: fplPatternString,
// Comments: "Pattern for FPL message",
// Expression: regexp.MustCompile(fplPatternString),
// },
// },
// },
// "CNL": {
// Patterns: []PatternConfig{
// {
// Pattern: cnlPatternString,
// Comments: "Pattern for CNL message",
// Expression: regexp.MustCompile(cnlPatternString),
// },
// },
// },
// "DLA": {
// Patterns: []PatternConfig{
// {
// Pattern: dlaPatternString,
// Comments: "Pattern for DLA message",
// Expression: regexp.MustCompile(dlaPatternString),
// },
// },
// },
// }
2024-07-20 00:01:04 +08:00
2024-08-06 12:44:32 +08:00
// func GetBodyPatterns() map[string]BodyConfig {
// return bodyPatterns
// }
2024-07-20 00:01:04 +08:00
2024-07-19 20:02:18 +08:00
func SetMyConfig ( cfg * Config ) {
MyConfig = cfg
}
func GetMyConfig () * Config {
if MyConfig == nil {
cfg , err := LoadConfig ()
if err != nil {
2024-07-20 00:01:04 +08:00
fmt . Printf ( "error loading config: %v" , err )
2024-07-19 20:02:18 +08:00
}
MyConfig = cfg
}
return MyConfig
}
// LoadConfig loads the configuration from a file
func LoadConfig () ( * Config , error ) {
2024-07-20 00:01:04 +08:00
// log := utils.Logger
2024-07-19 20:02:18 +08:00
env := os . Getenv ( "GO_ENV" )
if env == "" {
env = "dev"
}
2024-07-20 00:01:04 +08:00
// log.Infof("Environment: %s", env)
2024-07-19 20:02:18 +08:00
viper . SetConfigType ( "toml" )
viper . SetConfigName ( "config." + env )
viper . AddConfigPath ( "configs" )
viper . SetEnvPrefix ( "tele" )
viper . SetEnvKeyReplacer ( strings . NewReplacer ( "." , "_" ))
if err := viper . ReadInConfig (); err != nil {
errMsg := fmt . Sprintf ( "error reading config file for environment '%s': %v" , env , err )
2024-07-20 00:01:04 +08:00
// log.Error(errMsg)
2024-07-19 20:02:18 +08:00
return nil , fmt . Errorf ( errMsg )
}
2024-07-20 00:01:04 +08:00
// log.Debug("Config file read successfully")
// log.Debugf("Config keys: %v", viper.AllKeys())
2024-07-19 20:02:18 +08:00
var config Config
if err := viper . Unmarshal ( & config ); err != nil {
errMsg := fmt . Sprintf ( "unable to decode config into struct for environment '%s': %v" , env , err )
2024-07-20 00:01:04 +08:00
// log.Error(errMsg)
2024-07-19 20:02:18 +08:00
return nil , fmt . Errorf ( errMsg )
}
2024-07-22 12:42:30 +08:00
// loadLoggerConfig()
2024-07-19 20:02:18 +08:00
2024-07-20 00:01:04 +08:00
// log.Debugf("Config loaded: %+v", config)
2024-07-24 11:07:04 +08:00
// loadLoggerConfig()
2024-07-19 20:02:18 +08:00
return & config , nil
}
// ValidateConfig validates the loaded configuration
func ValidateConfig ( cfg * Config ) error {
2024-07-20 00:01:04 +08:00
// log := utils.Logger
2024-07-19 20:02:18 +08:00
if cfg . Nats . Client == "" {
2024-07-20 00:01:04 +08:00
return fmt . Errorf ( "nats client is required" )
2024-07-19 20:02:18 +08:00
}
if cfg . Nats . URL == "" {
2024-07-20 00:01:04 +08:00
return fmt . Errorf ( "nats URL is required" )
2024-07-19 20:02:18 +08:00
}
if cfg . Subscription . Topic == "" {
2024-07-20 00:01:04 +08:00
return fmt . Errorf ( "subscription topic is required" )
2024-07-19 20:02:18 +08:00
}
2024-07-20 08:21:49 +08:00
// fmt.Println("config validation passed")
2024-07-19 20:02:18 +08:00
return nil
}