Serve public MinIO files at /file/:file_name and add a native Ubuntu MinIO install.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -54,6 +54,9 @@ Desktop.ini
|
||||
docker-compose.override.yaml
|
||||
docker-compose.override.yml
|
||||
|
||||
# Local MinIO Ubuntu package (do not commit)
|
||||
deployment/minio.deb
|
||||
|
||||
# Temporary files
|
||||
tmp/
|
||||
temp/
|
||||
|
||||
@@ -12,9 +12,9 @@ storage:
|
||||
password: <your-db-password>
|
||||
pg_pool_max_conn: 10
|
||||
minio:
|
||||
endpoint: example-minio:9000 # use localhost:9003 when running outside Docker
|
||||
access_key: <your-minio-access-key>
|
||||
secret_key: <your-minio-secret-key>
|
||||
endpoint: 127.0.0.1:9000
|
||||
access_key: TX3H1H6XTS288QW07S8Z
|
||||
secret_key: FWWspiI4n4LMknnxYCmzFTCY1ObkGiuaU9SNwOCU
|
||||
bucket: dev-minio
|
||||
use_ssl: false
|
||||
app_version: 0.0.1
|
||||
|
||||
74
deployment/install-minio-ubuntu.sh
Executable file
74
deployment/install-minio-ubuntu.sh
Executable file
@@ -0,0 +1,74 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
# Native MinIO for Ubuntu (no Docker). Binds only to 127.0.0.1.
|
||||
#
|
||||
# Fresh install (keeps existing data if present):
|
||||
# sudo bash install-minio-ubuntu.sh /path/to/minio.deb
|
||||
#
|
||||
# Wipe data + config, then install:
|
||||
# sudo bash install-minio-ubuntu.sh --wipe /path/to/minio.deb
|
||||
|
||||
WIPE=0
|
||||
DEB_PATH=""
|
||||
for arg in "$@"; do
|
||||
case "$arg" in
|
||||
--wipe) WIPE=1 ;;
|
||||
*) DEB_PATH="$arg" ;;
|
||||
esac
|
||||
done
|
||||
DEB_PATH="${DEB_PATH:-./minio.deb}"
|
||||
ENV_SRC="$(cd "$(dirname "$0")" && pwd)/minio.env"
|
||||
|
||||
if [[ "$(id -u)" -ne 0 ]]; then
|
||||
echo "run as root: sudo bash $0 [--wipe] $DEB_PATH" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ ! -f "$DEB_PATH" ]]; then
|
||||
echo "deb not found: $DEB_PATH" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ ! -f "$ENV_SRC" ]]; then
|
||||
echo "env file not found: $ENV_SRC" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
systemctl stop minio 2>/dev/null || true
|
||||
systemctl reset-failed minio 2>/dev/null || true
|
||||
pkill -x minio 2>/dev/null || true
|
||||
sleep 1
|
||||
|
||||
if [[ "$WIPE" -eq 1 ]]; then
|
||||
echo "wiping /data/minio and /etc/default/minio"
|
||||
rm -rf /data/minio
|
||||
rm -f /etc/default/minio
|
||||
fi
|
||||
|
||||
port_busy() {
|
||||
local port="$1"
|
||||
ss -H -tln | awk '{print $4}' | grep -E ":${port}$" >/dev/null
|
||||
}
|
||||
|
||||
for port in 9000 9001; do
|
||||
if port_busy "$port"; then
|
||||
echo "port $port is already in use:" >&2
|
||||
ss -tlnp | grep -E ":${port}\\b" >&2 || true
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
id minio-user >/dev/null 2>&1 || useradd --system --home-dir /data/minio --shell /sbin/nologin minio-user
|
||||
mkdir -p /data/minio
|
||||
chown -R minio-user:minio-user /data/minio
|
||||
|
||||
cp "$ENV_SRC" /etc/default/minio
|
||||
chmod 600 /etc/default/minio
|
||||
|
||||
dpkg -i "$DEB_PATH"
|
||||
|
||||
systemctl daemon-reload
|
||||
systemctl enable minio
|
||||
systemctl start minio
|
||||
systemctl --no-pager --full status minio
|
||||
6
deployment/minio.env
Normal file
6
deployment/minio.env
Normal file
@@ -0,0 +1,6 @@
|
||||
MINIO_ROOT_USER=TX3H1H6XTS288QW07S8Z
|
||||
MINIO_ROOT_PASSWORD=FWWspiI4n4LMknnxYCmzFTCY1ObkGiuaU9SNwOCU
|
||||
MINIO_VOLUMES=/data/minio
|
||||
MINIO_ADDRESS=127.0.0.1:9000
|
||||
MINIO_CONSOLE_ADDRESS=127.0.0.1:9001
|
||||
MINIO_OPTS=
|
||||
@@ -41,8 +41,8 @@ services:
|
||||
- MINIO_ROOT_USER=TX3H1H6XTS288QW07S8Z
|
||||
- MINIO_ROOT_PASSWORD=FWWspiI4n4LMknnxYCmzFTCY1ObkGiuaU9SNwOCU
|
||||
ports:
|
||||
- '9003:9000'
|
||||
- '9093:9093'
|
||||
- '127.0.0.1:9003:9000'
|
||||
- '127.0.0.1:9093:9093'
|
||||
volumes:
|
||||
- main-minio-storage:/storage
|
||||
networks:
|
||||
|
||||
@@ -55,7 +55,7 @@ func New(cfg *config.Config, client *pgxpool.Pool, logger *logging.Logger) *gin.
|
||||
|
||||
// Each bounded context registers its own modules' handlers.
|
||||
// Add new modules inside the relevant context's router.go rather than here.
|
||||
identity.Register(adminGroup, client, logger)
|
||||
identity.Register(adminGroup, router, client, logger)
|
||||
org.Register(adminGroup, client, logger)
|
||||
lending.Register(adminGroup, client, logger)
|
||||
cards.Register(adminGroup, client, logger)
|
||||
|
||||
@@ -29,7 +29,12 @@ import (
|
||||
)
|
||||
|
||||
// Register wires every identity module's handler onto router.
|
||||
func Register(router *gin.RouterGroup, client postgresql.Client, logger *logging.Logger) {
|
||||
// engine is the root gin engine so public file GET can live at /file/:file_name.
|
||||
func Register(router *gin.RouterGroup, engine *gin.Engine, client postgresql.Client, logger *logging.Logger) {
|
||||
userH := user.NewHandler(logger, userdb.NewRepository(client, logger))
|
||||
userH.Register(router)
|
||||
userH.RegisterPublic(engine)
|
||||
|
||||
for _, h := range Handlers(client, logger) {
|
||||
h.Register(router)
|
||||
}
|
||||
@@ -41,7 +46,6 @@ func Handlers(client postgresql.Client, logger *logging.Logger) []handlers.Handl
|
||||
// Swap sms.NewLogSender for your SMS provider: it only needs to
|
||||
// implement sms.Sender (Send(ctx, phone, message)).
|
||||
auth.NewHandler(logger, authdb.NewRepository(client, logger), sms.NewLogSender(logger)),
|
||||
user.NewHandler(logger, userdb.NewRepository(client, logger)),
|
||||
role.NewHandler(logger, roledb.NewRepository(client, logger)),
|
||||
permission.NewHandler(logger, permissiondb.NewRepository(client, logger)),
|
||||
roleusers.NewHandler(logger, roleusersdb.NewRepository(client, logger)),
|
||||
|
||||
@@ -13,7 +13,6 @@ import (
|
||||
|
||||
"online-back/internal/appresult"
|
||||
"online-back/internal/config"
|
||||
"online-back/internal/handlers"
|
||||
"online-back/pkg/logging"
|
||||
minioUpload "online-back/pkg/minio"
|
||||
|
||||
@@ -23,9 +22,10 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
baseURL = "/users"
|
||||
fileUploadURL = "/file"
|
||||
fileURL = "/file/:id"
|
||||
baseURL = "/users"
|
||||
fileUploadURL = "/file"
|
||||
fileURL = "/file/:id"
|
||||
publicFileURL = "/file/:file_name"
|
||||
)
|
||||
|
||||
type handler struct {
|
||||
@@ -33,7 +33,7 @@ type handler struct {
|
||||
repository Repository
|
||||
}
|
||||
|
||||
func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler {
|
||||
func NewHandler(logger *logging.Logger, repository Repository) *handler {
|
||||
return &handler{logger: logger, repository: repository}
|
||||
}
|
||||
|
||||
@@ -47,15 +47,17 @@ func (h *handler) Register(router *gin.RouterGroup) {
|
||||
group.PUT("/:id", h.update)
|
||||
group.DELETE("/:id", h.delete)
|
||||
|
||||
// Avatar/profile file storage — kept at the top-level /file path (not
|
||||
// nested under /users) to preserve the existing API contract; this used
|
||||
// to live in the auth handler purely incidentally.
|
||||
// Upload/delete stay on the admin API (JWT). GET is public at /file/:file_name
|
||||
// so nginx can serve avatars as https://host/file/<file_name>.
|
||||
files := router.Group("", appresult.JwtTokenCheck())
|
||||
files.POST(fileUploadURL, h.fileUpload)
|
||||
files.GET(fileURL, h.getFile)
|
||||
files.DELETE(fileURL, h.deleteFile)
|
||||
}
|
||||
|
||||
func (h *handler) RegisterPublic(engine *gin.Engine) {
|
||||
engine.GET(publicFileURL, h.getFile)
|
||||
}
|
||||
|
||||
// list godoc
|
||||
// @Summary List users
|
||||
// @Description Returns paginated users with role user, with optional filters
|
||||
@@ -375,16 +377,21 @@ func (h *handler) fileUpload(c *gin.Context) {
|
||||
}
|
||||
|
||||
// getFile godoc
|
||||
// @Description download file
|
||||
// @Description download file by object name (public; not under /api/admin)
|
||||
// @Tags file-upload
|
||||
// @Produce octet-stream
|
||||
// @Param id path string true "object name"
|
||||
// @Security BearerAuth
|
||||
// @Param file_name path string true "object name"
|
||||
// @Success 200 {file} binary
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 404 {object} appresult.AppError
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /file/{id} [get]
|
||||
// @Router /file/{file_name} [get]
|
||||
func (h *handler) getFile(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
fileName := c.Param("file_name")
|
||||
if fileName == "" || strings.Contains(fileName, "..") || strings.ContainsAny(fileName, "/\\") {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
minioCfg := config.GetConfig().Minio
|
||||
minioClient, ok := h.connectMinio(c, minioCfg)
|
||||
@@ -392,7 +399,7 @@ func (h *handler) getFile(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
object, err := minioClient.GetObject(c.Request.Context(), minioCfg.Bucket, id, minio.GetObjectOptions{})
|
||||
object, err := minioClient.GetObject(c.Request.Context(), minioCfg.Bucket, fileName, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
h.logger.Errorf("get object from minio failed: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrNotAcceptable)
|
||||
|
||||
19
nginx/file.conf
Normal file
19
nginx/file.conf
Normal file
@@ -0,0 +1,19 @@
|
||||
# Public file download (MinIO objects).
|
||||
# Copy into the HTTPS server block, or:
|
||||
# sudo cp nginx/file.conf /etc/nginx/snippets/online-back-file.conf
|
||||
# include /etc/nginx/snippets/online-back-file.conf;
|
||||
#
|
||||
# URL: https://<host>/file/<file_name>
|
||||
# App: GET http://127.0.0.1:5005/file/<file_name>
|
||||
|
||||
location /file/ {
|
||||
proxy_pass http://127.0.0.1:5005/file/;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
|
||||
expires 7d;
|
||||
add_header Cache-Control "public, max-age=604800";
|
||||
}
|
||||
@@ -30,6 +30,19 @@ server {
|
||||
include /etc/letsencrypt/options-ssl-nginx.conf;
|
||||
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
|
||||
|
||||
# Public MinIO files: https://test.webulgam.com/file/<file_name>
|
||||
# Snippet lives at nginx/file.conf — copy to snippets if you prefer include.
|
||||
location /file/ {
|
||||
proxy_pass http://127.0.0.1:5005/file/;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
expires 7d;
|
||||
add_header Cache-Control "public, max-age=604800";
|
||||
}
|
||||
|
||||
# --- existing project → :5050 ---
|
||||
location /admin {
|
||||
proxy_pass http://127.0.0.1:5050;
|
||||
|
||||
Reference in New Issue
Block a user