Compare commits

...

4 Commits

Author SHA1 Message Date
5934863fa6 changed http server to run on unix socket 2021-12-31 00:21:25 +01:00
1dc1b9c351 removed port from config (for now) 2021-12-30 23:53:46 +01:00
4559f2b67e added html and template creation 2021-12-30 23:49:35 +01:00
f21da31561 code cleanup 2021-12-30 22:55:57 +01:00
2 changed files with 61 additions and 17 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,5 +1,6 @@
# TODO: Write documentation for `Kotatsu`
require "system/user"
require "option_parser"
require "http/server"
require "json"
@@ -8,12 +9,16 @@ 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")
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
json.field "port", 8080
json.field "title", "Kotatsu"
json.field "services" do
json.array do
@@ -34,12 +39,12 @@ module Kotatsu
return conf
end
def self.writeConfig(version, port, title, services)
#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 "port", port
json.field "title", title
json.field "services" do
json.array do
@@ -58,12 +63,11 @@ 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 +75,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
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
@@ -91,6 +95,23 @@ module Kotatsu
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"
@@ -116,7 +137,7 @@ module Kotatsu
parser.on "add", "Add a service" do
if File.exists?(CONFIG_FILE) == true
version, port, title, services = readConfig()
version, title, services = readConfig()
print "Name: "
s_name = gets.to_s
print "Link: "
@@ -133,7 +154,7 @@ module Kotatsu
end
services_new << s_service
writeConfig(version, port, title, services_new)
writeConfig(version, title, services_new)
exit
else
STDERR.puts "ERROR! Config file not found: #{CONFIG_FILE}"
@@ -143,7 +164,7 @@ module Kotatsu
parser.on "list", "Lists all services" do
if File.exists?(CONFIG_FILE) == true
_, _, _, services = readConfig()
_, _, services = readConfig()
i = 0
while services.size > i
puts "##{i}: #{services[i][0]} ; #{services[i][1]} ; #{services[i][2]}"
@@ -163,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