Skip to content

Commit

Permalink
refactor: plugin lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
haveachin committed Dec 27, 2022
1 parent 9873d16 commit 4329570
Show file tree
Hide file tree
Showing 13 changed files with 328 additions and 173 deletions.
24 changes: 11 additions & 13 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import (
const (
devEnvironment = "dev"
prodEnvironment = "prod"

configDefaultPath = "configs/"
)

var (
Expand Down Expand Up @@ -64,7 +62,7 @@ var (
)

if _, err := os.Stat(configPath); err != nil && errors.Is(err, os.ErrNotExist) {
if err := safeWriteFromEmbeddedFS(configDefaultPath, "."); err != nil {
if err := safeWriteFromEmbeddedFS("configs", "."); err != nil {
return err
}
}
Expand Down Expand Up @@ -103,25 +101,25 @@ var (

eventBus := event.NewInternalBus()
pluginManager = infrared.PluginManager{
Proxies: proxies,
Plugins: []infrared.Plugin{
&webhook.Plugin{},
&prometheus.Plugin{},
&api.Plugin{},
&traffic_limiter.Plugin{},
},
Proxies: proxies,
Logger: logger,
EventBus: eventBus,
}
logger.Info("loading plugins")
pluginManager.RegisterPlugin(&webhook.Plugin{})
pluginManager.RegisterPlugin(&prometheus.Plugin{})
pluginManager.RegisterPlugin(&api.Plugin{})
pluginManager.RegisterPlugin(&traffic_limiter.Plugin{})

logger.Debug("loading plugins")
pluginManager.LoadPlugins(data)
logger.Info("enabling plugins")
logger.Debug("enabling plugins")
pluginManager.EnablePlugins()
defer pluginManager.DisablePlugins()

logger.Info("starting proxies")
logger.Debug("starting proxies")
for _, proxy := range proxies {
go proxy.ListenAndServe(eventBus, logger)
defer proxy.Close()
}

sc := make(chan os.Signal, 1)
Expand Down
9 changes: 9 additions & 0 deletions configs/plugins/api.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
# Enables the API to access real time data.
#
#api:
# Enables the plugin
#
#enable: true

# The address that the server binds to
#
#bind: :8080

# Sets this in the allowed origin header
#
#allowedOrigins:
# - "http://*"
# - "https://*"
6 changes: 6 additions & 0 deletions configs/plugins/prometheus.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# Enables the prometheus server for real time metric tracking.
#
#prometheus:
# Enables the plugin
#
#enable: true

# The address that the server binds to
#
#bind: :9070
80 changes: 46 additions & 34 deletions configs/plugins/traffic_limiter.yml
Original file line number Diff line number Diff line change
@@ -1,37 +1,49 @@
#trafficLimiters:
#default:
# Server IDs to watch the traffic
# Configuration for traffic limiters
#
#trafficLimiter:
# Enables the plugin
#
#enable: true

# A map of traffic limiter configurations
#
#trafficLimiters:
# This is just an ID. In this case "default",
# but you can give it a name that makes sense for you.
#
#serverIds:
# - default

# File is used to persistently store data
#
#file: bandwidth.yml

# This is the amount of traffic until the server gets limited.
# Valid sizes are B, KB, MB, GB, TB, PB and EB.
#
#trafficLimit: 1TB
#default:
# Server IDs to watch the traffic
#
#serverIds:
# - default

# File is used to persistently store data
#
#file: bandwidth.yml

# This is the amount of traffic until the server gets limited.
# Valid sizes are B, KB, MB, GB, TB, PB and EB.
#
#trafficLimit: 1TB

# Sets the schedule for the job that resets the consumed bytes from the traffic limiter.
# For more info on the Cron sysntax see here: https://en.wikipedia.org/wiki/Cron
#
#resetCron: "@monthly"

# The message that is displayed to a client when they try to connect
# but the server is out of bandwidth.
#
#outOfBandwidthMessage: Sorry {{username}}, but the server is out of bandwidth.
# Sets the schedule for the job that resets the consumed bytes from the traffic limiter.
# For more info on the Cron sysntax see here: https://en.wikipedia.org/wiki/Cron
#
#resetCron: "@monthly"
# The message that is displayed to a client when they try to connect
# but the server is out of bandwidth.
#
#outOfBandwidthMessage: Sorry {{username}}, but the server is out of bandwidth.

# This is the ping response that clients see of your server when it is out of bandwidth.
#
#outOfBandwidthStatus:
#versionName: Infrared
#protocolNumber: 0
#maxPlayerCount: 0
#playerCount: 0
#iconPath: icons/default.png
#motd: |
# Powered by Infrared
# §6Server at {{serverDomain}} is out of bandwidth.
# This is the ping response that clients see of your server when it is out of bandwidth.
#
#outOfBandwidthStatus:
#versionName: Infrared
#protocolNumber: 0
#maxPlayerCount: 0
#playerCount: 0
#iconPath: icons/default.png
#motd: |
# Powered by Infrared
# §6Server at {{serverDomain}} is out of bandwidth.
62 changes: 35 additions & 27 deletions configs/plugins/webhook.yml
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
# Configuration for webhooks
#
#webhooks:
# A webhook defined by an ID. In this case "default"
#webhook:
# Enables the plugin
#
#default:
# Gateway IDs to listen for events to. All events that the connection triggers
# have also the gateway ID attached to them.
#enable: true

# A map of webhook configurations to webhooks
#
#webhooks:
# This is just an ID. In this case "default",
# but you can give it a name that makes sense for you.
#
#gatewayIds:
# - default
#default:
# Gateway IDs to listen for events to. All events that the connection triggers
# have also the gateway ID attached to them.
#
#gatewayIds:
# - default

# The URL to POST the JSON to
#
#url: https://example.com/callback
# The URL to POST the JSON to
#
#url: https://example.com/callback

# Dial timeout is the amount of time that Infrared waits for it cancels the POST request.
# This is best set in defaults to not have to copy-paste this all the time. See bottom of the file.
#
#dialTimeout: 1s

# Event Topics to listen for.
# Available events are:
# - AcceptedConn - When a new connection to Infrared is made.
# - PreProcessing - Before the new connection is processed.
# - PostProcessing - After a connection is processed.
# - PrePlayerJoin - Before a client is connecting to the target server.
# - PlayerJoin - When a player joins a server.
# - PlayerLeave - When a player leaves the server.
#
#events:
# - PlayerJoin
# - PlayerLeave
# Dial timeout is the amount of time that Infrared waits for it cancels the POST request.
# This is best set in defaults to not have to copy-paste this all the time. See bottom of the file.
#
#dialTimeout: 1s
# Event Topics to listen for.
# Available events are:
# - AcceptedConn - When a new connection to Infrared is made.
# - PreProcessing - Before the new connection is processed.
# - PostProcessing - After a connection is processed.
# - PrePlayerJoin - Before a client is connecting to the target server.
# - PlayerJoin - When a player joins a server.
# - PlayerLeave - When a player leaves the server.
#
#events:
# - PlayerJoin
# - PlayerLeave

# Defaults are global default values that can be
#
Expand Down
Loading

0 comments on commit 4329570

Please sign in to comment.