crystal-gauntlet/src/crystal-gauntlet.cr

136 lines
3.0 KiB
Crystal
Raw Normal View History

2022-12-30 17:04:27 +01:00
require "http/server"
require "http/server/handler"
2022-12-30 17:04:27 +01:00
require "uri"
require "sqlite3"
require "migrate"
require "dotenv"
2022-12-31 09:16:43 +01:00
require "toml"
2023-01-02 11:59:37 +01:00
require "colorize"
2022-12-30 17:04:27 +01:00
require "./enums"
2022-12-31 02:08:09 +01:00
require "./lib/hash"
require "./lib/format"
require "./lib/accounts"
require "./lib/gjp"
require "./lib/clean"
2022-12-31 14:25:43 +01:00
require "./lib/songs"
2022-12-30 17:04:27 +01:00
Dotenv.load
module CrystalGauntlet
VERSION = "0.1.0"
2022-12-31 09:16:43 +01:00
CONFIG = TOML.parse(File.read("./config.toml"))
2023-01-02 11:59:37 +01:00
LOG = ::Log.for("crystal-gauntlet")
2022-12-31 09:16:43 +01:00
def config_get(key : String)
this = CONFIG
key.split(".").each do |val|
next_val = this.as(Hash)[val]?
if next_val == nil
return nil
else
this = next_val
end
end
return this
end
2022-12-30 17:04:27 +01:00
DATABASE = DB.open(ENV["DATABASE_URL"])
@@endpoints = Hash(String, (String -> String)).new
def self.endpoints
@@endpoints
end
2023-01-02 11:59:37 +01:00
def severity_color(severity : Log::Severity) : Colorize::Object
case severity
when .trace?
Colorize.with.dark_gray
when .debug?
Colorize.with.dark_gray
when .info?
Colorize.with.cyan
when .notice?
Colorize.with.cyan
when .warn?
Colorize.with.yellow
when .error?
Colorize.with.red
when .fatal?
Colorize.with.light_red
else
Colorize.with.white
end
end
struct CrystalGauntletFormat < Log::StaticFormatter
def run
Colorize.with.light_gray.dim.surround(@io) do
timestamp
end
string " "
severity_color(@entry.severity).surround(@io) do
@entry.severity.label.rjust(@io, 6)
end
string ": "
Colorize.with.white.surround(@io) do
source
end
string " - "
message
end
end
class GDHandler
include HTTP::Handler
def call(context)
2022-12-30 17:04:27 +01:00
# expunge trailing slashes
path = context.request.path.chomp("/")
path = path.sub(config_get("general.append_path").as(String | Nil) || "", "")
body = context.request.body
if CrystalGauntlet.endpoints.has_key?(path) && body
func = CrystalGauntlet.endpoints[path]
value = func.call(body.gets_to_end)
2023-01-02 11:59:37 +01:00
LOG.debug { "-> " + value }
context.response.content_type = "text/plain"
context.response.print value
2022-12-30 17:04:27 +01:00
else
call_next(context)
2022-12-30 17:04:27 +01:00
end
end
end
def self.run()
server = HTTP::Server.new([
HTTP::ErrorHandler.new,
HTTP::LogHandler.new,
2023-01-02 10:37:06 +01:00
HTTP::StaticFileHandler.new("data/", fallthrough = true, directory_listing = false),
CrystalGauntlet::GDHandler.new
])
2022-12-30 17:04:27 +01:00
listen_on = URI.parse(ENV["LISTEN_ON"]? || "http://localhost:8080").normalize
case listen_on.scheme
when "http"
server.bind_tcp(listen_on.hostname.not_nil!, listen_on.port.not_nil!)
when "unix"
server.bind_unix(listen_on.to_s.sub("unix://",""))
end
2023-01-02 11:59:37 +01:00
Log.setup_from_env(backend: Log::IOBackend.new(formatter: CrystalGauntletFormat))
2023-01-01 06:42:34 +01:00
2023-01-02 11:59:37 +01:00
LOG.notice { "Listening on #{listen_on.to_s}" }
server.listen
2022-12-30 17:04:27 +01:00
end
end
require "./endpoints/**"
CrystalGauntlet.run()