src/main/scala/bootstrap/liftweb/Boot.scala
changeset 2 cf829ec742b3
child 22 6b1a7f3429ca
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/bootstrap/liftweb/Boot.scala	Sun Apr 03 15:55:02 2011 +0200
@@ -0,0 +1,97 @@
+/*
+ * Copyright 2011 Tomas Zeman <tzeman@volny.cz>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package bootstrap.liftweb
+
+import net.liftweb.common._
+import net.liftweb.http._
+import net.liftweb.mapper._
+import net.liftweb.sitemap._
+import net.liftweb.sitemap.Loc._
+import net.liftweb.util._
+import net.liftweb.widgets.menu.MenuWidget
+import net.tz.lift.snippet.ActionLinks
+import net.tz.lift.util.YmdDateTimeConverter
+import radview.model._
+import radview.snippet._
+import scala.xml.NodeSeq
+
+class Boot extends Logger {
+
+  def boot = {
+    /* DB stuff */
+    initDb("radacct", RadAcctConnectionIdentifier)
+    initDb("radius", RadiusConnectionIdentifier)
+    initDb("syslog", SyslogConnectionIdentifier)
+    S.addAround(DB.buildLoanWrapper(List(RadAcctConnectionIdentifier,
+      RadiusConnectionIdentifier, SyslogConnectionIdentifier)))
+
+    if (Props.mode == Props.RunModes.Development)
+      DB.addLogFunc { (dbLog, l) => dbLog.statementEntries.foreach { e =>
+        debug("Query: " + e.statement)
+    }}
+
+    /* Date format */
+    LiftRules.dateTimeConverter.default.set { () => YmdDateTimeConverter }
+
+    /* Handle end slash and drop it (except for home page) */
+    LiftRules.statelessRewrite.append {
+      case RewriteRequest(ParsePath(xs,_,_,true),_,_) if (xs.size > 1) &&
+        (xs.lastOption == Some("index")) =>
+          RewriteResponse(xs dropRight 1)
+    }
+
+    /* Snippet dispatch */
+    LiftRules.snippetDispatch.append {
+      case "Menubar" => new AnyRef with DispatchSnippet {
+        def dispatch: DispatchIt = {
+          case _ => { xhtml => MenuWidget() }
+        }
+      }
+      case "action-links" => ActionLinks
+    }
+
+    /* Sitemap */
+    SiteMap.enforceUniqueLinks = false
+
+    LiftRules.setSiteMap(SiteMap(
+      Menu.i("Home") / "index" >> Hidden,
+      Menu(CellSnippet),
+      Menu.i("Cells") / "cell",
+      Menu(SessionSnippet),
+      Menu(AccountSnippet),
+      Menu.i("Accounts") / "account"
+    ))
+
+    /* Menu widget */
+    MenuWidget.init()
+
+    /* Http conf */
+    LiftRules.logServiceRequestTiming = false
+    LiftRules.early.append(_.setCharacterEncoding("UTF-8"))
+  }
+
+  def initDb(ident: String, connId: ConnectionIdentifier) = {
+    val driver = "com.mysql.jdbc.Driver"
+    val vendor = new StandardDBVendor(driver,
+      Props.get("db."+ident+".url") openOr ("jdbc:mysql://localhost/"+ident),
+      Props.get("db."+ident+".user"), Props.get("db."+ident+".pass"))
+    //LiftRules.unloadHooks.append(vendor.closeAllConnections_! _)
+    DB.defineConnectionManager(connId, vendor)
+    vendor.newConnection(connId)
+  }
+}
+
+// vim: set ts=2 sw=2 et: