changed http server to run on unix socket

This commit is contained in:
2021-12-31 00:21:25 +01:00
parent 1dc1b9c351
commit 5934863fa6

View File

@@ -1,5 +1,6 @@
# TODO: Write documentation for `Kotatsu`
require "system/user"
require "option_parser"
require "http/server"
require "json"
@@ -8,6 +9,7 @@ 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 --
@@ -195,12 +197,17 @@ module Kotatsu
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
#Run http server
webserv = HTTP::Server.new do |context|
context.response.content_type = "text/html"
context.response << webcontent
end
webaddr = webserv.bind_tcp 8080
webaddr = webserv.bind_unix(Socket::UNIXAddress.new(UNIX_SOCKET_PATH.to_s))
puts "Started kotatsu webserver on #{webaddr}"
webserv.listen
end
end