Added storage endpoint and show on indes
This commit is contained in:
5
.env
5
.env
@@ -10,4 +10,7 @@ UPLOAD_DIR=./uploads
|
||||
BASE_URL=http://localhost:8080
|
||||
|
||||
# How long to keep uploaded files before deletion (examples: 1d, 12h, 30m)
|
||||
DELETE_FILES_AFTER=1d
|
||||
DELETE_FILES_AFTER=1d
|
||||
|
||||
# Maximum storage capacity in GB
|
||||
MAX_STORAGE_GB=500
|
||||
@@ -10,4 +10,7 @@ UPLOAD_DIR=./uploads
|
||||
BASE_URL=http://localhost:8080
|
||||
|
||||
# How long to keep uploaded files before deletion (examples: 1d, 12h, 30m)
|
||||
DELETE_FILES_AFTER=1d
|
||||
DELETE_FILES_AFTER=1d
|
||||
|
||||
# Maximum storage capacity in GB
|
||||
MAX_STORAGE_GB=10
|
||||
71
controllers/storage.go
Normal file
71
controllers/storage.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func getDirectorySize(path string) (int64, error) {
|
||||
var size int64
|
||||
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !info.IsDir() {
|
||||
size += info.Size()
|
||||
}
|
||||
return err
|
||||
})
|
||||
return size, err
|
||||
}
|
||||
|
||||
func HandleStorageInfo(c *gin.Context) {
|
||||
uploadDir := os.Getenv("UPLOAD_DIR")
|
||||
if uploadDir == "" {
|
||||
uploadDir = "./uploads"
|
||||
}
|
||||
|
||||
maxStorageEnv := os.Getenv("MAX_STORAGE_GB")
|
||||
if maxStorageEnv == "" {
|
||||
maxStorageEnv = "10"
|
||||
}
|
||||
|
||||
maxStorageGB, err := strconv.ParseFloat(maxStorageEnv, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Invalid MAX_STORAGE_GB configuration"})
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := os.Stat(uploadDir); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(uploadDir, 0755); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create upload directory"})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
usedBytes, err := getDirectorySize(uploadDir)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to calculate storage usage"})
|
||||
return
|
||||
}
|
||||
|
||||
maxBytes := int64(maxStorageGB * 1024 * 1024 * 1024)
|
||||
usedGB := float64(usedBytes) / (1024 * 1024 * 1024)
|
||||
usagePercent := (float64(usedBytes) / float64(maxBytes)) * 100
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"storage": gin.H{
|
||||
"used_bytes": usedBytes,
|
||||
"used_gb": usedGB,
|
||||
"max_gb": maxStorageGB,
|
||||
"max_bytes": maxBytes,
|
||||
"usage_percent": usagePercent,
|
||||
"available_gb": maxStorageGB - usedGB,
|
||||
"available_bytes": maxBytes - usedBytes,
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -59,6 +59,6 @@ func HandleFileUpload(c *gin.Context) {
|
||||
"id": fileID,
|
||||
"filename": header.Filename,
|
||||
"size": header.Size,
|
||||
"url": baseURL + "/files/" + fileID + "/" + header.Filename,
|
||||
"url": baseURL + "/uploads/" + fileID + "/" + header.Filename,
|
||||
})
|
||||
}
|
||||
|
||||
1
main.go
1
main.go
@@ -18,6 +18,7 @@ func main() {
|
||||
r.GET("/", controllers.HandleIndex)
|
||||
r.POST("/upload", controllers.HandleFileUpload)
|
||||
r.GET("/uploads/:fileID/:filename", controllers.HandleFileDownload)
|
||||
r.GET("/storage", controllers.HandleStorageInfo)
|
||||
r.GET("/health", func(c *gin.Context) { c.JSON(200, gin.H{"status": "ok"}) })
|
||||
|
||||
if port := os.Getenv("PORT"); port != "" {
|
||||
|
||||
@@ -14,6 +14,10 @@
|
||||
.get { background: #61affe; }
|
||||
.post { background: #49cc90; }
|
||||
.step { margin: 10px 0; padding: 10px; background: #fff; border: 1px solid #ddd; border-radius: 3px; }
|
||||
.storage-info { margin: 20px 0; padding: 15px; background: #f9f9f9; border-radius: 5px; border-left: 4px solid #28a745; }
|
||||
.progress-bar { width: 100%; height: 20px; background: #e9ecef; border-radius: 10px; overflow: hidden; margin: 10px 0; }
|
||||
.progress-fill { height: 100%; background: linear-gradient(90deg, #28a745 0%, #ffc107 70%, #dc3545 90%); transition: width 0.3s ease; border-radius: 10px; }
|
||||
.storage-stats { display: flex; justify-content: space-between; align-items: center; margin-top: 10px; font-size: 14px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
@@ -21,6 +25,21 @@
|
||||
<p><strong>Version:</strong> 1.0.0</p>
|
||||
<p>Simple file upload and download service for developers using HTTP requests in CLI.</p>
|
||||
|
||||
<div class="storage-info">
|
||||
<h3>💾 Todays storage usage</h3>
|
||||
<p>
|
||||
<small>This resets after 24h</small>
|
||||
</p>
|
||||
<div class="progress-bar">
|
||||
<div class="progress-fill" id="progress-fill" style="width: 0%"></div>
|
||||
</div>
|
||||
<div class="storage-stats">
|
||||
<span id="storage-used">Loading...</span>
|
||||
<span id="storage-percent">Loading...</span>
|
||||
<span id="storage-total">Loading...</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h2>📋 API endpoints</h2>
|
||||
|
||||
<div class="endpoint">
|
||||
@@ -37,6 +56,13 @@
|
||||
<pre><code>curl -O {{.BaseURL}}/uploads/{fileID}/{filename}</code></pre>
|
||||
</div>
|
||||
|
||||
<div class="endpoint">
|
||||
<h3><span class="method get">GET</span>/storage</h3>
|
||||
<p><strong>Description:</strong> Check storage usage and capacity</p>
|
||||
<p><strong>cURL example:</strong></p>
|
||||
<pre><code>curl {{.BaseURL}}/storage</code></pre>
|
||||
</div>
|
||||
|
||||
<div class="endpoint">
|
||||
<h3><span class="method get">GET</span>/health</h3>
|
||||
<p><strong>Description:</strong> Check service health</p>
|
||||
@@ -68,5 +94,26 @@ curl -X POST -F "file=@myfile.txt" {{.BaseURL}}/upload
|
||||
|
||||
# 2. Download the file
|
||||
curl -O {{.BaseURL}}/uploads/abc123.../myfile.txt</code></pre>
|
||||
|
||||
<script>
|
||||
async function loadStorageInfo() {
|
||||
try {
|
||||
const response = await fetch('{{.BaseURL}}/storage');
|
||||
const data = await response.json();
|
||||
const storage = data.storage;
|
||||
|
||||
document.getElementById('storage-used').textContent = `${storage.used_gb.toFixed(2)} GB used`;
|
||||
document.getElementById('storage-percent').textContent = `${storage.usage_percent.toFixed(1)}% full`;
|
||||
document.getElementById('storage-total').textContent = `${storage.max_gb} GB total`;
|
||||
document.getElementById('progress-fill').style.width = `${storage.usage_percent}%`;
|
||||
} catch (error) {
|
||||
document.getElementById('storage-used').textContent = 'Error loading storage info';
|
||||
document.getElementById('storage-percent').textContent = '';
|
||||
document.getElementById('storage-total').textContent = '';
|
||||
}
|
||||
}
|
||||
|
||||
loadStorageInfo();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user