new quic release [chat tests] 3
All checks were successful
Create and publish a Docker image 🚀 / build-and-push-image (push) Successful in 1m29s
All checks were successful
Create and publish a Docker image 🚀 / build-and-push-image (push) Successful in 1m29s
This commit is contained in:
62
main.go
62
main.go
@@ -1,48 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/quic-go/webtransport-go"
|
||||
)
|
||||
|
||||
func main() {
|
||||
wm := &webtransport.Server{
|
||||
CheckOrigin: func(r *http.Request) bool { return true },
|
||||
}
|
||||
|
||||
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)
|
||||
mux.HandleFunc("/chat", func(w http.ResponseWriter, r *http.Request) {
|
||||
session, err := wm.Upgrade(w, r)
|
||||
if err != nil {
|
||||
fmt.Printf("Ошибка апгрейда: %v\n", err)
|
||||
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))
|
||||
go handleChatSession(session)
|
||||
})
|
||||
|
||||
// Порт 8080 внутри контейнера
|
||||
port := ":8080"
|
||||
fmt.Printf("Сервер запущен на порту %s...\n", port)
|
||||
fmt.Println("Сервер чата запущен на :8080")
|
||||
// Traefik пробросит сюда HTTP/3 запрос
|
||||
http.ListenAndServe(":8080", mux)
|
||||
}
|
||||
|
||||
err := http.ListenAndServe(port, mux)
|
||||
if err != nil {
|
||||
fmt.Printf("Ошибка при запуске сервера: %s\n", err)
|
||||
func handleChatSession(session *webtransport.Session) {
|
||||
for {
|
||||
// Принимаем двунаправленный поток (для сообщений)
|
||||
stream, err := session.AcceptStream(context.Background())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
go func(s webtransport.Stream) {
|
||||
defer s.Close()
|
||||
buf := make([]byte, 1024)
|
||||
n, _ := s.Read(buf)
|
||||
fmt.Printf("Сообщение: %s\n", string(buf[:n]))
|
||||
s.Write([]byte("Сервер: Принято!"))
|
||||
}(*stream)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user