Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Code ported to mongodb backend #10

Merged
merged 7 commits into from

2 participants

Cédric Soulas Tristan Sloughter
Commits on Mar 6, 2012
  1. Cédric Soulas
Commits on Mar 7, 2012
  1. Cédric Soulas
  2. Cédric Soulas
  3. Cédric Soulas
Commits on Mar 11, 2012
  1. README updated

    Cedric Soulas authored
  2. Code updated for Opa 9.0.1

    Cedric Soulas authored
  3. README format updated

    Cedric Soulas authored
This page is out of date. Refresh to see the latest.
4 Makefile
View
@@ -1,11 +1,11 @@
NAME = main.exe
-SRC = src/todo.opa src/user.opa src/admin.opa src/main.opa src/ui.opa
+SRC = src/type.opa src/todo.opa src/user.opa src/admin.opa src/main.opa src/ui.opa
all: $(NAME)
$(NAME): $(SRC)
- opa --parser js-like $(SRC) -o $(NAME)
+ opa --database mongo $(SRC) -o $(NAME)
clean:
rm -f $(NAME)
26 README.md
View
@@ -1,17 +1,17 @@
-## Install Opa: [http://opalang.org](http://opalang.org)
+## Install
-## Compile and Run:
+- Opa 9.0.1: [http://opalang.org](http://opalang.org)
+- Mongodb 2.0: http://mongodb.org
+
+## Compile
+
+```bash
+make
+```
+
+## Run
```bash
-19:05 (master) λ make
-opa --parser js-like src/todo.opa src/user.opa src/admin.opa src/main.opa -o main.exe
-Embedding file "/Users/tristan/Devel/opado/resources/destroy.png" as resource "resources/destroy.png" with mimetype "image/png"
-Embedding file "/Users/tristan/Devel/opado/resources/js/bugherd.js" as resource "resources/js/bugherd.js" with mimetype "application/javascript"
-Embedding file "/Users/tristan/Devel/opado/resources/js/google_analytics.js" as resource "resources/js/google_analytics.js" with mimetype "application/javascript"
-Embedding file "/Users/tristan/Devel/opado/resources/todos.css" as resource "resources/todos.css" with mimetype "text/css"
-[tristan@marx ~/Devel/opado]
-19:05 (master) λ ./main.exe
-Accesses logged to access.log
-Messages logged to error.log
-Http (OPA/1056) serving on http://marx.local:8080
+mongod --dbpath . > log.log &
+./main.exe
```
2  resources/style.css
View
@@ -150,6 +150,8 @@ ul#todo_list .display:hover .icon.icon-remove {display:inline-block;}
.todo_content {padding:0 3px;font-size:18px; line-height:22px;color:#6C6B66;}
+.done .todo_content{text-decoration: line-through;}
+
/* Edit input */
#todo_list .editing .display, #todo_list .edit {display: none;}
#todo_list .editing .edit {display: block;}
22 src/admin.opa
View
@@ -1,16 +1,15 @@
-package opado.admin
-
-import opado.ui
-import opado.todo
-import opado.user
+import stdlib.database.mongo
module Admin {
function add_users() {
- users = /users;
- Map.iter((function(_, y){
- items = /todo_items[y.username];
- add_user_to_page(y.username, y.fullname, y.is_oauth, Map.size(items))
- }), users)
+ dbset(User.t, _) users = /opado/users;
+ it = DbSet.iterator(users);
+ Iter.iter((function(user){
+ useref = user.ref;
+ dbset(Todo.t, _) items = /opado/todos[ useref == useref ];
+ it = DbSet.iterator(items);
+ add_user_to_page(user.username, user.fullname, user.is_oauth, Iter.count(it))
+ }), it)
}
function add_user_to_page(string username, string fullname, bool is_oauth, int size) {
@@ -33,7 +32,8 @@ module Admin {
}
resource =
- (Parser.general_parser((http_request -> 'toto))) parser (.*) -> function(_req){
+ (Parser.general_parser((http_request -> 'toto))) parser { (.*) : function(_req){
mypage("Admin", admin())
}
+ }
}
25 src/main.opa
View
@@ -1,22 +1,17 @@
-package opado.main
-
-import opado.user
-import opado.admin
-import opado.todo
-
function with_request(f){
f(ThreadContext.get({current}).request ? error("no request"))
}
-urls = parser
- {Rule.debug_parse_string((function(s){Log.notice("URL", s)}))}
- Rule.fail -> error("")
- | "/todos?" result={Todo.resource} -> with_request(result)
- | "/connect?" data=(.*) -> User.connect(Text.to_string(data))
- | "/user" result={User.resource} -> with_request(result)
- | "/login" result={User.resource} -> with_request(result)
- | "/admin" result={Admin.resource} -> with_request(result)
- | (.*) result={Todo.resource} -> with_request(result)
+urls = parser {
+ //Rule.debug_parse_string((function(s){Log.notice("URL", s)}))
+ //Rule.fail : error("Error")
+ | "/todos?" result={Todo.resource} : with_request(result)
+ | "/connect?" data=(.*) : User.connect(Text.to_string(data))
+ | "/user" result={User.resource} : with_request(result)
+ | "/login" result={User.resource} : with_request(result)
+ | "/admin" result={Admin.resource} : with_request(result)
+ | (.*) result={Todo.resource} : with_request(result)
+ }
Server.start(Server.http,
[{resources:@static_resource_directory("resources")},
59 src/todo.opa
View
@@ -1,18 +1,5 @@
-package opado.todo
-
-import opado.user
-import opado.ui
import stdlib.web.client
-type todo_item = {
- string value,
- bool done,
- string created_at
-}
-
-database stringmap(stringmap(todo_item)) /todo_items
-database /todo_items[_][_]/done = false
-
module Todo {
function update_counts() {
num_done = Dom.length(Dom.select_class("done"));
@@ -32,11 +19,8 @@ module Todo {
}
exposed @async function db_make_done(string id) {
- username = User.get_username();
- items = /todo_items[username];
- item = Option.get(StringMap.get(id, items));
- @/todo_items[username] <-
- StringMap.add(id, {item with done : true}, items)
+ useref = User.get_username();
+ /opado/todos[~{ id }] <- { done : true };
}
function remove_item(string id) {
@@ -46,9 +30,9 @@ module Todo {
}
exposed @async function db_remove_item(string id) {
- username = User.get_username();
- items = /todo_items[username];
- @/todo_items[username] <- StringMap.remove(id, items)
+ useref = User.get_username();
+ Db.remove(@/opado/todos[~{ id }]);
+ void
}
@async function remove_all_done() {
@@ -62,17 +46,16 @@ module Todo {
add_todo_to_page(id, x, false)
}
- exposed @async function db_add_todo(string id, string x) {
- username = User.get_username();
- items = /todo_items[username];
- @/todo_items[username] <-
- StringMap.add(id, { value : x, done : false, created_at : "" }, items)
+ exposed @async function db_add_todo(string id, string value) {
+ useref = User.get_username();
+ /opado/todos[~{ id }] <- { id : id, useref : useref, value : value } // not necessary to specify default values
}
exposed function add_todos() {
- username = User.get_username();
- items = /todo_items[username];
- StringMap.iter((function(x,y){add_todo_to_page(x, y.value, y.done)}), items)
+ useref = User.get_username();
+ dbset(Todo.t, _) items = /opado/todos[ useref == useref];
+ it = DbSet.iterator(items);
+ Iter.iter((function(item){add_todo_to_page(item.id, item.value, item.done)}), it)
}
function update_todo(string id, string value) {
@@ -95,12 +78,18 @@ module Todo {
}
function add_todo_to_page(string id, string value, bool is_done) {
+ done = if (is_done) "done" else ""
+ checkbox = if (is_done) {
+ <input checked="yes" class="check" type="checkbox" onclick={function(_){make_done(id)}}/>
+ }else{
+ <input class="check" type="checkbox" onclick={function(_){make_done(id)}}/>
+ }
line =
- <li><div class="todo {if (is_done) "done" else ""}" id={ id }>
+ <li><div class="todo {done}" id={ id }>
<div class="display">
<span id={id^"_destroy"} class="todo_destroy icon icon-remove" onclick={function(_){remove_item(id)}}></span>
- <input class="check" type="checkbox" onclick={function(_){make_done(id)}}/>
- <div id={id^"_todo"} class="todo_content" onclick={function(_){make_editable(id, value)}}>{ value }</div>
+ {checkbox}
+ <div id={id^"_todo"} class="todo_content" onclick={function(_){ if (is_done) { }else{ make_editable(id, value) }}}>{ value }</div>
</div>
</div></li>
Dom.transform([#todo_list =+ line]);
@@ -154,6 +143,8 @@ module Todo {
}
resource =
- (Parser.general_parser((http_request -> resource))) parser
- (.*) -> function(_req){todos()}
+ (Parser.general_parser((http_request -> resource))) parser {
+ (.*) : function(_req){todos()}
+ }
+
}
34 src/type.opa
View
@@ -0,0 +1,34 @@
+type User.password = string
+type User.ref = string
+
+type User.t = {
+ User.ref ref,
+ string username,
+ string fullname,
+ User.password password,
+ bool is_oauth,
+}
+
+type User.status = {User.ref logged} or {unlogged}
+
+type User.info = UserContext.t(User.status)
+type User.map('a) = ordered_map(User.ref, 'a, String.order)
+
+type Todo.id = string
+
+type Todo.t = {
+ Todo.id id,
+ string useref,
+ string value,
+ bool done,
+ string created_at
+}
+
+database opado @mongo {
+ User.t /users[{ref}]
+ /users[_]/is_oauth = false
+ Todo.t /todos[{id}]
+ /todos[_]/done = false
+ // Default value for string is "" (empty string)
+}
+
2  src/ui.opa
View
@@ -2,8 +2,6 @@
* @author Ida Swarczewskaja
**/
-package opado.ui
-
import stdlib.themes.bootstrap.core
/*module Desktop {
73 src/user.opa
View
@@ -1,22 +1,25 @@
-/*
+/*:54
* USER.OPA
*
* @author Tristan Sloughter
* @author Matthieu Guffroy
**/
-package opado.user
-
import stdlib.crypto
import stdlib.web.client
import stdlib.core.web.core
import stdlib.widgets.core
import stdlib.widgets.loginbox
import stdlib.widgets.formbuilder
-import stdlib.{themes.bootstrap, widgets.bootstrap}
+import stdlib.{widgets.bootstrap}
import stdlib.apis.{facebook, facebook.auth, facebook.graph}
-import opado.ui
+/**
+database stringmap(stringmap(todo_item)) /todo_items
+database /todo_items[_][_]/done = false
+database User.map(User.t) /users
+database /users[_]/is_oauth = false
+*/
// DATA
@@ -32,40 +35,23 @@ FBA = FbAuth(OpaIntro1.config)
FBG = FbGraph
redirect = "http://opado.org/connect"
-@abstract type User.password = string
-@abstract type User.ref = string
-
-type User.t = { string username
- , string fullname
- , User.password password
- , bool is_oauth
- }
-
-type User.status = {User.ref logged} or {unlogged}
-
-type User.info = UserContext.t(User.status)
-type User.map('a) = ordered_map(User.ref, 'a, String.order)
-
-database User.map(User.t) /users
-database /users[_]/is_oauth = false
-
-
module User_data {
function User.ref mk_ref(string login){
String.to_lower(login)
}
- function string ref_to_string(User.ref login){
- login
+ function string ref_to_string(User.ref ref){
+ ref
}
- function void save(User.ref ref,User.t user){
- @/users[ref] <- user
+ function void save(User.t user){
+ /opado/users[{ref : user.ref}] <- user
}
function option(User.t) get(User.ref ref){
- ?/users[ref]
+ ?/opado/users[~{ref}]
}
+
}
module User {
@@ -77,21 +63,17 @@ module User {
}
function user_create(username, password, is_oauth) {
- useref = User_data.mk_ref(username);
- user = User_data.get(useref);
+ ref = User_data.mk_ref(username);
+ user = User_data.get(ref);
match (user) {
- case { none }:
- user =
- (User.t) { username: useref
- , fullname : ""
- , password : Crypto.Hash.sha2(password)
- , is_oauth : is_oauth };
- @/users[username] <- user
+ case { none }: void
+ /opado/users[~{ ref }] <- { ref : ref, username: ref, password : Crypto.Hash.md5(password), is_oauth : is_oauth}; // not necessary to specify default values
default: void
}
}
+
function get_status() {
UserContext.execute((function(a){a}), state)
}
@@ -108,7 +90,7 @@ module User {
user = User_data.get(useref);
match (user) {
case { some : u }:
- if (u.is_oauth == false && u.password == Crypto.Hash.sha2(password)) {
+ if (u.is_oauth == false && u.password == Crypto.Hash.md5(password)) {
UserContext.change(function(_){
{ logged :User_data.mk_ref(login) }
},state)
@@ -224,11 +206,11 @@ module User {
<p>
Username : <input id=#{username_id}
onchange={function(_){
- User_data.save(r, {user with username : Dom.get_value(#{username_id})})
+ User_data.save({user with ref : r, username : Dom.get_value(#{username_id})})
}}
value={user.username} /><br />
Fullname : <input id=#{fullname_id}
- onchange={function(_){ User_data.save(r, {user with fullname : Dom.get_value(#{fullname_id})})
+ onchange={function(_){ User_data.save({user with ref : r, fullname : Dom.get_value(#{fullname_id})})
}}
value={user.fullname} />
</p>
@@ -354,10 +336,11 @@ module User {
}
resource =
- (Parser.general_parser((http_request -> resource))) parser
- | "/new" -> function(_req) { mypage("New User",new()) }
- | "/edit" -> function(_req) { edit() }
- | "/view/" login = (.*) -> function(_req) { view(Text.to_string(login)) }
- | .* -> function(_req) { start() }
+ (Parser.general_parser((http_request -> resource))) parser {
+ | "/new" : function(_req) { mypage("New User",new()) }
+ | "/edit" : function(_req) { edit() }
+ | "/view/" login = (.*) : function(_req) { view(Text.to_string(login)) }
+ | .* : function(_req) { start() }
+ }
}
Something went wrong with that request. Please try again.