diff options
Diffstat (limited to 'hybring.cr')
-rw-r--r-- | hybring.cr | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/hybring.cr b/hybring.cr index f4a2f07..13ba893 100644 --- a/hybring.cr +++ b/hybring.cr @@ -17,6 +17,7 @@ # along with hybring. If not, see <https://www.gnu.org/licenses/>. require "http/server" +require "ini" require "uri" require "./sqlite" @@ -31,8 +32,16 @@ def http_error(context, status, message = nil) context.response.respond_with_status status, message end -db = Database.new "hybring.db" -File.write "index.xhtml", page +if ARGV.empty? + puts "usage: #{PROGRAM_NAME} config.ini" + exit 1 +end +cfg = INI.parse File.read ARGV[0] +db = Database.new cfg["path"]["db"] +members = db.members +page = Page.new cfg["url"]["static"], cfg["url"]["api"] +File.write Path[cfg["path"]["static"]] / "index.xhtml", page.build members + server = HTTP::Server.new do |context| # Manually crafted request next http_error context, 405 if context.request.method != "POST" @@ -78,16 +87,23 @@ server = HTTP::Server.new do |context| next http_error context, 400, "Invalid Parameter" if invalid_param next http_error context, 400, "Missing Parameter" unless params.size == 3 + members.each do |nick, opennic, icann| + errors["nick"] = "Must be unique" if nick == params["nick"] + errors["opennic"] = "Must be unique" if opennic == params["opennic"] + errors["icann"] = "Must be unique" if icann == params["icann"] + end + if errors.empty? # TODO: write feed else context.response.status_code = 400 unless errors.empty? end context.response.content_type = "application/xhtml+xml" - context.response.print page errors, params + context.response.print page.build members, errors, params # TODO: schedule dynamic check end -address = server.bind_tcp 8080 +# TODO: support Unix socket +address = server.bind_tcp cfg["listen"]["port"].to_i puts "Listening on http://#{address}" server.listen |