11package space .itoncek .trailcompass ;
22
33import io .javalin .Javalin ;
4- import static io .javalin .apibuilder .ApiBuilder .post ;
5- import static io .javalin .apibuilder .ApiBuilder .ws ;
6- import static org .apache .commons .codec .digest .DigestUtils .sha512 ;
74import org .hibernate .SessionFactory ;
85import org .hibernate .jpa .HibernatePersistenceConfiguration ;
96import org .hibernate .tool .schema .Action ;
2017import java .io .IOException ;
2118import java .util .TreeSet ;
2219
20+ import static io .javalin .apibuilder .ApiBuilder .post ;
21+ import static org .apache .commons .codec .digest .DigestUtils .sha512 ;
22+ import static space .itoncek .trailcompass .commons .utils .RandomUtils .generateRandomString ;
23+ import static space .itoncek .trailcompass .commons .utils .RandomUtils .pickRandomStrings ;
24+
2325public class TrailServer {
2426 private static final Logger log = LoggerFactory .getLogger (TrailServer .class );
2527 public final boolean dev = System .getenv ("dev" ) != null && Boolean .parseBoolean (System .getenv ("dev" ));
2628 private final int PORT = System .getenv ("PORT" ) == null ? 8080 : Integer .parseInt (System .getenv ("PORT" ));
27- private final String CONNECTION_STRING = System .getenv ("CONNECTION_STRING" ) == null ? "jdbc:postgresql://localhost:5002/TrailCompass" : System .getenv ("CONNECTION_STRING" );
28- private final String CONNECTION_USER = System .getenv ("CONNECTION_USER" ) == null ? "postgres" : System .getenv ("CONNECTION_USER" );
29- private final String CONNECTION_PASSWORD = System .getenv ("CONNECTION_PASSWORD" ) == null ? "postgres" : System .getenv ("CONNECTION_PASSWORD" );
30- public final SessionFactory ef ;
29+ public final SessionFactory ef ;
3130 private final Javalin app ;
3231 public final TrailCompassHandler tch ;
3332 public final LocationManager lm ;
@@ -36,6 +35,9 @@ public class TrailServer {
3635 public final ConfigManager config ;
3736
3837 public TrailServer () {
38+ String connString = System .getenv ("CONNECTION_STRING" ) == null ? "jdbc:postgresql://localhost:5002/TrailCompass" : System .getenv ("CONNECTION_STRING" );
39+ String connUser = System .getenv ("CONNECTION_USER" ) == null ? "postgres" : System .getenv ("CONNECTION_USER" );
40+ String connPassword = System .getenv ("CONNECTION_PASSWORD" ) == null ? "postgres" : System .getenv ("CONNECTION_PASSWORD" );
3941 ef = new HibernatePersistenceConfiguration ("TrailCompass" )
4042 .managedClasses (
4143 PerformanceTrace .class ,
@@ -46,10 +48,10 @@ public TrailServer() {
4648 )
4749 .jdbcPoolSize (8 )
4850 // PostgreSQL
49- .jdbcUrl (CONNECTION_STRING )
51+ .jdbcUrl (connString )
5052 // Credentials
51- .jdbcUsername (CONNECTION_USER )
52- .jdbcPassword (CONNECTION_PASSWORD )
53+ .jdbcUsername (connUser )
54+ .jdbcPassword (connPassword )
5355 // Automatic schema export
5456 .schemaToolingAction (Action .UPDATE )
5557 // SQL statement logging
@@ -96,10 +98,7 @@ public TrailServer() {
9698 cfg .router .caseInsensitiveRoutes = true ;
9799 cfg .router .treatMultipleSlashesAsSingleSlash = true ;
98100 cfg .router .ignoreTrailingSlashes = true ;
99- cfg .router .apiBuilder (() -> {
100- post ("/" , tch ::handle );
101- ws ("/gamemanager/await" , gm ::awaitWS );
102- });
101+ cfg .router .apiBuilder (() -> post ("/" , tch ::handle ));
103102 });
104103 }
105104
@@ -130,14 +129,14 @@ public void start() {
130129 }
131130
132131 final boolean [] needsCardReset = {false };
133- ef .runInTransaction (em -> {
132+ ef .runInTransaction (em -> {
134133 KeyStore ks = em .find (KeyStore .class , KeyStore .KeystoreKeys .DECK_DEALT );
135134 if (!Boolean .parseBoolean (ks .getKvalue ())) {
136135 needsCardReset [0 ] = true ;
137136 }
138137 });
139138
140- if (needsCardReset [0 ]) {
139+ if (needsCardReset [0 ]) {
141140 dm .resetDeck ();
142141 ef .runInTransaction (em -> {
143142 KeyStore ks = em .find (KeyStore .class , KeyStore .KeystoreKeys .DECK_DEALT );
@@ -149,14 +148,18 @@ public void start() {
149148 log .info ("{}\n {}" , TextGraphics .generateIntroMural (), TextGraphics .isDebuggerPresent () ? TextGraphics .generateDevWarningBox () : "" );
150149
151150 if (tch .ex .auth ().needsDefaultUser ()) {
152- // TODO)) REMOVE BEFORE RELEASE! DEBUG ONLY!
153- // String user = pickRandomStrings();
154- String user = "itoncek" ;
155- // String password = generateRandomString(16, true, false);
156- String password = "root" ;
157- tch .ex .auth ().createUser (user , sha512 (password ), true );
158- log .info (TextGraphics .generateLoginBox (user , password ));
159- }
151+ String user ;
152+ String password ;
153+ if (dev ) {
154+ user = "admin" ;
155+ password = "root" ;
156+ }else {
157+ user = pickRandomStrings ();
158+ password = generateRandomString (16 , true , false );
159+ }
160+ tch .ex .auth ().createUser (user , sha512 (password ), true );
161+ log .info (TextGraphics .generateLoginBox (user , password ));
162+ }
160163 }
161164
162165 private void stop () throws IOException {
0 commit comments