diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | build.sc | 4 | ||||
-rw-r--r-- | server/src/Main.scala | 31 |
3 files changed, 32 insertions, 5 deletions
@@ -1 +1,3 @@ out/ + +messages.db @@ -5,7 +5,9 @@ object server extends ScalaModule { def ivyDeps = Agg( ivy"com.lihaoyi::cask:0.3.6", // web framework, http://www.lihaoyi.com/cask/ - ivy"com.lihaoyi::scalatags:0.7.0" // html rendering DSL http://www.lihaoyi.com/scalatags/ + ivy"com.lihaoyi::scalatags:0.7.0", // html rendering DSL http://www.lihaoyi.com/scalatags/ + ivy"io.getquill::quill-jdbc:3.4.10", // language integrated queries https://getquill.io/#docs + ivy"org.xerial:sqlite-jdbc:3.28.0" // actual database driver, note the single ':' ) } diff --git a/server/src/Main.scala b/server/src/Main.scala index fdcabb8..9d17fa4 100644 --- a/server/src/Main.scala +++ b/server/src/Main.scala @@ -1,16 +1,39 @@ + +import io.getquill.{SnakeCase, SqliteJdbcContext} import scala.collection.mutable +import com.typesafe.config.ConfigFactory +import java.nio.file.Files + object Main extends cask.MainRoutes { - val messages = mutable.ListBuffer.empty[Message] + object ctx extends SqliteJdbcContext(SnakeCase, ConfigFactory.parseString( + // This config uses the HOCON syntax https://github.com/lightbend/config/blob/master/HOCON.md + // It is usually read from a file. + s"""{driverClassName = "org.sqlite.JDBC", jdbcUrl = "jdbc:sqlite:messages.db"}""" + )) + import ctx._ // this import allows us to construct SQL queries in a nice DSL - messages += Message("John Smith", 0, "Hello, world!") + // tables aren't typically create from within a program, but we do it here' + // for demonstration purposes + ctx.executeAction( + """ + create table if not exists message( + author string not null, + date integer not null, + content string not null + ); + """ + ) @cask.get("/") - def get() = Templates.page(messages.toList).render + def get() = { + val messages: Seq[Message] = ctx.run(ctx.query[Message]) + Templates.page(messages).render + } @cask.postJson("/") def post(message: Message) = { - messages += message + run(query[Message].insert(lift(message))) } @cask.staticResources("/assets") |