NETRAVE/docker/netrave-protohandler/config_manager.rb
VetheonGames 5df588674e There's a lot here...
So, let's get into it.
First, we are straying from the in-memory ideation of storing all our pcap chunks in the memory
I fear that we might bottle up the ram if we depend solely on it.
So, i've made things default to sqlite flatfile, with the option for both remote and in memory
This way, dependant on the users needs, they can use a different implementation.

That's why the creation of a great deal of files and an entire new directory space
I've almost finished implementing the database system into itself, but I want the DB system
to be largely independent of the rest of the system. Because, you know me,
I am a fan of encapsulation. and I like making software coded to be independent.

Yes, if the rest of the system isn't working, the database has nothing to docker
but at least it makes the system more robust, if even a major system can collapse,
and the system as a whole remains functional.

Philisophically, I find this is largely lacking in modern software, and I believe
that these practices will see the protohandler performing much better than we would expect
Since this system as a whole will be independent of the main NETRAVE system,
it opens the door for further expanding, or perhaps even completely repurposing
this protocol going forward.
2023-10-02 15:31:36 -06:00

54 lines
1.8 KiB
Ruby

# frozen_string_literal: true
require 'json'
require 'singleton'
module ProtoConfig
class ConfigManager
include Singleton
attr_reader :recently_connected_time, :listen_ip, :listen_port, :db_type, :db_params, :db_username, :db_password
def initialize
# Initialize with default settings
@recently_connected_time = 120 # default value
@listen_ip = '0.0.0.0' # default value
@listen_port = 3080 # default value
@db_type = 'sqlite'
@db_params = { username: nil, password: nil } # default value
# Check if the config file exists in the current working directory
config_path = 'config.json'
create_default_config(config_path) unless File.exist?(config_path)
load_config(config_path)
end
def create_default_config(config_path)
default_config = {
'recently_connected_time' => @recently_connected_time,
'listen_ip' => @listen_ip,
'listen_port' => @listen_port,
'db_type' => @db_type,
'db_params' => @db_params,
'db_username' => @db_username,
'db_password' => @db_password
}
File.write(config_path, JSON.pretty_generate(default_config))
end
def load_config(config_path)
# Load configuration from a JSON file
config_data = JSON.parse(File.read(config_path))
@recently_connected_time = config_data['recently_connected_time'] if config_data['recently_connected_time']
@listen_ip = config_data['listen_ip'] if config_data['listen_ip']
@listen_port = config_data['listen_port'] if config_data['listen_port']
@db_type = config_data['db_type'] if config_data['db_type']
@db_params = config_data['db_params'] if config_data['db_params']
rescue StandardError => e
puts "Failed to load configuration: #{e.message}"
end
end
end