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