# TODO: Write documentation for `Kotatsu` require "option_parser" require "http/server" require "json" module Kotatsu VERSION = "0.1.0" CONFIG_DIR = Path.new(Path.home, ".config", "kotatsu") CONFIG_FILE = Path.new(CONFIG_DIR, "config.json") def self.buildConfig() conf = JSON.build do |json| json.object do json.field "config_version", 1 json.field "port", 8080 json.field "title", "Kotatsu" json.field "services" do json.array do json.array do json.string "Peko! Peko! Peko!" json.string "https://peko.dance/" json.string "none" end json.array do json.string "Entertainment Company" json.string "https://hololive.dance/" json.string "none" end end end end end return conf end def self.makeConfig(file, dir) puts "Creating new config file in #{file}" if Dir.exists?(dir) == false Dir.mkdir(dir) if Dir.exists?(dir) == false STDERR.puts "ERROR! Could not create directory: #{dir}" exit(1) end end File.write(file, buildConfig()) if File.exists?(file) == false STDERR.puts "ERROR! Could not create file: #{file}" exit(1) end end OptionParser.parse do |parser| parser.banner = "Kotatsu - Easy to configure server start page" parser.on "-h", "--help", "Show this help" do puts parser exit end parser.on "-v", "--version", "Show kotatsu version" do puts VERSION exit end parser.on "-mc", "--make-config", "Creates config file without running kotatsu" do if File.exists?(CONFIG_FILE) == false makeConfig(CONFIG_FILE, CONFIG_DIR) exit else puts "Config already exists" exit end end parser.on "list", "Lists all services" do if File.exists?(CONFIG_FILE) == true conf = JSON.parse(File.read(CONFIG_FILE)) services = conf["services"].as_a i = 0 while services.size > i puts "##{i}: #{services[i][0]} ; #{services[i][1]} ; #{services[i][2]}" i+=1 end exit else STDERR.puts "ERROR! Config file not found: #{CONFIG_FILE}" exit(1) end end parser.invalid_option do |flag| STDERR.puts "ERROR! Not a valid option: #{flag}" STDERR.puts "Use -h or --help for all available options" exit(1) end end if File.exists?(CONFIG_FILE) == false puts "Could not find config file" makeConfig(CONFIG_FILE, CONFIG_DIR) end webserv = HTTP::Server.new do |context| context.response.content_type = "text/plain" context.response.print "Running kotatsu #{VERSION}" end webaddr = webserv.bind_tcp 8080 puts "Started kotatsu webserver on #{webaddr}" webserv.listen end