summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2017-04-16 16:37:33 +0300
committerLars Wirzenius <liw@liw.fi>2017-04-16 16:45:12 +0300
commitd1b1cf62e613390845e9f97f8cb38d1af20618ef (patch)
tree5d78dbdb93eed3824d79bf4cd4002f19a34dd110
parenta9be3b3889f794e70b2fbfa0741a41e16c877488 (diff)
downloaddistixapi-d1b1cf62e613390845e9f97f8cb38d1af20618ef.tar.gz
Turn backend into a cliapp application
This will make it easier to use from a systemd unit.
-rwxr-xr-xdistix-backend74
-rw-r--r--yarns/900.yarn5
2 files changed, 54 insertions, 25 deletions
diff --git a/distix-backend b/distix-backend
index ea621c0..3172a5e 100755
--- a/distix-backend
+++ b/distix-backend
@@ -7,6 +7,7 @@ import sys
import yaml
import bottle
+import cliapp
import distixapi
@@ -16,10 +17,13 @@ class AuthenticationPlugin(object):
name = 'AuthenticationPlugin'
+ def __init__(self, users):
+ self._users = users
+
def apply(self, callback, route):
def authorize(*args, **kwargs):
try:
- scopes = distixapi.get_scopes(users, bottle.request)
+ scopes = distixapi.get_scopes(self._users, bottle.request)
except distixapi.AuthenticationError:
return bottle.abort(401, 'Unauthorized')
if route['method'].lower() not in scopes:
@@ -28,39 +32,61 @@ class AuthenticationPlugin(object):
return authorize
-@bottle.route('/')
-def root():
- return 'This is the root'
+class API(object):
+
+ def __init__(self, users):
+ self.app = bottle.Bottle()
+ self.app.install(AuthenticationPlugin(users))
+ self.app.route('/version', method='GET', callback=self.version)
-@bottle.route('/version')
-def version():
- return { 'version': '1.0' }
+ def run(self, port):
+ self.app.run(port=port)
+ def version(self):
+ return { 'version': '1.0' }
-# Command line args.
-pid_file = sys.argv[1]
-port_file = sys.argv[2]
-users_file = sys.argv[3]
+class DistixBackend(cliapp.Application):
+ def add_settings(self):
+ self.settings.string(
+ ['port-file'],
+ 'pick random ports, write to FILE',
+ metavar='FILE')
+ self.settings.string(
+ ['pid-file'],
+ 'write pid to FILE',
+ metavar='FILE')
-log_file = open('log', 'a')
-def log(msg):
- log_file.write('{} {}\n'.format(os.getpid(), msg))
- log_file.flush()
+ self.settings.string(
+ ['users-file'],
+ 'read users list from FILE',
+ metavar='FILE')
+ def process_args(self, args):
+ users = self.read_user_file()
+ port = self.pick_random_port()
+ self.write_pid_file()
+ api = API(users)
+ api.run(port)
-# Write pid to named file.
+ def read_user_file(self):
+ filename = self.settings['users-file']
+ if os.path.exists(filename):
+ with open(filename) as f:
+ return yaml.safe_load(f)
-with open(pid_file, 'w') as f:
- f.write('{}\n'.format(os.getpid()))
+ def pick_random_port(self):
+ port = random.randint(1025, 32767)
+ with open(self.settings['port-file'], 'w') as f:
+ f.write('{}\n'.format(port))
+ return port
+ def write_pid_file(self):
+ filename = self.settings['pid-file']
+ with open(filename, 'w') as f:
+ f.write('{}\n'.format(os.getpid()))
-# Pick a random port and write it to named file.
-port = random.randint(1025, 32767)
-with open(port_file, 'w') as f:
- f.write('{}\n'.format(port))
-users = yaml.safe_load(open(users_file))
-bottle.run(port=port, quiet=True, plugins=[AuthenticationPlugin()])
+DistixBackend(version=distixapi.__version__).run()
diff --git a/yarns/900.yarn b/yarns/900.yarn
index 5d11865..a41a558 100644
--- a/yarns/900.yarn
+++ b/yarns/900.yarn
@@ -18,7 +18,10 @@
IMPLEMENTS GIVEN a running backend instance
backend = os.path.join(srcdir, 'distix-backend')
cliapp.runcmd(
- ['/usr/sbin/daemonize', '-c.', backend, 'pid', 'port', 'users.yaml'])
+ ['/usr/sbin/daemonize', '-c', datadir,
+ backend, '--pid-file', 'pid', '--port-file', 'port',
+ '--log', 'distix-backend.log',
+ '--users-file', 'users.yaml'])
vars['pid'] = cat('pid').strip()
vars['port'] = cat('port').strip()