admin reachable on server

This commit is contained in:
Trey t
2025-11-27 23:47:29 -06:00
parent 469f21a833
commit e6f05b2368
3 changed files with 66 additions and 66 deletions

View File

@@ -2,9 +2,9 @@ package admin
import (
"net/http"
"net/http/httputil"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
@@ -252,75 +252,32 @@ func SetupRoutes(router *gin.Engine, db *gorm.DB, cfg *config.Config, deps *Depe
}
}
// Serve admin panel static files
setupStaticFiles(router)
// Proxy admin panel requests to Next.js server
setupAdminProxy(router)
}
// setupStaticFiles configures serving the admin panel static files
func setupStaticFiles(router *gin.Engine) {
// Determine the static files directory
// Check multiple possible locations
possiblePaths := []string{
"admin/out", // Development: from project root
"./admin/out", // Development: relative
"/app/admin/out", // Docker: absolute path
// setupAdminProxy configures reverse proxy to the Next.js admin panel
func setupAdminProxy(router *gin.Engine) {
// Get admin panel URL from env, default to localhost:3000
adminURL := os.Getenv("ADMIN_PANEL_URL")
if adminURL == "" {
adminURL = "http://127.0.0.1:3000"
}
var staticDir string
for _, path := range possiblePaths {
if _, err := os.Stat(path); err == nil {
staticDir = path
break
}
}
if staticDir == "" {
// Admin panel not built yet, skip static file serving
target, err := url.Parse(adminURL)
if err != nil {
return
}
// Serve static files at /admin/*
router.GET("/admin/*filepath", func(c *gin.Context) {
filePath := c.Param("filepath")
proxy := httputil.NewSingleHostReverseProxy(target)
// Clean the path
filePath = strings.TrimPrefix(filePath, "/")
if filePath == "" {
filePath = "index.html"
}
// Handle all /admin/* requests
router.Any("/admin/*filepath", func(c *gin.Context) {
proxy.ServeHTTP(c.Writer, c.Request)
})
fullPath := filepath.Join(staticDir, filePath)
// Check if file exists
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
// For SPA routing, serve index.html for non-existent paths
// But only if it's not a file request (no extension or .html)
ext := filepath.Ext(filePath)
if ext == "" || ext == ".html" {
// Try to find the specific page's index.html
pagePath := filepath.Join(staticDir, filePath, "index.html")
if _, err := os.Stat(pagePath); err == nil {
c.File(pagePath)
return
}
// Fall back to root index.html
c.File(filepath.Join(staticDir, "index.html"))
return
}
c.Status(http.StatusNotFound)
return
}
// If it's a directory, serve index.html
info, _ := os.Stat(fullPath)
if info.IsDir() {
indexPath := filepath.Join(fullPath, "index.html")
if _, err := os.Stat(indexPath); err == nil {
c.File(indexPath)
return
}
}
c.File(fullPath)
// Also handle /admin without trailing path
router.Any("/admin", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "/admin/")
})
}