Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
2025-09-14 20:03:47 +01:00
parent 43b6159feb
commit 8f2cea6abf
6 changed files with 80 additions and 13 deletions

View File

@@ -17,6 +17,7 @@ pkgs.dockerTools.buildImage {
name = "root";
paths = with pkgs; [
git
git-lfs
curl
jq
nix

View File

@@ -27,11 +27,14 @@ var (
maxUploadSize int64 = 1 << 30 // 1GB
deployLock sync.Mutex
infoLog = log.New(os.Stdout, "", log.LstdFlags)
errorLog = log.New(os.Stderr, "", log.LstdFlags)
)
func main() {
if authenticationKey == "" || targetDirectory == "" {
log.Fatal("AUTH_KEY and TARGET_DIR must be set")
errorLog.Fatal("AUTH_KEY and TARGET_DIR must be set")
}
if port == "" {
@@ -43,15 +46,15 @@ func main() {
basePath = "/" + subPath
}
log.Printf("starting server on :%s, endpoint %q, target directory %q", port, basePath, targetDirectory)
infoLog.Printf("starting server on :%s, endpoint %q, target directory %q", port, basePath, targetDirectory)
http.HandleFunc(basePath, withRecovery(handle))
log.Fatal(http.ListenAndServe(":"+port, nil))
errorLog.Fatal(http.ListenAndServe(":"+port, nil))
}
func handle(w http.ResponseWriter, r *http.Request) {
remoteIP := realIP(r)
log.Printf("incoming %q request on %q from %s", r.Method, r.URL.Path, remoteIP)
infoLog.Printf("incoming %q request on %q from %s", r.Method, r.URL.Path, remoteIP)
if r.Method != http.MethodPost {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
@@ -60,7 +63,7 @@ func handle(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
if subtle.ConstantTimeCompare([]byte(auth), []byte(authenticationKey)) != 1 {
log.Printf("unauthorized request from %s", remoteIP)
errorLog.Printf("unauthorized request from %s", remoteIP)
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
@@ -112,7 +115,7 @@ func handle(w http.ResponseWriter, r *http.Request) {
defer os.RemoveAll(extractDir)
if err := extractor.Extract(ctx, archiveStream, extract(extractDir)); err != nil {
log.Printf("failed to extract archive: %v", err)
errorLog.Printf("failed to extract archive: %v", err)
http.Error(w, "bad archive", http.StatusBadRequest)
return
}
@@ -131,7 +134,7 @@ func handle(w http.ResponseWriter, r *http.Request) {
}
w.WriteHeader(http.StatusOK)
log.Printf("upload successful from %s", remoteIP)
infoLog.Printf("upload successful from %s", remoteIP)
}
func realIP(r *http.Request) string {
@@ -235,7 +238,7 @@ func withRecovery(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer func() {
if v := recover(); v != nil {
log.Printf("panic: %v", v)
errorLog.Printf("panic: %v", v)
http.Error(w, "internal error", http.StatusInternalServerError)
}
}()