Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

new version of hello_chat made with opa-create

  • Loading branch information...
commit 746e7af2efebe4af0435000bdf99b3d29bdd79cb 1 parent 79f02c7
@Aqua-Ye Aqua-Ye authored
View
31 Makefile
@@ -0,0 +1,31 @@
+########################################
+# USER VARIABLES
+EXE = hello_chat.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
+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
72 Makefile.common
@@ -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
106 hello_chat.opa
@@ -1,106 +0,0 @@
-/**
- * {1 Import standard classes of bootstrap css}
- *
- * see http://twitter.github.com/bootstrap/
- */
-import stdlib.themes.bootstrap
-
-/**
- * {1 Network infrastructure}
- */
-
-/**
- * The type of messages sent by a client to the chatroom
- */
-type message = { string author /**The name of the author (arbitrary string)*/
- , string text /**Content entered by the user*/
- }
-
-/**
- * The chatroom.
- */
-exposed Network.network(message) room = Network.cloud("room")
-
-/**
- * {1 User interface}
- */
-
-/**
- * Update the user interface in reaction to reception of a message.
- *
- * This function is meant to be registered with [room] as a callback.
- * Its sole role is to display the new message in [#conversation].
- *
- * @param x The message received from the chatroom
- */
-function user_update(message x) {
- line = <div class="row line">
- <div class="span1 columns userpic" />
- <div class="span2 columns user">{x.author}:</div>
- <div class="span13 columns message">{x.text}</div>
- </div>;
- #conversation =+ line;
- Dom.scroll_to_bottom(#conversation);
-}
-
-/**
- * Broadcast text to the [room].
- *
- * Read the contents of [#entry], clear these contents and send the message to [room].
- *
- * @param author The name of the author. Will be included in the message broadcasted.
- */
-function broadcast(author) {
- text = Dom.get_value(#entry);
- message = ~{author, text};
- Network.broadcast(message, room);
- Dom.clear_value(#entry);
-}
-
-/**
- * Build the user interface for a client.
- *
- * Pick a random author name which will be used throughout the chat.
- *
- * @return The user interface, ready to be sent by the server to the client on connection.
- */
-function start() {
- author = Random.string(8);
- <div class="navbar navbar-fixed-top">
- <div class="navbar-inner">
- <div class="container">
- <div id=#logo />
- </>
- </>
- </>
- <div id=#conversation class="container"
- onready={function(_) { Network.add_callback(user_update, room) }}></>
- <div id=#footer class="navbar navbar-fixed-bottom">
- <div class="container">
- <div class="input-append">
- <input id=#entry class="input-xlarge" type="text"
- onnewline={function(_) { broadcast(author) }}>
- <button class="btn btn-primary" type="button" onclick={function(_) { broadcast(author) }}>Post</button>
- </>
- </>
- </>
-}
-
-/**
- * {1 Application}
- */
-
-/**
- * Main entry point.
- *
- * Construct an application called "Chat" (users will see the name in the title bar),
- * embedding statically the contents of directory "resources", using the global stylesheet
- * "resources/chat.css" and the user interface defined in [start].
- */
-Server.start(
- Server.http,
- [ { resources: @static_resource_directory("resources") }
- , { register: { css: ["resources/chat.css"] } }
- , { title: "Chat", page: start }
- ]
-);
View
80 hello_chat_min.opa
@@ -1,80 +0,0 @@
-/**
- * Minimal version of hello_chat
- * - no style
- * - no @publish
- */
-
-/**
- * {1 Network infrastructure}
- */
-
-/**
- * The type of messages sent by a client to the chatroom
- */
-type message = { string author /**The name of the author (arbitrary string)*/
- , string text /**Content entered by the user*/
- }
-
-/**
- * The chatroom.
- */
-room = Network.network(message) (Network.cloud("room"))
-
-/**
- * {1 User interface}
- */
-
-/**
- * Update the user interface in reaction to reception of a message.
- *
- * This function is meant to be registered with [room] as a callback.
- * Its sole role is to display the new message in [#conversation].
- *
- * @param x The message received from the chatroom
- */
-function user_update(message x) {
- line = <div>{x.author}: {x.text}</div>;
- #conversation =+ line;
- Dom.scroll_to_bottom(#conversation);
-}
-
-/**
- * Broadcast text to the [room].
- *
- * Read the contents of [#entry], clear these contents and send the message to [room].
- *
- * @param author The name of the author. Will be included in the message broadcasted.
- */
-function broadcast(author) {
- text = Dom.get_value(#entry);
- message = ~{author, text};
- Network.broadcast(message, room);
- Dom.clear_value(#entry);
-}
-
-/**
- * Build the user interface for a client.
- *
- * Pick a random author name which will be used throughout the chat.
- *
- * @return The user interface, ready to be sent by the server to the client on connection.
- */
-function start() {
- author = Random.string(8);
- <div id=#conversation onready={function(_) { Network.add_callback(user_update, room) }} />
- <input id=#entry onnewline={function(_) { broadcast(author) }} />
- <input type="button" onclick={function(_) { broadcast(author) }} value="Post" />;
-}
-
-/**
- * {1 Application}
- */
-
-/**
- * Main entry point.
- *
- * Construct an application called "Chat" (users will see the name in the title bar),
- * embedding statically the contents of directory "resources", using the global stylesheet
- * "resources/chat.css" and the user interface defined in [start].
- */
-Server.start(Server.http, {title: "Chat", page: start });
View
11 opa.conf
@@ -0,0 +1,11 @@
+hello_chat.controller:
+ import hello_chat.view
+ src/controller.opa
+
+hello_chat.view:
+ import hello_chat.model
+ import stdlib.themes.bootstrap
+ src/view.opa
+
+hello_chat.model:
+ src/model.opa
View
36 resources/chat.css
@@ -1,36 +0,0 @@
-/***Header***/
-#logo {
- background: url("/resources/opa-logo.png") no-repeat scroll 0 0 transparent;
- height: 32px;
- margin: 10px 0 5px;
- width: 61px;
-}
-
-/***Conversation***/
-#conversation {
- overflow:auto;
- position: absolute;
- margin: auto;
- bottom: 48px;
- top: 50px;
- left: 0;
- right: 0;
-}
-.line {
- border-bottom:1px solid #ddd;
- padding-bottom:8px;
- margin-bottom:8px !important;
-}
-.user, .message {padding-top:8px;}
-.userpic {
- background: url("/resources/user.png") no-repeat 0 0;
- height: 40px;
- width:40px;
-}
-.user {color: #000;font-weight:bold;}
-.message{color:#666;}
-
-/***Footer***/
-#footer {
- background:#eee;
-}
View
45 resources/css/style.css
@@ -0,0 +1,45 @@
+#logo {
+ background: url("/resources/img/opa-logo.png") no-repeat scroll 0 0 transparent;
+ height: 32px;
+ margin: 10px 0 5px;
+ width: 61px;
+}
+
+#conversation {
+ overflow: auto;
+ position: absolute;
+ margin: auto;
+ bottom: 48px;
+ top: 50px;
+ left: 0;
+ right: 0;
+}
+
+.line {
+ border-bottom: 1px solid #ddd;
+ padding-bottom: 8px;
+ margin-bottom: 8px !important;
+}
+
+.user, .message {
+ padding-top: 8px;
+}
+
+.userpic {
+ background: url("/resources/img/user.png") no-repeat 0 0;
+ height: 40px;
+ width: 40px;
+}
+
+.user {
+ color: #000;
+ font-weight: bold;
+}
+
+.message{
+ color:#666;
+}
+
+footer {
+ background:#eee;
+}
View
0  resources/opa-logo.png → resources/img/opa-logo.png
File renamed without changes
View
0  resources/user.png → resources/img/user.png
File renamed without changes
View
23 src/controller.opa
@@ -0,0 +1,23 @@
+module Controller {
+
+ // URL dispatcher of your application; add URL handling as needed
+ dispatcher = {
+ parser {
+ case (.*) : View.default_page()
+ }
+ }
+
+}
+
+resources = @static_resource_directory("resources")
+
+Server.start(Server.http, [
+ { register:
+ [ { doctype: { html5 } },
+ { js: [ ] },
+ { css: [ "/resources/css/style.css"] }
+ ]
+ },
+ { ~resources },
+ { custom: Controller.dispatcher }
+])
View
27 src/model.opa
@@ -0,0 +1,27 @@
+/**
+ * The type of messages sent by a client to the chatroom
+ */
+type message = { string author /**The name of the author (arbitrary string)*/
+ , string text /**Content entered by the user*/
+ }
+
+module Model {
+
+ /**
+ * The chatroom.
+ */
+ private Network.network(message) room = Network.cloud("room")
+
+ exposed function broadcast(message) {
+ Network.broadcast(message, room);
+ }
+
+ function register_message_callback(callback) {
+ Network.add_callback(callback, room);
+ }
+
+ function new_author() {
+ Random.string(8);
+ }
+
+}
View
52 src/view.opa
@@ -0,0 +1,52 @@
+module View {
+
+ function page_template(content) {
+ <>
+ <div class="navbar navbar-fixed-top">
+ <div class=navbar-inner>
+ <div class=container>
+ <div id=#logo />
+ </>
+ </>
+ </>
+ {content}
+ </>
+ }
+
+ function user_update(message msg) {
+ line = <div class="row line">
+ <div class="span1 columns userpic" />
+ <div class="span2 columns user">{msg.author}:</>
+ <div class="span13 columns message">{msg.text}</>
+ </>;
+ #conversation =+ line;
+ Dom.scroll_to_bottom(#conversation);
+ }
+
+ function broadcast(author) {
+ text = Dom.get_value(#entry);
+ Model.broadcast(~{author, text});
+ Dom.clear_value(#entry);
+ }
+
+ function chat_html(author) {
+ <div id=#conversation class=container-fluid
+ onready={function(_) { Model.register_message_callback(user_update)}} />
+ <footer class="navbar navbar-fixed-bottom">
+ <div class=container>
+ <div class=input-append>
+ <input id=#entry class=input-xlarge type=text
+ onnewline={function(_) { broadcast(author) }}>
+ <button class="btn btn-primary" type=button
+ onclick={function(_) { broadcast(author) }}>Post</>
+ </>
+ </>
+ </>
+ }
+
+ function default_page() {
+ author = Model.new_author();
+ Resource.page("Opa chat", page_template(chat_html(author)));
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.