]>
git.frykholm.com Git - friends.git/blob - friends/server.py
ca64779b6cb91abcc1909f810b924685ac12676f
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 if hub_mode
== 'unsubscribe':
35 path
= hub_topic
.split(self
.settings
['domain'])[1]
36 user
= path
.split('user/')[1]
37 row
= db
.execute("select id from user where name=?",(user
,)).fetchone()
39 db
.execute("INSERT into subscriptions (userid, expires, callback, verified) values (?,?,?,?)",(row
['id'],datetime
.datetime
.now(),hub_callback
,False))
42 class XrdHandler(tornado
.web
.RequestHandler
):
44 self
.render("templates/xrd.xml", hostname
="ronin.frykholm.com", url
=self
.settings
['domain'])
46 class FingerHandler(tornado
.web
.RequestHandler
):
48 user
= self
.get_argument('resource')
49 user
= user
.split('acct:')[1]
50 (user
,domain
) = user
.split('@')
51 rows
= db
.execute("select id from user where user.name=?",(user
,)).fetchone()
54 self
.write("Not found")
57 lnk
= Link(rel
='http://spec.example.net/photo/1.0',
59 href
='{}/static/{}.jpg'.format(self
.settings
['domain'],user
))
60 lnk
.titles
.append(('User Photo', 'en'))
61 lnk
.titles
.append(('Benutzerfoto', 'de'))
62 lnk
.properties
.append(('http://spec.example.net/created/1.0', '1970-01-01'))
63 lnk2
= Link(rel
='http://schemas.google.com/g/2010#updates-from',
64 type='application/atom+xml',
65 href
='{}/user/{}'.format(self
.settings
['domain'],user
))
67 rd
= RD(subject
='{}/{}'.format(self
.settings
['domain'],user
))
68 rd
.properties
.append('http://spec.example.net/type/person')
71 self
.write(rd
.to_json())
72 #self.render("templates/user.xml", user=user)
74 class UserHandler(tornado
.web
.RequestHandler
):
76 entries
= db
.execute("select entry.id,text,ts from user,entry where user.id=entry.userid and user.name=?",(user
,))
77 #import pdb;pdb.set_trace()
78 self
.set_header("Content-Type", 'application/atom+xml')
79 self
.render("templates/feed.xml",
81 feed_url
="{}/user/{}".format(self
.settings
['domain'], user
),
82 hub_url
="{}/hub".format(self
.settings
['domain']),
86 application
= tornado
.web
.Application([
87 (r
"/.well-known/host-meta", XrdHandler
),
88 (r
"/.well-known/webfinger", FingerHandler
),
89 (r
"/user/(.+)", UserHandler
),
90 (r
"/hub", PushHandler
),
91 ],debug
=True,**settings
)
92 srv
= tornado
.httpserver
.HTTPServer(application
, ssl_options
={
93 "certfile": "ronin.frykholm.com.pem",
94 "keyfile": "ronin.frykholm.com.key",
97 print("No db found, creating in {}".format(path
))
98 con
= sqlite3
.connect(path
)
99 con
.execute(""" create table user (id integer primary key,
102 create table entry (id integer primary key,
105 ts timestamp default current_timestamp,
106 FOREIGN KEY(userid) REFERENCES user(id));
107 create table subscriptions (id integer primary key,
112 FOREIGN KEY(userid) REFERENCES user(id));""")
115 if __name__
== "__main__":
116 dbPath
= 'friends.db'
117 tornado
.options
.parse_command_line()
118 if not os
.path
.exists(dbPath
):
120 db
= sqlite3
.connect(dbPath
, detect_types
=sqlite3
.PARSE_DECLTYPES|sqlite3
.PARSE_COLNAMES
)
121 db
.row_factory
= sqlite3
.Row
123 tornado
.ioloop
.IOLoop
.instance().start()