Files
qgo-server/main.go
Smile Rex 93d9a85967
All checks were successful
Create and publish a Docker image 🚀 / build-and-push-image (push) Successful in 1m14s
new quic release 2
2026-03-09 23:51:12 +03:00

49 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import (
"fmt"
"net/http"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/api/hello", func(w http.ResponseWriter, r *http.Request) {
// 1. Настройка CORS для работы со Svelte и другими фронтендами
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
// Если это Preflight-запрос (OPTIONS), просто отвечаем 200 OK
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
// 2. Логирование для отладки
// Traefik v3 пробрасывает протокол. Проверим также заголовок X-Forwarded-Proto
proto := r.Header.Get("X-Forwarded-Proto")
if proto == "" {
proto = r.Proto
}
fmt.Printf("Получен запрос. Метод: %s, Протокол: %s, Путь: %s\n", r.Method, proto, r.URL.Path)
// 3. Ответ клиенту
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
response := fmt.Sprintf("Привет! Твой запрос обработан через Traefik v3.\nПротокол (внутренний): %s\n", r.Proto)
w.Write([]byte(response))
})
// Порт 8080 внутри контейнера
port := ":8080"
fmt.Printf("Сервер запущен на порту %s...\n", port)
err := http.ListenAndServe(port, mux)
if err != nil {
fmt.Printf("Ошибка при запуске сервера: %s\n", err)
}
}