Permalink
Browse files

MVC wiki app example ready to push on dotcloud

  • Loading branch information...
1 parent 48be6c5 commit 2bc061eb6eb658f297e5b3405f67db479b7f6203 @cedricss committed Aug 16, 2012
Showing with 223 additions and 2 deletions.
  1. +31 −0 Makefile
  2. +72 −0 Makefile.common
  3. +1 −1 builder
  4. +11 −0 opa.conf
  5. +7 −0 resources/css/style.css
  6. +2 −1 run
  7. +20 −0 src/controller.opa
  8. +26 −0 src/model.opa
  9. +53 −0 src/view.opa
View
@@ -0,0 +1,31 @@
+########################################
+# USER VARIABLES
+EXE = wiki.exe
+ifdef SystemRoot
+ RUN_CMD = $(EXE)
+else
+ RUN_CMD = ./$(EXE)
+endif
+
+PACKNAME =
+SRC =
+PCKDIR = ./plugins/
+PCK =
+PLUGIN =
+PLUGINDIR =
+OTHER_DEPENDS = resources/*
+CONF_FILE = opa.conf
+
+#Compiler variables
+OPACOMPILER ?= ~/opa/bin/opa
+FLAG = --opx-dir _build --import-package stdlib.database.mongo
+PORT = 8080
+
+RUN_OPT =
+
+default: exe
+
+run: exe
+ $(RUN_CMD) $(RUN_OPT) || true
+
+include Makefile.common
View
@@ -0,0 +1,72 @@
+########################################
+# MAKEFILE VARIABLES
+OPACOMPILER ?= opa
+OPA = $(OPACOMPILER) $(FLAG) $(OPAOPT)
+PWD ?= $(shell pwd)
+BUILDDIR ?= _build
+export BUILDDIR
+OPAGENAPI ?= $(OPACOMPILER) --api-only
+DEPENDS = $(SRC) $(PCK:%=$(BUILDDIR)/%) $(PLUGIN:%=$(BUILDDIR)/%)
+
+ifdef CONF_FILE
+ CONFIG = --conf $(CONF_FILE) --conf-opa-files
+else
+ CONFIG =
+endif
+
+ifdef SystemRoot
+ RM = rmdir /s /q
+ RMFILE = del /s /q
+else
+ RM = rm -rf
+ RMFILE = rm
+endif
+
+# Hack to bypass the lack of "make -p" on Windows:
+_ = $(shell mkdir _build 2>/dev/null)
+
+########################################
+# MAIN RULE
+exe : $(EXE)
+pack : $(PACKNAME)
+
+########################################
+# MAIN PACKAGE BUILDING
+$(PACKNAME) : $(BUILDDIR)/$(PACKNAME)
+
+$(BUILDDIR)/$(PACKNAME) : $(DEPENDS)
+ @echo "### Building package $(PACKNAME)"
+ $(OPA) --autocompile $(SRC) $(PLUGIN:%=$(BUILDDIR)/%) --build-dir $(BUILDDIR) -I $(BUILDDIR) $(OPAOPT)
+ $(RM) $(BUILDDIR)/$(PACKNAME)
+ @mv $(PACKNAME) $(BUILDDIR)/
+
+########################################
+# SUBS PACKAGE/PLUGIN BUILDING
+$(BUILDDIR)/%.opx :
+ make $(@:$(BUILDDIR)/%.opx=-C $(PCKDIR)/%) pack
+
+$(BUILDDIR)/%.opp :
+ make $(@:$(BUILDDIR)/%.opp=-C $(PLUGINDIR)/%)
+
+########################################
+# EXECUTABLE BUILDING
+$(EXE) : pack $(SRC) $(MAINSRC) $(DEPENDS) $(OTHER_DEPENDS)
+ @echo "### Building executable $(EXE) "
+ $(OPA) $(COMPILOPT) $(MAINSRC) $(CONFIG) $(PLUGIN:%=$(BUILDDIR)/%) -o $@ --build-dir $(BUILDDIR)/$(EXE)
+
+$(EXE:%.exe=%.run) : $(EXE)
+ ./$(EXE) -p $(PORT)
+
+########################################
+# Used by recursives makefile
+pack.depends :
+ @echo $(PCK) $(PLUGIN)
+
+########################################
+# CLEANING
+clean ::
+ @$(PCK:%.opx=make -C $(PCKDIR)/% clean &&) $(PLUGIN:%.opp=make -C $(PLUGINDIR)/% clean &&) echo "### Cleaning $(BUILDDIR)";
+ $(RM) $(BUILDDIR) 2>/dev/null
+ $(RMFILE) $(EXE) 2>/dev/null
+
+
View
@@ -9,4 +9,4 @@ if [ ! -d ~/opa ] ; then
sh install -- ~/opa
fi
-~/opa/bin/opa app.opa -o app.js
+make
View
@@ -0,0 +1,11 @@
+wiki.controller:
+ import wiki.view
+ src/controller.opa
+
+wiki.view:
+ import wiki.model
+ import stdlib.themes.bootstrap
+ src/view.opa
+
+wiki.model:
+ src/model.opa
View
@@ -0,0 +1,7 @@
+#main {
+ margin-top: 50px;
+}
+
+textarea {
+ width: 100%;
+}
View
3 run
@@ -2,7 +2,8 @@
MONGO_URL="${DOTCLOUD_DB_MONGODB_LOGIN}:${DOTCLOUD_DB_MONGODB_PASSWORD}@${DOTCLOUD_DB_MONGODB_HOST}:${DOTCLOUD_DB_MONGODB_PORT}"
-exec ./app.js \
+exec ./wiki.exe \
--logs-path /var/log/supervisor \
--db-remote "$MONGO_URL" \
+ --verbose 8 \
$@
View
@@ -0,0 +1,20 @@
+resources = @static_resource_directory("resources")
+
+custom = {
+ parser {
+ case r={Server.resource_map(resources)} : r
+ case "/" : Resource.default_redirection_page("/index.html")
+ case "/statistics" : Resource.page("Display", View.statistics())
+ case p=(.*) : path = Text.to_string(p); Resource.page(path, View.page(path))
+ }
+}
+
+Server.start(Server.http, [
+ { register:
+ [ { doctype: { html5 } },
+ { js: [ ] },
+ { css: [ "/resources/css/style.css"] }
+ ]
+ },
+ { ~custom }
+])
View
@@ -0,0 +1,26 @@
+type page = {
+ string path,
+ string content,
+ int counter
+}
+
+database admin {
+ page /page[{path}]
+}
+
+module Model {
+
+ function get_content(path) {
+ /admin/page[{~path}]/counter++;
+ /admin/page[{~path}]/content
+ }
+
+ function set_content(path, content) {
+ /admin/page[{~path}]/content <- content
+ }
+
+ function statistics() {
+ DbSet.iterator(/admin/page)
+ }
+
+}
View
@@ -0,0 +1,53 @@
+module View {
+
+ function template(content) {
+ <div class="navbar navbar-fixed-top">
+ <div class="navbar-inner">
+ <div class="container">
+ <a class="brand" href="./index.html">wiki</a>
+ <div class="nav-collapse collapse">
+ <ul class="nav">
+ <li><a href="/statistics">Statistics</a></li>
+ </ul>
+ </div>
+ </div>
+ </div>
+ </div>
+ <div id=#main class="container-fluid">
+ <div class="row-fluid">
+ {content}
+ </div>
+ <hr>
+ <footer>
+ <p>wiki</p>
+ </footer>
+ </div>
+ }
+
+ function statistics() {
+ tbody = Iter.fold(
+ function(page, acc) { <>{acc}<tr><td>{page.path}</td><td>{page.counter}</td></tr></> },
+ Model.statistics(), <></>
+ )
+ content =
+ <h3>Page statistics</h3>
+ <table class="table table-bordered">
+ <thead><tr><td>Path</td><td>Counter</td></tr></thead>
+ <tbody>{tbody}</tbody>
+ </table>
+ template(content)
+ }
+
+ function page(path) {
+ content =
+ <div class="hero-unit">
+ <h3>{path}</h3>
+ <textarea id=#content rows="15">{ Model.get_content(path) }</textarea>
+ <button class="btn" onclick={ function(_) Model.set_content(path, Dom.get_value(#content)) }>
+ Save
+ </button>
+ </div>;
+ template(content)
+ }
+
+}

0 comments on commit 2bc061e

Please sign in to comment.