Compare commits
4 Commits
4fc6ac892f
...
5934863fa6
| Author | SHA1 | Date | |
|---|---|---|---|
| 5934863fa6 | |||
| 1dc1b9c351 | |||
| 4559f2b67e | |||
| f21da31561 |
11
html/kotatsu.html
Normal file
11
html/kotatsu.html
Normal 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>
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
# TODO: Write documentation for `Kotatsu`
|
# TODO: Write documentation for `Kotatsu`
|
||||||
|
|
||||||
|
require "system/user"
|
||||||
require "option_parser"
|
require "option_parser"
|
||||||
require "http/server"
|
require "http/server"
|
||||||
require "json"
|
require "json"
|
||||||
@@ -8,12 +9,16 @@ module Kotatsu
|
|||||||
VERSION = "0.1.0"
|
VERSION = "0.1.0"
|
||||||
CONFIG_DIR = Path.new(Path.home, ".config", "kotatsu")
|
CONFIG_DIR = Path.new(Path.home, ".config", "kotatsu")
|
||||||
CONFIG_FILE = Path.new(CONFIG_DIR, "config.json")
|
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|
|
conf = JSON.build do |json|
|
||||||
json.object do
|
json.object do
|
||||||
json.field "config_version", 1
|
json.field "config_version", 1
|
||||||
json.field "port", 8080
|
|
||||||
json.field "title", "Kotatsu"
|
json.field "title", "Kotatsu"
|
||||||
json.field "services" do
|
json.field "services" do
|
||||||
json.array do
|
json.array do
|
||||||
@@ -34,12 +39,12 @@ module Kotatsu
|
|||||||
return conf
|
return conf
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.writeConfig(version, port, title, services)
|
#Writes the config file in JSON format
|
||||||
|
def self.writeConfig(version, title, services)
|
||||||
i = 0
|
i = 0
|
||||||
conf = JSON.build do |json|
|
conf = JSON.build do |json|
|
||||||
json.object do
|
json.object do
|
||||||
json.field "config_version", version
|
json.field "config_version", version
|
||||||
json.field "port", port
|
|
||||||
json.field "title", title
|
json.field "title", title
|
||||||
json.field "services" do
|
json.field "services" do
|
||||||
json.array do
|
json.array do
|
||||||
@@ -58,12 +63,11 @@ module Kotatsu
|
|||||||
File.write(CONFIG_FILE, conf)
|
File.write(CONFIG_FILE, conf)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#Reads the JSON config and returns it as various variables
|
||||||
def self.readConfig()
|
def self.readConfig()
|
||||||
conf = JSON.parse(File.read(CONFIG_FILE))
|
conf = JSON.parse(File.read(CONFIG_FILE))
|
||||||
version = conf["config_version"].as_i
|
version = conf["config_version"].as_i
|
||||||
port = conf["port"].as_i
|
|
||||||
title = conf["title"].as_s
|
title = conf["title"].as_s
|
||||||
|
|
||||||
t_services = conf["services"].as_a
|
t_services = conf["services"].as_a
|
||||||
services = [] of Array(String)
|
services = [] of Array(String)
|
||||||
i = 0
|
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]
|
services << [conf["services"][i][0].as_s, conf["services"][i][1].as_s, conf["services"][i][2].as_s]
|
||||||
i+=1
|
i+=1
|
||||||
end
|
end
|
||||||
|
return version, title, services
|
||||||
return version, port, title, services
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#Checks if config dir exists and then creates a config file
|
||||||
def self.makeConfig(file, dir)
|
def self.makeConfig(file, dir)
|
||||||
puts "Creating new config file in #{file}"
|
puts "Creating new config file in #{file}"
|
||||||
if Dir.exists?(dir) == false
|
if Dir.exists?(dir) == false
|
||||||
@@ -91,6 +95,23 @@ module Kotatsu
|
|||||||
end
|
end
|
||||||
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|
|
OptionParser.parse do |parser|
|
||||||
parser.banner = "Kotatsu - Easy to configure server start page"
|
parser.banner = "Kotatsu - Easy to configure server start page"
|
||||||
|
|
||||||
@@ -116,7 +137,7 @@ module Kotatsu
|
|||||||
|
|
||||||
parser.on "add", "Add a service" do
|
parser.on "add", "Add a service" do
|
||||||
if File.exists?(CONFIG_FILE) == true
|
if File.exists?(CONFIG_FILE) == true
|
||||||
version, port, title, services = readConfig()
|
version, title, services = readConfig()
|
||||||
print "Name: "
|
print "Name: "
|
||||||
s_name = gets.to_s
|
s_name = gets.to_s
|
||||||
print "Link: "
|
print "Link: "
|
||||||
@@ -133,7 +154,7 @@ module Kotatsu
|
|||||||
end
|
end
|
||||||
services_new << s_service
|
services_new << s_service
|
||||||
|
|
||||||
writeConfig(version, port, title, services_new)
|
writeConfig(version, title, services_new)
|
||||||
exit
|
exit
|
||||||
else
|
else
|
||||||
STDERR.puts "ERROR! Config file not found: #{CONFIG_FILE}"
|
STDERR.puts "ERROR! Config file not found: #{CONFIG_FILE}"
|
||||||
@@ -143,7 +164,7 @@ module Kotatsu
|
|||||||
|
|
||||||
parser.on "list", "Lists all services" do
|
parser.on "list", "Lists all services" do
|
||||||
if File.exists?(CONFIG_FILE) == true
|
if File.exists?(CONFIG_FILE) == true
|
||||||
_, _, _, services = readConfig()
|
_, _, services = readConfig()
|
||||||
i = 0
|
i = 0
|
||||||
while services.size > i
|
while services.size > i
|
||||||
puts "##{i}: #{services[i][0]} ; #{services[i][1]} ; #{services[i][2]}"
|
puts "##{i}: #{services[i][0]} ; #{services[i][1]} ; #{services[i][2]}"
|
||||||
@@ -163,18 +184,30 @@ module Kotatsu
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# -- MAIN --
|
||||||
|
|
||||||
|
#Checks if a config exists, if not runs makeConfig
|
||||||
if File.exists?(CONFIG_FILE) == false
|
if File.exists?(CONFIG_FILE) == false
|
||||||
puts "Could not find config file"
|
puts "Could not find config file"
|
||||||
makeConfig(CONFIG_FILE, CONFIG_DIR)
|
makeConfig(CONFIG_FILE, CONFIG_DIR)
|
||||||
end
|
end
|
||||||
|
|
||||||
webserv = HTTP::Server.new do |context|
|
#Reads config file
|
||||||
context.response.content_type = "text/plain"
|
c_version, c_title, c_services = readConfig()
|
||||||
context.response.print "Running kotatsu #{VERSION}"
|
|
||||||
|
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
|
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}"
|
puts "Started kotatsu webserver on #{webaddr}"
|
||||||
webserv.listen
|
webserv.listen
|
||||||
|
end
|
||||||
end
|
|
||||||
Reference in New Issue
Block a user