]>
git.frykholm.com Git - friends.git/blob - friends/server.py
750ddc826a34d032fae38a1a3d6dee75fbdde0e1
4 import tornado
.httpserver
8 from rd
import RD
, Link
10 #insert into user (name,email) values('mikael','mikael@frykholm.com');
11 #insert into entry (userid,text) values (1,'My thoughts on ostatus');
12 import tornado
.options
15 "static_path": os
.path
.join(os
.path
.dirname(__file__
), "static"),
16 "cookie_secret": "__TODO:_GENERATE_YOUR_OWN_RANDOM_VALUE_HERE__",
17 "login_url": "/login",
18 "xsrf_cookies": False,
19 "domain":"https://ronin.frykholm.com",
22 class PushHandler(tornado
.web
.RequestHandler
):
23 #curl -v -k "https://ronin.frykholm.com/hub" -d "hub.callback=a" -d "hub.mode=b" -d "hub.topic=c" -d "hub.verify=d"
25 """ Someone wants to subscribe to hub_topic feed"""
26 hub_callback
= self
.get_argument('hub.callback')
27 hub_mode
= self
.get_argument('hub.mode')
28 hub_topic
= self
.get_argument('hub.topic')
29 hub_verify
= self
.get_argument('hub.verify')
30 hub_lease_seconds
= self
.get_argument('hub.lease_seconds','')
31 hub_secret
= self
.get_argument('hub.sercret','')
32 hub_verify_token
= self
.get_argument('hub.verify_token','')
33 print(self
.request
.body
)
34 if hub_mode
== 'unsubscribe':
36 path
= hub_topic
.split(self
.settings
['domain'])[1]
37 user
= path
.split('user/')[1]
38 row
= db
.execute("select id from user where name=?",(user
,)).fetchone()
40 db
.execute("INSERT into subscriptions (userid, expires, callback, verified) values (?,?,?,?)",(row
['id'],datetime
.datetime
.now(),hub_callback
,False))
43 #TODO add GET callback with the same data we got
44 #TODO store secret, add it to outgoing feeds with hmac
45 class XrdHandler(tornado
.web
.RequestHandler
):
47 self
.render("templates/xrd.xml", hostname
="ronin.frykholm.com", url
=self
.settings
['domain'])
49 class FingerHandler(tornado
.web
.RequestHandler
):
51 user
= self
.get_argument('resource')
52 user
= user
.split('acct:')[1]
53 (user
,domain
) = user
.split('@')
54 rows
= db
.execute("select id from user where user.name=?",(user
,)).fetchone()
57 self
.write("Not found")
60 lnk
= Link(rel
='http://spec.example.net/photo/1.0',
62 href
='{}/static/{}.jpg'.format(self
.settings
['domain'],user
))
63 lnk
.titles
.append(('User Photo', 'en'))
64 lnk
.titles
.append(('Benutzerfoto', 'de'))
65 lnk
.properties
.append(('http://spec.example.net/created/1.0', '1970-01-01'))
66 lnk2
= Link(rel
='http://schemas.google.com/g/2010#updates-from',
67 type='application/atom+xml',
68 href
='{}/user/{}'.format(self
.settings
['domain'],user
))
70 rd
= RD(subject
='{}/{}'.format(self
.settings
['domain'],user
))
71 rd
.properties
.append('http://spec.example.net/type/person')
74 self
.write(rd
.to_json())
75 #self.render("templates/user.xml", user=user)
77 class UserHandler(tornado
.web
.RequestHandler
):
79 entries
= db
.execute("select entry.id,text,ts from user,entry where user.id=entry.userid and user.name=?",(user
,))
80 #import pdb;pdb.set_trace()
81 self
.set_header("Content-Type", 'application/atom+xml')
82 self
.render("templates/feed.xml",
84 feed_url
="{}/user/{}".format(self
.settings
['domain'], user
),
85 hub_url
="{}/hub".format(self
.settings
['domain']),
89 application
= tornado
.web
.Application([
90 (r
"/.well-known/host-meta", XrdHandler
),
91 (r
"/.well-known/webfinger", FingerHandler
),
92 (r
"/user/(.+)", UserHandler
),
93 (r
"/hub", PushHandler
),
94 ],debug
=True,**settings
)
95 srv
= tornado
.httpserver
.HTTPServer(application
, ssl_options
={
96 "certfile": "ronin.frykholm.com.pem",
97 "keyfile": "ronin.frykholm.com.key",
100 print("No db found, creating in {}".format(path
))
101 con
= sqlite3
.connect(path
)
102 con
.execute(""" create table user (id integer primary key,
105 create table entry (id integer primary key,
108 ts timestamp default current_timestamp,
109 FOREIGN KEY(userid) REFERENCES user(id));
110 create table subscriptions (id integer primary key,
115 FOREIGN KEY(userid) REFERENCES user(id));""")
118 if __name__
== "__main__":
119 dbPath
= 'friends.db'
120 tornado
.options
.parse_command_line()
121 if not os
.path
.exists(dbPath
):
123 db
= sqlite3
.connect(dbPath
, detect_types
=sqlite3
.PARSE_DECLTYPES|sqlite3
.PARSE_COLNAMES
)
124 db
.row_factory
= sqlite3
.Row
126 tornado
.ioloop
.IOLoop
.instance().start()