From 140721e7fc350835cf1f5acc6c4e5e31a021542f Mon Sep 17 00:00:00 2001 From: Sergey Zhemoytel Date: Fri, 31 Jan 2025 22:35:47 +0300 Subject: [PATCH] update --- README.md | 4 ++++ api.go | 25 ++++++++++++++++++++++++- config.yaml | 3 +++ nginx.conf | 13 +++++++++---- 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index bd73b8c..3b46fdc 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,6 @@ # r11_urpm-repo +Проверим успешную загрузку пакета: + + +curl -X POST "http://localhost:8888/x86_64" -H "Authorization: Bearer your_secret_token_here" -F "package=@/path/to/your/package.rpm" diff --git a/api.go b/api.go index b4d280d..095a41b 100644 --- a/api.go +++ b/api.go @@ -3,9 +3,32 @@ package main import ( "net/http" "github.com/go-chi/chi/v5" + "strings" + "github.com/spf13/viper" ) func (r *Repository) UploadHandler(w http.ResponseWriter, req *http.Request) { + authHeader := req.Header.Get("Authorization") + if authHeader == "" { + http.Error(w, "Unauthorized", http.StatusUnauthorized) + logger.Error("Authorization header missing") + return + } + + parts := strings.Split(authHeader, " ") + if len(parts) != 2 || parts[0] != "Bearer" { + http.Error(w, "Unauthorized", http.StatusUnauthorized) + logger.Error("Invalid Authorization header format") + return + } + + token := parts[1] + if token != viper.GetString("auth.token") { + http.Error(w, "Unauthorized", http.StatusUnauthorized) + logger.Error("Invalid token") + return + } + arch := chi.URLParam(req, "arch") if arch == "" { http.Error(w, "Invalid architecture", http.StatusBadRequest) @@ -32,4 +55,4 @@ func (r *Repository) UploadHandler(w http.ResponseWriter, req *http.Request) { w.WriteHeader(http.StatusOK) w.Write([]byte("Package uploaded successfully")) logger.Infof("Package %s uploaded successfully for architecture %s", handler.Filename, arch) -} \ No newline at end of file +} diff --git a/config.yaml b/config.yaml index 489cd74..c5a9daa 100644 --- a/config.yaml +++ b/config.yaml @@ -4,3 +4,6 @@ repository: path: "/var/www/html/urpm-repo" log: level: "info" +auth: + token: "your_secret_token_here" + diff --git a/nginx.conf b/nginx.conf index 01830f0..1e7c3a2 100644 --- a/nginx.conf +++ b/nginx.conf @@ -30,15 +30,21 @@ http { listen 8080; server_name localhost; + root /var/www/html/urpm-repo; + location / { - root /var/www/html/urpm-repo; autoindex on; autoindex_exact_size off; autoindex_localtime on; } location ~ ^/(x86_64|i386|aarch64|riscv64|noarch|src)/ { - alias /var/www/html/urpm-repo/$1/; + autoindex on; + autoindex_exact_size off; + autoindex_localtime on; + } + + location ~ ^/(x86_64|i386|aarch64|riscv64|noarch|src)/media_info/ { autoindex on; autoindex_exact_size off; autoindex_localtime on; @@ -49,5 +55,4 @@ http { root /usr/share/nginx/html; } } -} - +} \ No newline at end of file