Files
backend/cmd/server/main.go

76 lines
2.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 (
"log"
"net/http"
"os"
"gitea.mrixs.me/minecraft-platform/backend/internal/api"
"gitea.mrixs.me/minecraft-platform/backend/internal/core"
"gitea.mrixs.me/minecraft-platform/backend/internal/database"
"github.com/go-chi/chi/v5"
"github.comcom/go-chi/chi/v5/middleware"
)
func main() {
// Инициализируем соединение с БД
db := database.Connect()
defer db.Close()
// --- Собираем наши зависимости (Dependency Injection) ---
userRepo := &database.UserRepository{DB: db}
// Сервисы
userService := &core.UserService{Repo: userRepo}
authService := &core.AuthService{UserRepo: userRepo} // Новый сервис
// Инициализируем сервис профилей, читая путь к ключу и домен из переменных окружения
keyPath := os.Getenv("RSA_PRIVATE_KEY_PATH")
if keyPath == "" {
log.Fatal("RSA_PRIVATE_KEY_PATH environment variable is not set")
}
domain := os.Getenv("APP_DOMAIN") // Нам нужен домен для генерации URL
if domain == "" {
log.Fatal("APP_DOMAIN environment variable is not set")
}
profileService, err := core.NewProfileService(userRepo, keyPath, domain)
if err != nil {
log.Fatalf("Failed to create profile service: %v", err)
}
// Хендлеры
userHandler := &api.UserHandler{Service: userService}
authHandler := &api.AuthHandler{Service: authService} // Новый хендлер
profileHandler := &api.ProfileHandler{Service: profileService} // Новый хендлер
// --- Настраиваем роутер ---
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
// Группа маршрутов для Web API
r.Route("/api", func(r chi.Router) {
r.Post("/register", userHandler.Register)
})
// Группа маршрутов для Yggdrasil API
r.Route("/authserver", func(r chi.Router) {
r.Post("/authenticate", authHandler.Authenticate)
// Здесь будут другие эндпоинты: refresh, validate, signout, invalidate
})
// Группа маршрутов для Session Server API
r.Route("/sessionserver/session/minecraft", func(r chi.Router) {
r.Get("/profile/{uuid}", profileHandler.GetProfile)
// Здесь будет эндпоинт /join
})
// Маршрут для проверки, что сервер жив
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Backend server is running!"))
})
log.Println("Starting backend server on :8080")
if err := http.ListenAndServe(":8080", r); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}