Compare commits

...

11 Commits

2 changed files with 170 additions and 7 deletions

11
html/kotatsu.html Normal file
View File

@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>--KOTATSU_TITLE--</title>
</head>
<body>
<h1>--KOTATSU_TITLE--</h1>
--KOTATSU_SERVICES--
</body>
</html>

View File

@@ -1,13 +1,84 @@
# TODO: Write documentation for `Kotatsu`
require "system/user"
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")
UNIX_SOCKET_PATH = Path.new("/tmp", "kotatsu.sock")
# -- 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
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
#Writes the config file in JSON format
def self.writeConfig(version, title, services)
i = 0
conf = JSON.build do |json|
json.object do
json.field "config_version", version
json.field "title", title
json.field "services" do
json.array do
while services.size > i
json.array do
json.string services[i][0]
json.string services[i][1]
json.string services[i][2]
end
i+=1
end
end
end
end
end
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
title = conf["title"].as_s
t_services = conf["services"].as_a
services = [] of Array(String)
i = 0
while t_services.size > i
services << [conf["services"][i][0].as_s, conf["services"][i][1].as_s, conf["services"][i][2].as_s]
i+=1
end
return version, 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
@@ -17,13 +88,30 @@ module Kotatsu
exit(1)
end
end
File.write(file, VERSION)
File.write(file, buildConfig())
if File.exists?(file) == false
STDERR.puts "ERROR! Could not create file: #{file}"
exit(1)
end
end
def self.makeContent(title, services)
content = File.read("./html/kotatsu.html")
content = content.gsub("--KOTATSU_TITLE--", title)
services_c = String.new
i = 0
while services.size > i
services_c = services_c + "<a href=\"#{services[i][1]}\">#{services[i][0]}</a><br>"
i+=1
end
content = content.sub("--KOTATSU_SERVICES--", services_c)
return content
end
# -- PARSER --
OptionParser.parse do |parser|
parser.banner = "Kotatsu - Easy to configure server start page"
@@ -37,6 +125,58 @@ module Kotatsu
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 "add", "Add a service" do
if File.exists?(CONFIG_FILE) == true
version, title, services = readConfig()
print "Name: "
s_name = gets.to_s
print "Link: "
s_link = gets.to_s
print "Icon: "
s_icon = gets.to_s
s_service = [s_name, s_link, s_icon]
services_new = [] of Array(String)
i = 0
while services.size > i
services_new << services[i]
i+=1
end
services_new << s_service
writeConfig(version, title, services_new)
exit
else
STDERR.puts "ERROR! Config file not found: #{CONFIG_FILE}"
exit(1)
end
end
parser.on "list", "Lists all services" do
if File.exists?(CONFIG_FILE) == true
_, _, services = readConfig()
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"
@@ -44,18 +184,30 @@ 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
webserv = HTTP::Server.new do |context|
context.response.content_type = "text/plain"
context.response.print "Running kotatsu #{VERSION}"
#Reads config file
c_version, c_title, c_services = readConfig()
webcontent = makeContent(c_title, c_services)
#Checks if socket file exists and deletes it
if File.exists?(UNIX_SOCKET_PATH) == true
File.delete(UNIX_SOCKET_PATH)
end
webaddr = webserv.bind_tcp 8080
#Run http server
webserv = HTTP::Server.new do |context|
context.response.content_type = "text/html"
context.response << webcontent
end
webaddr = webserv.bind_unix(Socket::UNIXAddress.new(UNIX_SOCKET_PATH.to_s))
puts "Started kotatsu webserver on #{webaddr}"
webserv.listen
end
end