From 2ac4ee4ea14863461b97e7a8cf61f838391947bc Mon Sep 17 00:00:00 2001
From: Ophestra Umiker <cat@ophivana.moe>
Date: Thu, 17 Oct 2024 01:51:27 +0900
Subject: [PATCH] feat(serve): listen on unix socket

Support listening on unix socket. Path is set by the LISTEN environment variable.

Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
---
 app.go  | 11 ++++++++++-
 conf.go |  2 ++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/app.go b/app.go
index 2b73e58..83af946 100644
--- a/app.go
+++ b/app.go
@@ -3,6 +3,7 @@ package main
 import (
 	"fmt"
 	"log"
+	"net"
 	"os"
 	"sync/atomic"
 	"time"
@@ -73,5 +74,13 @@ func serve(sig chan os.Signal, db *leveldb.DB) error {
 		}
 	}()
 
-	return app.Listen(conf[listenAddr])
+	if conf[listen] == "unset" {
+		return app.Listen(conf[listenAddr])
+	} else {
+		if l, err := net.Listen("unix", conf[listen]); err != nil {
+			return err
+		} else {
+			return app.Listener(l)
+		}
+	}
 }
diff --git a/conf.go b/conf.go
index a95b71f..c31d17e 100644
--- a/conf.go
+++ b/conf.go
@@ -7,6 +7,7 @@ import (
 
 const (
 	dbPath uint8 = iota
+	listen
 	listenAddr
 	allowedURL
 	hCaptchaSiteKey
@@ -17,6 +18,7 @@ const (
 // env variable, default pairing
 var confEnv = [...][2]string{
 	dbPath:            {"DB", "db"},
+	listen:            {"LISTEN", "unset"},
 	listenAddr:        {"LISTEN_ADDR", "127.0.0.1:3000"},
 	allowedURL:        {"ALLOWED_URL", "https://hizla.io"},
 	hCaptchaSiteKey:   {"HCAPTCHA_SITE_KEY", "unset"},
-- 
GitLab