code cleanup

This commit is contained in:
2021-12-30 22:55:57 +01:00
parent 4fc6ac892f
commit f21da31561

View File

@@ -9,7 +9,11 @@ module Kotatsu
CONFIG_DIR = Path.new(Path.home, ".config", "kotatsu") CONFIG_DIR = Path.new(Path.home, ".config", "kotatsu")
CONFIG_FILE = Path.new(CONFIG_DIR, "config.json") CONFIG_FILE = Path.new(CONFIG_DIR, "config.json")
def self.buildConfig() #to be deprecated # -- METHODS --
#Creates the initial config from hardcoded data
#TO BE DEPRECATED
def self.buildConfig()
conf = JSON.build do |json| conf = JSON.build do |json|
json.object do json.object do
json.field "config_version", 1 json.field "config_version", 1
@@ -34,6 +38,7 @@ module Kotatsu
return conf return conf
end end
#Writes the config file in JSON format
def self.writeConfig(version, port, title, services) def self.writeConfig(version, port, title, services)
i = 0 i = 0
conf = JSON.build do |json| conf = JSON.build do |json|
@@ -58,12 +63,12 @@ module Kotatsu
File.write(CONFIG_FILE, conf) File.write(CONFIG_FILE, conf)
end end
#Reads the JSON config and returns it as various variables
def self.readConfig() def self.readConfig()
conf = JSON.parse(File.read(CONFIG_FILE)) conf = JSON.parse(File.read(CONFIG_FILE))
version = conf["config_version"].as_i version = conf["config_version"].as_i
port = conf["port"].as_i port = conf["port"].as_i
title = conf["title"].as_s title = conf["title"].as_s
t_services = conf["services"].as_a t_services = conf["services"].as_a
services = [] of Array(String) services = [] of Array(String)
i = 0 i = 0
@@ -71,10 +76,10 @@ module Kotatsu
services << [conf["services"][i][0].as_s, conf["services"][i][1].as_s, conf["services"][i][2].as_s] services << [conf["services"][i][0].as_s, conf["services"][i][1].as_s, conf["services"][i][2].as_s]
i+=1 i+=1
end end
return version, port, title, services return version, port, title, services
end end
#Checks if config dir exists and then creates a config file
def self.makeConfig(file, dir) def self.makeConfig(file, dir)
puts "Creating new config file in #{file}" puts "Creating new config file in #{file}"
if Dir.exists?(dir) == false if Dir.exists?(dir) == false
@@ -91,6 +96,8 @@ module Kotatsu
end end
end end
# -- PARSER --
OptionParser.parse do |parser| OptionParser.parse do |parser|
parser.banner = "Kotatsu - Easy to configure server start page" parser.banner = "Kotatsu - Easy to configure server start page"
@@ -163,18 +170,23 @@ module Kotatsu
end end
end end
# -- MAIN --
#Checks if a config exists, if not runs makeConfig
if File.exists?(CONFIG_FILE) == false if File.exists?(CONFIG_FILE) == false
puts "Could not find config file" puts "Could not find config file"
makeConfig(CONFIG_FILE, CONFIG_DIR) makeConfig(CONFIG_FILE, CONFIG_DIR)
end end
#Reads config file
c_version, c_port, c_title, c_services = readConfig()
#Run http server
webserv = HTTP::Server.new do |context| webserv = HTTP::Server.new do |context|
context.response.content_type = "text/plain" context.response.content_type = "text/plain"
context.response.print "Running kotatsu #{VERSION}" context.response.print "Running kotatsu #{VERSION}"
end end
webaddr = webserv.bind_tcp c_port
webaddr = webserv.bind_tcp 8080
puts "Started kotatsu webserver on #{webaddr}" puts "Started kotatsu webserver on #{webaddr}"
webserv.listen webserv.listen
end end