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.
This commit is contained in:
f
2023-01-21 17:36:47 -03:00
parent 17ce9a4887
commit fac61d7113

View File

@ -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 || "{}")