From c6acb91607c5b53b13fae193526280d738abf31e Mon Sep 17 00:00:00 2001 From: Loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com> Date: Tue, 5 Nov 2024 21:31:28 +0800 Subject: [PATCH] Refactor: instance --- lib/instance.go | 87 ++++++++++++++++++++++++++++++++++++++++--------- main.go | 7 +--- 2 files changed, 73 insertions(+), 21 deletions(-) diff --git a/lib/instance.go b/lib/instance.go index 5411d4d5..d2f5e00b 100644 --- a/lib/instance.go +++ b/lib/instance.go @@ -4,55 +4,90 @@ import ( "encoding/json" "errors" "os" + "strings" "github.com/tailscale/hujson" ) -type Instance struct { - config *config +type Instance interface { + InitConfig(configFile string) error + InitConfigFromBytes(content []byte) error + AddInput(InputConverter) + AddOutput(OutputConverter) + ResetInput() + ResetOutput() + RunInput(Container) error + RunOutput(Container) error + Run() error +} + +type instance struct { input []InputConverter output []OutputConverter } -func NewInstance() (*Instance, error) { - return &Instance{ - config: new(config), +func NewInstance() (Instance, error) { + return &instance{ input: make([]InputConverter, 0), output: make([]OutputConverter, 0), }, nil } -func (i *Instance) Init(configFile string) error { - content, err := os.ReadFile(configFile) +func (i *instance) InitConfig(configFile string) error { + var content []byte + var err error + configFile = strings.TrimSpace(configFile) + if strings.HasPrefix(strings.ToLower(configFile), "http://") || strings.HasPrefix(strings.ToLower(configFile), "https://") { + content, err = GetRemoteURLContent(configFile) + } else { + content, err = os.ReadFile(configFile) + } if err != nil { return err } + return i.InitConfigFromBytes(content) +} + +func (i *instance) InitConfigFromBytes(content []byte) error { + config := new(config) + // Support JSON with comments and trailing commas content, _ = hujson.Standardize(content) - if err := json.Unmarshal(content, &i.config); err != nil { + if err := json.Unmarshal(content, &config); err != nil { return err } - for _, input := range i.config.Input { + for _, input := range config.Input { i.input = append(i.input, input.converter) } - for _, output := range i.config.Output { + for _, output := range config.Output { i.output = append(i.output, output.converter) } return nil } -func (i *Instance) Run() error { - if len(i.input) == 0 || len(i.output) == 0 { - return errors.New("input type and output type must be specified") - } +func (i *instance) AddInput(ic InputConverter) { + i.input = append(i.input, ic) +} +func (i *instance) AddOutput(oc OutputConverter) { + i.output = append(i.output, oc) +} + +func (i *instance) ResetInput() { + i.input = make([]InputConverter, 0) +} + +func (i *instance) ResetOutput() { + i.output = make([]OutputConverter, 0) +} + +func (i *instance) RunInput(container Container) error { var err error - container := NewContainer() for _, ic := range i.input { container, err = ic.Input(container) if err != nil { @@ -60,6 +95,10 @@ func (i *Instance) Run() error { } } + return nil +} + +func (i *instance) RunOutput(container Container) error { for _, oc := range i.output { if err := oc.Output(container); err != nil { return err @@ -68,3 +107,21 @@ func (i *Instance) Run() error { return nil } + +func (i *instance) Run() error { + if len(i.input) == 0 || len(i.output) == 0 { + return errors.New("input type and output type must be specified") + } + + container := NewContainer() + + if err := i.RunInput(container); err != nil { + return err + } + + if err := i.RunOutput(container); err != nil { + return err + } + + return nil +} diff --git a/main.go b/main.go index b60c3eb8..436988f6 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,3 @@ -// GeoIP generator -// -// Before running this file, the GeoIP database must be downloaded and present. -// To download GeoIP database: https://dev.maxmind.com/geoip/geoip2/geolite2/ -// Inside you will find block files for IPv4 and IPv6 and country code mapping. package main import ( @@ -33,7 +28,7 @@ func main() { log.Fatal(err) } - if err := instance.Init(*configFile); err != nil { + if err := instance.InitConfig(*configFile); err != nil { log.Fatal(err) }