aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2019-05-04 01:18:45 +0200
committerMarvin Borner2019-05-04 01:18:45 +0200
commit9bf091e3f12e80cf19511d51dc20cbcc9d40855b (patch)
tree93c5a4ef84346c7a056585b194295bbcd2ed1e2d
parenta6fc766d6eb808584c7cebea216f683b53f8e99b (diff)
Added command line arguments
Co-authored-by: LarsVomMars <lars@kroenner.eu>
-rw-r--r--src/main/kotlin/App.kt46
-rw-r--r--src/main/kotlin/UserHandler.kt4
2 files changed, 42 insertions, 8 deletions
diff --git a/src/main/kotlin/App.kt b/src/main/kotlin/App.kt
index 133c27b..a8c1b03 100644
--- a/src/main/kotlin/App.kt
+++ b/src/main/kotlin/App.kt
@@ -11,6 +11,7 @@ import io.javalin.security.*
import io.javalin.security.SecurityUtil.roles
import java.net.*
import java.util.logging.*
+import kotlin.system.*
const val fileHome = "files"
val databaseController = DatabaseController()
@@ -18,11 +19,8 @@ val userHandler = UserHandler()
val fileController = FileController()
private val log = Logger.getLogger("App.kt")
-fun main() {
- val app = Javalin.create().apply {
- port(7000)
- accessManager { handler, ctx, permittedRoles -> roleManager(handler, ctx, permittedRoles) }
- }.start()
+fun main(args: Array<String>) {
+ val app = startServer(args)
// Set up templating
RockerRuntime.getInstance().isReloading = false
@@ -171,12 +169,48 @@ fun roleManager(handler: Handler, ctx: Context, permittedRoles: Set<Role>) {
databaseController.getRoles(userHandler.getVerifiedUserId(ctx)).any { it in permittedRoles } -> handler.handle(
ctx
)
- //ctx.host()!!.contains("localhost") -> handler.handle(ctx) // DEBUG
+ // ctx.host()!!.contains("localhost") -> handler.handle(ctx) // DEBUG
else -> ctx.status(401).redirect("/user/login")
}
}
/**
+ * Starts the server and parses the command line arguments
+ */
+fun startServer(args: Array<String>): Javalin {
+ var runServer = true
+ var port = 7000
+
+ args.forEachIndexed { index, element ->
+ run {
+ val wantsPort = element.startsWith("-p") || element.startsWith("--port")
+ val wantsHelp = element.startsWith("-h") || element.startsWith("--help")
+
+ if (wantsPort) {
+ val portArgument = args[index + 1].toInt()
+ if (portArgument in 1..65535) port = portArgument
+ } else if (wantsHelp) {
+ runServer = false
+ log.info("Help:\nUse -p or --port to specify a port.")
+ }
+ }
+ }
+
+ return if (runServer) {
+ try {
+ Javalin.create().apply {
+ port(port)
+ accessManager { handler, ctx, permittedRoles -> roleManager(handler, ctx, permittedRoles) }
+ disableStartupBanner()
+ }.start()
+ } catch (_: Exception) {
+ log.warning("Port already in use!")
+ exitProcess(1)
+ }
+ } else exitProcess(1)
+}
+
+/**
* Declares the roles in which a user can be in
*/
enum class Roles : Role {
diff --git a/src/main/kotlin/UserHandler.kt b/src/main/kotlin/UserHandler.kt
index 5f369b1..16a8e9c 100644
--- a/src/main/kotlin/UserHandler.kt
+++ b/src/main/kotlin/UserHandler.kt
@@ -65,7 +65,7 @@ class UserHandler {
* Logs the user out of the system
*/
fun logout(ctx: Context) {
- ctx.clearCookieStore()
+ ctx.clearCookieStore() // TODO: Fix cookie clearing after logout
ctx.redirect("/")
}
@@ -135,7 +135,7 @@ class UserHandler {
if (databaseController.isUserRegistrationValid(username)) {
databaseController.createUser(username, password, "USER")
databaseController.removeRegistrationIndex(username)
- ctx.redirect("/login")
+ ctx.redirect("/user/login")
} else ctx.status(401).result("This user is not authorized to register.")
} else ctx.status(400).result("The passwords don't match!")
} catch (_: Exception) {