annotate mod_couchdb/couchdb/couchdb.lib.lua @ 2894:165d2877eeac

mod_firewall: Add experimental user-centric persistent marks behind a feature flag
author Kim Alvefur <zash@zash.se>
date Sat, 24 Feb 2018 21:40:56 +0100
parents 316d7c8e1fb0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
204
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 local http = require "socket.http";
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 local url = require "socket.url";
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 local couchapi = module:require("couchdb/couchapi");
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 local json = module:require("couchdb/json");
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8 local couchdb_url = assert(module:get_option("couchdb_url"), "Option couchdb_url not specified");
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 local db = couchapi.db(couchdb_url);
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 local function couchdb_get(key)
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 local a,b = db:doc(key):get()
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 print(json.encode(a));
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 if b == 404 then return nil; end
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 if b == 200 then b = nil; end
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 return a.payload,b;
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 end
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 local function couchdb_put(key, value)
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 local a,b = db:doc(key):get();
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 return db:doc(key):put({ payload = value, _rev = a and a._rev });
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 end
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 local st = require "util.stanza";
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 local handlers = {};
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 handlers.accounts = {
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 get = function(self, user)
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 return couchdb_get(self.host.."/"..user.."/account");
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 end;
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 set = function(self, user, data)
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 return couchdb_put(self.host.."/"..user.."/account", data);
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 end;
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 };
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 handlers.vcard = {
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 get = function(self, user)
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 return couchdb_get(self.host.."/"..user.."/vcard");
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 end;
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 set = function(self, user, data)
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 return couchdb_put(self.host.."/"..user.."/vcard", data);
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 end;
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 };
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 handlers.private = {
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 get = function(self, user)
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 return couchdb_get(self.host.."/"..user.."/private");
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 end;
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 set = function(self, user, data)
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 return couchdb_put(self.host.."/"..user.."/private", data);
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 end;
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 };
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 handlers.roster = {
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 get = function(self, user)
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 return couchdb_get(self.host.."/"..user.."/roster");
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 end;
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 set = function(self, user, data)
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 return couchdb_put(self.host.."/"..user.."/roster", data);
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 end;
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 };
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 -----------------------------
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 local driver = {};
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 driver.__index = driver;
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 function driver:open(host, datastore, typ)
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 local cache_key = host.." "..datastore;
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 if self.ds_cache[cache_key] then return self.ds_cache[cache_key]; end
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 local instance = setmetatable({}, self);
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 instance.host = host;
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 instance.datastore = datastore;
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 local handler = handlers[datastore];
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 if not handler then return nil; end
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 for key,val in pairs(handler) do
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 instance[key] = val;
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 end
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 if instance.init then instance:init(); end
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77 self.ds_cache[cache_key] = instance;
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 return instance;
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 end
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81 -----------------------------
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82 local _M = {};
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
83
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84 function _M.new()
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
85 local instance = setmetatable({}, driver);
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 instance.__index = instance;
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
87 instance.ds_cache = {};
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88 return instance;
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 end
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90
316d7c8e1fb0 mod_couchdb: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 return _M;