Skip to content

Commit

Permalink
Refactor: instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Loyalsoldier committed Nov 5, 2024
1 parent b1baee0 commit c6acb91
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 21 deletions.
87 changes: 72 additions & 15 deletions lib/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,101 @@ 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 {
return err
}
}

return nil
}

func (i *instance) RunOutput(container Container) error {
for _, oc := range i.output {
if err := oc.Output(container); err != nil {
return err
Expand All @@ -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
}
7 changes: 1 addition & 6 deletions main.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit c6acb91

Please sign in to comment.