113 lines
3.4 KiB
Java
113 lines
3.4 KiB
Java
|
|
package ins.frontend.utils;
|
||
|
|
|
||
|
|
import dev.unlegitdqrk.unlegitlibrary.file.ConfigurationManager;
|
||
|
|
import org.openautonomousconnection.webserver.utils.PasswordHasher;
|
||
|
|
import org.openautonomousconnection.webserver.utils.Pbkdf2Sha256Hasher;
|
||
|
|
|
||
|
|
import java.io.File;
|
||
|
|
import java.io.IOException;
|
||
|
|
import java.sql.DriverManager;
|
||
|
|
import java.sql.SQLException;
|
||
|
|
import java.util.Objects;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Application singleton holding DAO + password hasher.
|
||
|
|
*/
|
||
|
|
public final class WebApp {
|
||
|
|
|
||
|
|
private static volatile WebApp INSTANCE;
|
||
|
|
private static boolean isInit;
|
||
|
|
private static UserDao.DataSourceProvider dsp = null;
|
||
|
|
private final UserDao userDao;
|
||
|
|
private final RegistrarDao registrarDao;
|
||
|
|
private final PasswordHasher passwordHasher;
|
||
|
|
|
||
|
|
private WebApp(UserDao userDao, RegistrarDao registrarDao, PasswordHasher passwordHasher) {
|
||
|
|
this.userDao = Objects.requireNonNull(userDao, "userDao");
|
||
|
|
this.registrarDao = Objects.requireNonNull(registrarDao, "registrarDao");
|
||
|
|
this.passwordHasher = Objects.requireNonNull(passwordHasher, "passwordHasher");
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Initializes the singleton (call once at server startup).
|
||
|
|
*/
|
||
|
|
public static void init() {
|
||
|
|
if (isInit) return;
|
||
|
|
|
||
|
|
try {
|
||
|
|
if (!new File(new File(".").getParent(), "db.properties").exists()) {
|
||
|
|
new File(new File(".").getParent(), "db.properties").createNewFile();
|
||
|
|
}
|
||
|
|
|
||
|
|
ConfigurationManager config = new ConfigurationManager(new File(new File(".").getParent(), "db.properties"));
|
||
|
|
config.loadProperties();
|
||
|
|
|
||
|
|
if (!config.isSet("db.url")) {
|
||
|
|
config.set(
|
||
|
|
"db.url",
|
||
|
|
"jdbc:mariadb://localhost:3306/ins?useUnicode=true&characterEncoding=utf8"
|
||
|
|
);
|
||
|
|
config.saveProperties();
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!config.isSet("db.user")) {
|
||
|
|
config.set("db.user", "username");
|
||
|
|
config.saveProperties();
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!config.isSet("db.password")) {
|
||
|
|
config.set("db.password", "password");
|
||
|
|
config.saveProperties();
|
||
|
|
}
|
||
|
|
|
||
|
|
dsp = () -> {
|
||
|
|
try {
|
||
|
|
isInit = true;
|
||
|
|
|
||
|
|
return DriverManager.getConnection(config.getString("db.url"), config.getString("db.user"), config.getString("db.password"));
|
||
|
|
} catch (SQLException e) {
|
||
|
|
throw new SQLException("Failed to open DB connection", e);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
} catch (IOException e) {
|
||
|
|
throw new RuntimeException("Failed to create config", e);
|
||
|
|
}
|
||
|
|
UserDao udao = new UserDao(dsp);
|
||
|
|
RegistrarDao rdao = new RegistrarDao(dsp);
|
||
|
|
PasswordHasher hasher = new Pbkdf2Sha256Hasher(150_000, 16, 32);
|
||
|
|
|
||
|
|
INSTANCE = new WebApp(udao, rdao, hasher);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Returns the initialized instance.
|
||
|
|
*
|
||
|
|
* @return app
|
||
|
|
*/
|
||
|
|
public static WebApp get() {
|
||
|
|
WebApp v = INSTANCE;
|
||
|
|
if (v == null) throw new IllegalStateException("Oac2WebApp not initialized. Call Oac2WebApp.init(...) first.");
|
||
|
|
return v;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return user DAO
|
||
|
|
*/
|
||
|
|
public UserDao users() {
|
||
|
|
return userDao;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return registrar DAO
|
||
|
|
*/
|
||
|
|
public RegistrarDao dao() {
|
||
|
|
return registrarDao;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return password hasher
|
||
|
|
*/
|
||
|
|
public PasswordHasher passwordHasher() {
|
||
|
|
return passwordHasher;
|
||
|
|
}
|
||
|
|
}
|