Files
kotatsu/src/kotatsu.cr
2021-12-29 04:36:00 +01:00

62 lines
1.5 KiB
Crystal

# TODO: Write documentation for `Kotatsu`
require "option_parser"
require "http/server"
module Kotatsu
VERSION = "0.1.0"
CONFIG_DIR = Path.new(Path.home, ".config", "kotatsu")
CONFIG_FILE = Path.new(CONFIG_DIR, "config.json")
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, VERSION)
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.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