This commit is contained in:
Sergey Zhemoytel 2025-01-31 22:35:47 +03:00
parent b9d5f0b884
commit 140721e7fc
4 changed files with 40 additions and 5 deletions

View file

@ -1,2 +1,6 @@
# r11_urpm-repo # 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"

25
api.go
View file

@ -3,9 +3,32 @@ package main
import ( import (
"net/http" "net/http"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"strings"
"github.com/spf13/viper"
) )
func (r *Repository) UploadHandler(w http.ResponseWriter, req *http.Request) { 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") arch := chi.URLParam(req, "arch")
if arch == "" { if arch == "" {
http.Error(w, "Invalid architecture", http.StatusBadRequest) 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.WriteHeader(http.StatusOK)
w.Write([]byte("Package uploaded successfully")) w.Write([]byte("Package uploaded successfully"))
logger.Infof("Package %s uploaded successfully for architecture %s", handler.Filename, arch) logger.Infof("Package %s uploaded successfully for architecture %s", handler.Filename, arch)
} }

View file

@ -4,3 +4,6 @@ repository:
path: "/var/www/html/urpm-repo" path: "/var/www/html/urpm-repo"
log: log:
level: "info" level: "info"
auth:
token: "your_secret_token_here"

View file

@ -30,15 +30,21 @@ http {
listen 8080; listen 8080;
server_name localhost; server_name localhost;
root /var/www/html/urpm-repo;
location / { location / {
root /var/www/html/urpm-repo;
autoindex on; autoindex on;
autoindex_exact_size off; autoindex_exact_size off;
autoindex_localtime on; autoindex_localtime on;
} }
location ~ ^/(x86_64|i386|aarch64|riscv64|noarch|src)/ { 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 on;
autoindex_exact_size off; autoindex_exact_size off;
autoindex_localtime on; autoindex_localtime on;
@ -49,5 +55,4 @@ http {
root /usr/share/nginx/html; root /usr/share/nginx/html;
} }
} }
} }