mirror of
https://github.com/Direct-Dev-Ru/go-lcg.git
synced 2025-11-16 01:29:55 +00:00
Исправления в ветке auth-feature
This commit is contained in:
152
serve/serve.go
152
serve/serve.go
@@ -5,13 +5,49 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/direct-dev-ru/linux-command-gpt/config"
|
||||
"github.com/direct-dev-ru/linux-command-gpt/ssl"
|
||||
)
|
||||
|
||||
// makePath создает путь с учетом BasePath
|
||||
func makePath(path string) string {
|
||||
basePath := config.AppConfig.Server.BasePath
|
||||
if basePath == "" || basePath == "/" {
|
||||
return path
|
||||
}
|
||||
|
||||
// Убираем слэш в конце basePath если есть
|
||||
basePath = strings.TrimSuffix(basePath, "/")
|
||||
|
||||
// Убираем слэш в начале path если есть
|
||||
path = strings.TrimPrefix(path, "/")
|
||||
|
||||
// Если path пустой, возвращаем basePath с слэшем в конце
|
||||
if path == "" {
|
||||
return basePath + "/"
|
||||
}
|
||||
|
||||
return basePath + "/" + path
|
||||
}
|
||||
|
||||
// getBasePath возвращает BasePath для использования в шаблонах
|
||||
func getBasePath() string {
|
||||
basePath := config.AppConfig.Server.BasePath
|
||||
if basePath == "" || basePath == "/" {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSuffix(basePath, "/")
|
||||
}
|
||||
|
||||
// StartResultServer запускает HTTP/HTTPS сервер для просмотра сохраненных результатов
|
||||
func StartResultServer(host, port string) error {
|
||||
// Инициализируем CSRF менеджер
|
||||
if err := InitCSRFManager(); err != nil {
|
||||
return fmt.Errorf("failed to initialize CSRF manager: %v", err)
|
||||
}
|
||||
|
||||
addr := fmt.Sprintf("%s:%s", host, port)
|
||||
|
||||
// Проверяем, нужно ли использовать HTTPS
|
||||
@@ -103,78 +139,116 @@ func registerHTTPSRoutes() {
|
||||
registerRoutesExceptHome()
|
||||
|
||||
// Регистрируем главную страницу с проверкой HTTPS
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.HandleFunc(makePath("/"), func(w http.ResponseWriter, r *http.Request) {
|
||||
// Проверяем, пришел ли запрос по HTTP (не HTTPS)
|
||||
if r.TLS == nil {
|
||||
handleHTTPSRedirect(w, r)
|
||||
return
|
||||
}
|
||||
// Если уже HTTPS, обрабатываем как обычно
|
||||
handleResultsPage(w, r)
|
||||
AuthMiddleware(handleResultsPage)(w, r)
|
||||
})
|
||||
|
||||
// Регистрируем главную страницу без слэша в конце для BasePath
|
||||
basePath := config.AppConfig.Server.BasePath
|
||||
if basePath != "" && basePath != "/" {
|
||||
basePath = strings.TrimSuffix(basePath, "/")
|
||||
http.HandleFunc(basePath, func(w http.ResponseWriter, r *http.Request) {
|
||||
// Проверяем, пришел ли запрос по HTTP (не HTTPS)
|
||||
if r.TLS == nil {
|
||||
handleHTTPSRedirect(w, r)
|
||||
return
|
||||
}
|
||||
// Если уже HTTPS, обрабатываем как обычно
|
||||
AuthMiddleware(handleResultsPage)(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// registerRoutesExceptHome регистрирует все маршруты кроме главной страницы
|
||||
func registerRoutesExceptHome() {
|
||||
// Страница входа (без аутентификации)
|
||||
http.HandleFunc(makePath("/login"), handleLoginPage)
|
||||
|
||||
// API для аутентификации (без аутентификации)
|
||||
http.HandleFunc(makePath("/api/login"), handleLogin)
|
||||
http.HandleFunc(makePath("/api/logout"), handleLogout)
|
||||
http.HandleFunc(makePath("/api/validate-token"), handleValidateToken)
|
||||
|
||||
// Файлы
|
||||
http.HandleFunc("/file/", handleFileView)
|
||||
http.HandleFunc("/delete/", handleDeleteFile)
|
||||
http.HandleFunc(makePath("/file/"), AuthMiddleware(handleFileView))
|
||||
http.HandleFunc(makePath("/delete/"), AuthMiddleware(handleDeleteFile))
|
||||
|
||||
// История запросов
|
||||
http.HandleFunc("/history", handleHistoryPage)
|
||||
http.HandleFunc("/history/view/", handleHistoryView)
|
||||
http.HandleFunc("/history/delete/", handleDeleteHistoryEntry)
|
||||
http.HandleFunc("/history/clear", handleClearHistory)
|
||||
http.HandleFunc(makePath("/history"), AuthMiddleware(handleHistoryPage))
|
||||
http.HandleFunc(makePath("/history/view/"), AuthMiddleware(handleHistoryView))
|
||||
http.HandleFunc(makePath("/history/delete/"), AuthMiddleware(handleDeleteHistoryEntry))
|
||||
http.HandleFunc(makePath("/history/clear"), AuthMiddleware(handleClearHistory))
|
||||
|
||||
// Управление промптами
|
||||
http.HandleFunc("/prompts", handlePromptsPage)
|
||||
http.HandleFunc("/prompts/add", handleAddPrompt)
|
||||
http.HandleFunc("/prompts/edit/", handleEditPrompt)
|
||||
http.HandleFunc("/prompts/edit-verbose/", handleEditVerbosePrompt)
|
||||
http.HandleFunc("/prompts/delete/", handleDeletePrompt)
|
||||
http.HandleFunc("/prompts/restore/", handleRestorePrompt)
|
||||
http.HandleFunc("/prompts/restore-verbose/", handleRestoreVerbosePrompt)
|
||||
http.HandleFunc("/prompts/save-lang", handleSaveLang)
|
||||
http.HandleFunc(makePath("/prompts"), AuthMiddleware(handlePromptsPage))
|
||||
http.HandleFunc(makePath("/prompts/add"), AuthMiddleware(handleAddPrompt))
|
||||
http.HandleFunc(makePath("/prompts/edit/"), AuthMiddleware(handleEditPrompt))
|
||||
http.HandleFunc(makePath("/prompts/edit-verbose/"), AuthMiddleware(handleEditVerbosePrompt))
|
||||
http.HandleFunc(makePath("/prompts/delete/"), AuthMiddleware(handleDeletePrompt))
|
||||
http.HandleFunc(makePath("/prompts/restore/"), AuthMiddleware(handleRestorePrompt))
|
||||
http.HandleFunc(makePath("/prompts/restore-verbose/"), AuthMiddleware(handleRestoreVerbosePrompt))
|
||||
http.HandleFunc(makePath("/prompts/save-lang"), AuthMiddleware(handleSaveLang))
|
||||
|
||||
// Веб-страница для выполнения запросов
|
||||
http.HandleFunc("/run", handleExecutePage)
|
||||
http.HandleFunc(makePath("/run"), AuthMiddleware(CSRFMiddleware(handleExecutePage)))
|
||||
|
||||
// API для выполнения запросов
|
||||
http.HandleFunc("/api/execute", handleExecute)
|
||||
http.HandleFunc(makePath("/api/execute"), AuthMiddleware(CSRFMiddleware(handleExecute)))
|
||||
// API для сохранения результатов и истории
|
||||
http.HandleFunc("/api/save-result", handleSaveResult)
|
||||
http.HandleFunc("/api/add-to-history", handleAddToHistory)
|
||||
http.HandleFunc(makePath("/api/save-result"), AuthMiddleware(CSRFMiddleware(handleSaveResult)))
|
||||
http.HandleFunc(makePath("/api/add-to-history"), AuthMiddleware(CSRFMiddleware(handleAddToHistory)))
|
||||
}
|
||||
|
||||
// registerRoutes регистрирует все маршруты сервера
|
||||
func registerRoutes() {
|
||||
// Страница входа (без аутентификации)
|
||||
http.HandleFunc(makePath("/login"), handleLoginPage)
|
||||
|
||||
// API для аутентификации (без аутентификации)
|
||||
http.HandleFunc(makePath("/api/login"), handleLogin)
|
||||
http.HandleFunc(makePath("/api/logout"), handleLogout)
|
||||
http.HandleFunc(makePath("/api/validate-token"), handleValidateToken)
|
||||
|
||||
// Главная страница и файлы
|
||||
http.HandleFunc("/", handleResultsPage)
|
||||
http.HandleFunc("/file/", handleFileView)
|
||||
http.HandleFunc("/delete/", handleDeleteFile)
|
||||
http.HandleFunc(makePath("/"), AuthMiddleware(handleResultsPage))
|
||||
http.HandleFunc(makePath("/file/"), AuthMiddleware(handleFileView))
|
||||
http.HandleFunc(makePath("/delete/"), AuthMiddleware(handleDeleteFile))
|
||||
|
||||
// История запросов
|
||||
http.HandleFunc("/history", handleHistoryPage)
|
||||
http.HandleFunc("/history/view/", handleHistoryView)
|
||||
http.HandleFunc("/history/delete/", handleDeleteHistoryEntry)
|
||||
http.HandleFunc("/history/clear", handleClearHistory)
|
||||
http.HandleFunc(makePath("/history"), AuthMiddleware(handleHistoryPage))
|
||||
http.HandleFunc(makePath("/history/view/"), AuthMiddleware(handleHistoryView))
|
||||
http.HandleFunc(makePath("/history/delete/"), AuthMiddleware(handleDeleteHistoryEntry))
|
||||
http.HandleFunc(makePath("/history/clear"), AuthMiddleware(handleClearHistory))
|
||||
|
||||
// Управление промптами
|
||||
http.HandleFunc("/prompts", handlePromptsPage)
|
||||
http.HandleFunc("/prompts/add", handleAddPrompt)
|
||||
http.HandleFunc("/prompts/edit/", handleEditPrompt)
|
||||
http.HandleFunc("/prompts/edit-verbose/", handleEditVerbosePrompt)
|
||||
http.HandleFunc("/prompts/delete/", handleDeletePrompt)
|
||||
http.HandleFunc("/prompts/restore/", handleRestorePrompt)
|
||||
http.HandleFunc("/prompts/restore-verbose/", handleRestoreVerbosePrompt)
|
||||
http.HandleFunc("/prompts/save-lang", handleSaveLang)
|
||||
http.HandleFunc(makePath("/prompts"), AuthMiddleware(handlePromptsPage))
|
||||
http.HandleFunc(makePath("/prompts/add"), AuthMiddleware(handleAddPrompt))
|
||||
http.HandleFunc(makePath("/prompts/edit/"), AuthMiddleware(handleEditPrompt))
|
||||
http.HandleFunc(makePath("/prompts/edit-verbose/"), AuthMiddleware(handleEditVerbosePrompt))
|
||||
http.HandleFunc(makePath("/prompts/delete/"), AuthMiddleware(handleDeletePrompt))
|
||||
http.HandleFunc(makePath("/prompts/restore/"), AuthMiddleware(handleRestorePrompt))
|
||||
http.HandleFunc(makePath("/prompts/restore-verbose/"), AuthMiddleware(handleRestoreVerbosePrompt))
|
||||
http.HandleFunc(makePath("/prompts/save-lang"), AuthMiddleware(handleSaveLang))
|
||||
|
||||
// Веб-страница для выполнения запросов
|
||||
http.HandleFunc("/run", handleExecutePage)
|
||||
http.HandleFunc(makePath("/run"), AuthMiddleware(CSRFMiddleware(handleExecutePage)))
|
||||
|
||||
// API для выполнения запросов
|
||||
http.HandleFunc("/api/execute", handleExecute)
|
||||
http.HandleFunc(makePath("/api/execute"), AuthMiddleware(CSRFMiddleware(handleExecute)))
|
||||
// API для сохранения результатов и истории
|
||||
http.HandleFunc("/api/save-result", handleSaveResult)
|
||||
http.HandleFunc("/api/add-to-history", handleAddToHistory)
|
||||
http.HandleFunc(makePath("/api/save-result"), AuthMiddleware(CSRFMiddleware(handleSaveResult)))
|
||||
http.HandleFunc(makePath("/api/add-to-history"), AuthMiddleware(CSRFMiddleware(handleAddToHistory)))
|
||||
|
||||
// Регистрируем главную страницу без слэша в конце для BasePath
|
||||
basePath := config.AppConfig.Server.BasePath
|
||||
if basePath != "" && basePath != "/" {
|
||||
basePath = strings.TrimSuffix(basePath, "/")
|
||||
http.HandleFunc(basePath, AuthMiddleware(handleResultsPage))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user