storage class

added config check in main page
This commit is contained in:
lukas-heiligenbrunner 2020-03-27 17:06:48 +01:00
parent 3818b6d1c7
commit b9058dcdba
2 changed files with 25 additions and 7 deletions

View File

@ -68,7 +68,12 @@ class Storage {
val inp = FileInputStream("settings.prop")
prop.load(inp)
mqttServer = prop.get("mqttserver") as String
mqttServer = prop["mqttserver"] as String
mqttPort = (prop["mqttport"] as String).toInt()
dbName = prop["dbname"] as String
dbUser = prop["dbuser"] as String
dbPassword = prop["dbpass"] as String
dbPort = (prop["dbport"] as String).toInt()
} catch (ee: FileNotFoundException) {
// file not generated yet
store()
@ -83,11 +88,11 @@ class Storage {
*/
fun store() {
prop["mqttserver"] = mqttServer
prop["mqttport"] = mqttPort
prop["mqttport"] = mqttPort.toString()
prop["dbname"] = dbName
prop["dbuser"] = dbUser
prop["dbpass"] = dbPassword
prop["dbport"] = dbPort
prop["dbport"] = dbPort.toString()
prop.store(FileOutputStream("settings.prop"), "")
}

View File

@ -4,6 +4,7 @@ import com.sun.net.httpserver.HttpExchange
import com.sun.net.httpserver.HttpHandler
import com.wasteinformationserver.basicutils.Log.Log.debug
import com.wasteinformationserver.basicutils.Log.Log.warning
import com.wasteinformationserver.basicutils.Storage
import com.wasteinformationserver.website.datarequests.login.LoginState
import java.io.IOException
@ -19,15 +20,25 @@ class MainPage : HttpHandler {
if (path == "/") {
path += "index.html"
}
debug("looking for: $path")
if (path.contains(".html")) {
// check if db and mqtt is configured properly
// todo check real connection / not only if filled in
if (!Storage.getInstance().isEveryThingDefined()) {
sendPage("/config.html", t)
return
}
if (LoginState.getObject().isLoggedIn || path == "/register.html" || path == "/index.html") { //pass only register page
sendPage(path, t)
} else {
}
else {
warning("user not logged in --> redirecting to login page")
sendPage("/index.html", t)
}
} else { //only detect login state on html pages
}
else { //only detect login state on html pages
sendPage(path, t)
}
}
@ -38,9 +49,11 @@ class MainPage : HttpHandler {
if (fs == null && path.contains(".html")) {
warning("wrong page sending 404")
sendPage("/404Error.html", t)
} else if (fs == null) {
}
else if (fs == null) {
warning("requested resource doesnt exist --> $path")
} else { // Object exists and is a file: accept with response code 200.
}
else { // Object exists and is a file: accept with response code 200.
var mime = "text/html"
val s = path.substring(path.length - 3)
if (s == ".js") mime = "application/javascript"