v01
This commit is contained in:
@@ -9,6 +9,7 @@ import io.vertx.ext.web.Router;
|
||||
import io.vertx.ext.web.handler.BodyHandler;
|
||||
import io.vertx.ext.web.handler.SessionHandler;
|
||||
import io.vertx.ext.web.handler.StaticHandler;
|
||||
import io.vertx.ext.web.sstore.LocalSessionStore;
|
||||
import io.vertx.ext.web.sstore.SessionStore;
|
||||
import io.vertx.ext.web.sstore.redis.RedisSessionStore;
|
||||
import io.vertx.mysqlclient.MySQLConnectOptions;
|
||||
@@ -27,7 +28,6 @@ public class MainVerticle extends AbstractVerticle {
|
||||
private static final Logger log = LoggerFactory.getLogger(MainVerticle.class);
|
||||
|
||||
private Pool dbPool;
|
||||
private Redis redisClient;
|
||||
private UserService userService;
|
||||
|
||||
@Override
|
||||
@@ -39,10 +39,10 @@ public class MainVerticle extends AbstractVerticle {
|
||||
.put("db_name", System.getenv().getOrDefault("DB_NAME", "admin_db"))
|
||||
.put("db_user", System.getenv().getOrDefault("DB_USER", "admin_user"))
|
||||
.put("db_password", System.getenv().getOrDefault("DB_PASSWORD", "admin_pass"))
|
||||
.put("redis_host", System.getenv().getOrDefault("REDIS_HOST", "localhost"))
|
||||
.put("redis_port", Integer.parseInt(System.getenv().getOrDefault("REDIS_PORT", "6379")))
|
||||
.put("http_port", Integer.parseInt(System.getenv().getOrDefault("HTTP_PORT", "8080")));
|
||||
|
||||
log.info("Starting with config: {}", config.encodePrettily());
|
||||
|
||||
// Подключение к MariaDB
|
||||
MySQLConnectOptions connectOptions = new MySQLConnectOptions()
|
||||
.setHost(config.getString("db_host"))
|
||||
@@ -50,109 +50,145 @@ public class MainVerticle extends AbstractVerticle {
|
||||
.setDatabase(config.getString("db_name"))
|
||||
.setUser(config.getString("db_user"))
|
||||
.setPassword(config.getString("db_password"));
|
||||
|
||||
PoolOptions poolOptions = new PoolOptions().setMaxSize(5);
|
||||
dbPool = Pool.pool(vertx, connectOptions, poolOptions);
|
||||
|
||||
// Подключение к Redis
|
||||
RedisOptions redisOptions = new RedisOptions()
|
||||
.setConnectionString("redis://" + config.getString("redis_host") + ":" + config.getInteger("redis_port"));
|
||||
redisClient = Redis.createClient(vertx, redisOptions);
|
||||
|
||||
// Инициализация сервисов
|
||||
userService = new UserService(dbPool);
|
||||
|
||||
// Инициализация БД (создание таблицы users)
|
||||
userService.initDatabase().compose(v -> {
|
||||
// Настройка сессий с Redis
|
||||
SessionStore sessionStore = RedisSessionStore.create(vertx, redisClient);
|
||||
SessionHandler sessionHandler = SessionHandler.create(sessionStore)
|
||||
.setSessionCookieName("admin.session")
|
||||
.setCookieHttpOnlyFlag(true)
|
||||
.setCookieSecureFlag(false) // для разработки, в продакшене true + HTTPS
|
||||
.setSessionTimeout(3600000); // 1 час
|
||||
userService.initDatabase()
|
||||
.onSuccess(v -> log.info("Database initialized successfully"))
|
||||
.onFailure(err -> {
|
||||
log.error("Failed to initialize database", err);
|
||||
startPromise.fail(err);
|
||||
return;
|
||||
});
|
||||
|
||||
// Роутер
|
||||
Router router = Router.router(vertx);
|
||||
router.route().handler(BodyHandler.create());
|
||||
router.route().handler(sessionHandler);
|
||||
// Настройка сессий (используем LocalSessionStore для простоты)
|
||||
SessionStore sessionStore = LocalSessionStore.create(vertx);
|
||||
SessionHandler sessionHandler = SessionHandler.create(sessionStore)
|
||||
.setSessionCookieName("admin.session")
|
||||
.setCookieHttpOnlyFlag(true)
|
||||
.setCookieSecureFlag(false)
|
||||
.setSessionTimeout(3600000); // 1 час
|
||||
|
||||
// Health Checks
|
||||
HealthChecks hc = HealthChecks.create(vertx);
|
||||
hc.register("database", promise -> dbPool.getConnection().onComplete(ar -> {
|
||||
// Роутер
|
||||
Router router = Router.router(vertx);
|
||||
router.route().handler(BodyHandler.create());
|
||||
router.route().handler(sessionHandler);
|
||||
|
||||
// CORS для разработки
|
||||
router.route().handler(ctx -> {
|
||||
ctx.response()
|
||||
.putHeader("Access-Control-Allow-Origin", "*")
|
||||
.putHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
|
||||
.putHeader("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
||||
.putHeader("Access-Control-Allow-Credentials", "true");
|
||||
|
||||
if (ctx.request().method().name().equals("OPTIONS")) {
|
||||
ctx.response().end();
|
||||
} else {
|
||||
ctx.next();
|
||||
}
|
||||
});
|
||||
|
||||
// Health Checks
|
||||
HealthChecks hc = HealthChecks.create(vertx);
|
||||
hc.register("database", promise ->
|
||||
dbPool.getConnection().onComplete(ar -> {
|
||||
if (ar.succeeded()) {
|
||||
ar.result().close();
|
||||
promise.complete(Status.OK());
|
||||
} else {
|
||||
promise.fail(ar.cause());
|
||||
}
|
||||
}));
|
||||
hc.register("redis", promise -> {
|
||||
RedisAPI.api(redisClient)
|
||||
.ping(Collections.singletonList("ping"))
|
||||
.onSuccess(response -> {
|
||||
if ("PONG".equals(response.toString())) {
|
||||
promise.complete(Status.OK());
|
||||
} else {
|
||||
promise.fail("Unexpected ping response: " + response);
|
||||
}
|
||||
})
|
||||
.onFailure(promise::fail);
|
||||
});
|
||||
router.get("/health").handler(rc -> hc.checkStatus().onComplete(ar -> {
|
||||
})
|
||||
);
|
||||
|
||||
router.get("/health").handler(rc ->
|
||||
hc.checkStatus().onComplete(ar -> {
|
||||
if (ar.succeeded()) {
|
||||
rc.response().end(ar.result().toJson().encodePrettily());
|
||||
} else {
|
||||
rc.response().setStatusCode(503).end(ar.cause().getMessage());
|
||||
}
|
||||
}));
|
||||
})
|
||||
);
|
||||
|
||||
// Статическая раздача фронтенда (из webroot)
|
||||
router.route("/*").handler(StaticHandler.create("webroot").setCachingEnabled(false).setIndexPage("index.html"));
|
||||
// API маршруты
|
||||
AuthHandler authHandler = new AuthHandler(userService);
|
||||
SetupHandler setupHandler = new SetupHandler(userService);
|
||||
|
||||
// API маршруты
|
||||
AuthHandler authHandler = new AuthHandler(userService);
|
||||
SetupHandler setupHandler = new SetupHandler(userService);
|
||||
// Проверка статуса (нужна ли инициализация)
|
||||
router.get("/api/status").handler(setupHandler::checkStatus);
|
||||
|
||||
// Регистрация первого администратора (если таблица пуста)
|
||||
router.post("/api/setup").handler(setupHandler::handleSetup);
|
||||
// Регистрация первого администратора
|
||||
router.post("/api/setup").handler(setupHandler::handleSetup);
|
||||
|
||||
// Логин
|
||||
router.post("/api/login").handler(authHandler::handleLogin);
|
||||
// Логин
|
||||
router.post("/api/login").handler(authHandler::handleLogin);
|
||||
|
||||
// Выход
|
||||
router.post("/api/logout").handler(authHandler::handleLogout);
|
||||
// Выход
|
||||
router.post("/api/logout").handler(authHandler::handleLogout);
|
||||
|
||||
// Защищённые маршруты (требуют сессии)
|
||||
router.route("/api/admin/*").handler(authHandler::requireAuth);
|
||||
// Защищённые маршруты
|
||||
router.route("/api/admin/*").handler(authHandler::requireAuth);
|
||||
|
||||
// Пример защищённого эндпоинта - получение списка пользователей
|
||||
router.get("/api/admin/users").handler(rc -> {
|
||||
userService.getAllUsers().onComplete(ar -> {
|
||||
if (ar.succeeded()) {
|
||||
rc.response().end(ar.result().encode());
|
||||
} else {
|
||||
rc.response().setStatusCode(500).end(ar.cause().getMessage());
|
||||
}
|
||||
});
|
||||
// Получение списка пользователей
|
||||
router.get("/api/admin/users").handler(rc -> {
|
||||
userService.getAllUsers().onComplete(ar -> {
|
||||
if (ar.succeeded()) {
|
||||
rc.response()
|
||||
.putHeader("Content-Type", "application/json")
|
||||
.end(ar.result().encode());
|
||||
} else {
|
||||
rc.response().setStatusCode(500).end(ar.cause().getMessage());
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Запуск HTTP сервера
|
||||
int port = config.getInteger("http_port");
|
||||
return vertx.createHttpServer().requestHandler(router).listen(port);
|
||||
}).onComplete(ar -> {
|
||||
if (ar.succeeded()) {
|
||||
log.info("Server started on port {}", config.getInteger("http_port"));
|
||||
startPromise.complete();
|
||||
// Получение текущего пользователя
|
||||
router.get("/api/admin/me").handler(rc -> {
|
||||
Integer userId = rc.session().get("userId");
|
||||
if (userId != null) {
|
||||
rc.response()
|
||||
.putHeader("Content-Type", "application/json")
|
||||
.end(new JsonObject()
|
||||
.put("id", userId)
|
||||
.put("login", rc.session().get("login"))
|
||||
.encode());
|
||||
} else {
|
||||
log.error("Failed to start", ar.cause());
|
||||
startPromise.fail(ar.cause());
|
||||
rc.response().setStatusCode(401).end();
|
||||
}
|
||||
});
|
||||
|
||||
// Статическая раздача фронтенда
|
||||
router.route("/*").handler(StaticHandler.create("webroot")
|
||||
.setCachingEnabled(false)
|
||||
.setIndexPage("index.html"));
|
||||
|
||||
// Запуск HTTP сервера
|
||||
int port = config.getInteger("http_port");
|
||||
vertx.createHttpServer()
|
||||
.requestHandler(router)
|
||||
.listen(port).onComplete(http -> {
|
||||
if (http.succeeded()) {
|
||||
log.info("HTTP server started on port {}", port);
|
||||
startPromise.complete();
|
||||
} else {
|
||||
log.error("Failed to start HTTP server", http.cause());
|
||||
startPromise.fail(http.cause());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
if (dbPool != null) dbPool.close();
|
||||
public void stop(Promise<Void> stopPromise) {
|
||||
if (dbPool != null) {
|
||||
dbPool.close();
|
||||
}
|
||||
stopPromise.complete();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user