From fac61d71130d7dbd77e910df14bec5031990e869 Mon Sep 17 00:00:00 2001 From: f Date: Sat, 21 Jan 2023 17:36:47 -0300 Subject: [PATCH] feat: support nginx unix sockets #2 this adventure started two years ago when we hit a roadblock because crystal didn't support entirely and was about to drop support for dgram sockets, which is the only unix socket nginx supports. --- src/access_log.cr | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/access_log.cr b/src/access_log.cr index 44d7c18..2492456 100644 --- a/src/access_log.cr +++ b/src/access_log.cr @@ -1,4 +1,5 @@ require "json" +require "socket" require "sqlite3" require "pg" require "option_parser" @@ -8,6 +9,8 @@ require "./models/crawler" VERSION = "0.3.0" +# Default socket location +socket = "/tmp/access_log.socket" # The default database URI database = "sqlite3://./development.sqlite3" # Detect web crawlers @@ -65,6 +68,9 @@ if crawler crawler_re = Regex.union(crawlers.map { |c| c.pattern }) end +server = Socket.unix(Socket::Type::DGRAM) +server.bind Socket::UNIXAddress.new(socket) + # Just exit Signal::INT.trap do exit @@ -77,8 +83,11 @@ end # Open the database and wait for JSONL input. db = DB.open database do |db| - while (json = gets) + while true begin + msg, _ = server.receive(1024 * 64) # 64K + _, _, _, _, json = msg.split(" ", 5) + # Parse input access_log = AccessLog.from_json(json || "{}")