Commit 9720d50e authored by Mathieu's avatar Mathieu
Browse files

Initialize h2 database

parent c59cd707
......@@ -36,6 +36,10 @@ object Application extends App {
config.launchMode match {
case HelpMode println(usage)
case ServerMode =>
if (!Settings.location.exists)
Settings.location.mkdirs()
DB.initDB()
val server = new ConnectServer(secret = config.tokenSecret)
server.start()
}
......
......@@ -14,6 +14,8 @@ val scalaJWTVersion = "4.0.0"
val rosHttpVersion = "2.2.4"
val skuberVersion = "2.2.0"
val httpComponentsVersion = "4.5.9"
val slickVersion = "3.3.1"
val h2Version ="1.4.200"
val Resolvers = Seq(Resolver.sonatypeRepo("snapshots"),
"Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/",
......@@ -52,8 +54,10 @@ lazy val server = project.in(file("server")) settings (defaultSettings) settings
"org.scalaj" %% "scalaj-http" % "2.4.2",
"io.skuber" %% "skuber" % skuberVersion,
"io.monix" %% "monix" % "3.0.0",
"org.apache.httpcomponents" % "httpclient" % "4.5.9",
"org.apache.httpcomponents" % "httpmime" % "4.5.9",
"org.apache.httpcomponents" % "httpclient" % httpComponentsVersion,
"org.apache.httpcomponents" % "httpmime" % httpComponentsVersion,
"com.typesafe.slick" %% "slick" % slickVersion,
"com.h2database" % "h2" % h2Version
)
) dependsOn (shared) enablePlugins (ScalatraPlugin)
......
package org.openmoleconnect.server
import scala.concurrent.{Await, Future}
import scala.concurrent.duration.Duration
import slick.jdbc.H2Profile.api._
object DB {
case class UUID(value: String)
case class UUID(value: String) extends MappedTo[String]
case class Login(value: String)
case class Password(value: String)
case class Password(value: String) extends MappedTo[String]
case class User(login: Login, password: Password, uuid: UUID = UUID(""))
class Users(tag: Tag) extends Table[(UUID, String, Password)](tag, "USERS") {
def uuid = column[UUID]("UUID", O.PrimaryKey)
def login = column[String]("LOGIN")
def password = column[Password]("PASSWORD")
def * = (uuid, login, password)
}
val userTable = TableQuery[Users]
private val users = Seq(User(Login("foo"), Password("foo"), UUID("foo-123-567-foo")), User(Login("bar"), Password("bar"), UUID("bar-123-567-bar")))
def uuid(login: Login): Option[UUID] = users.find(_.login == login).map{_.uuid}
def uuid(login: Login): Option[UUID] = users.find(_.login == login).map {
_.uuid
}
def uuid(user: User): Option[UUID] = users.find(u => u.login == user.login && u.password == user.password).map {
_.uuid
}
lazy val db: Database = Database.forDriver(
driver = new org.h2.Driver,
url = s"jdbc:h2:/${Settings.location}/db"
)
def runTransaction[E <: Effect](actions: DBIOAction[_, NoStream, E]*) =
Await.result(
db.run(
DBIO.seq(
actions: _*
).transactionally), Duration.Inf)
def initDB() = {
runTransaction(userTable.schema.createIfNotExists)
}
def uuid(user: User): Option[UUID] = users.find(u=> u.login == user.login && u.password == user.password).map{_.uuid}
}
package org.openmoleconnect.server
object Settings {
val location = new java.io.File(System.getProperty("user.home"), ".openmole-connect")
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment