Moving to Sanic from Pyramid.

Views working as of now. Need to work on Authentication

Signed-off-by: tanshu <tanshu@gmail.com>
This commit is contained in:
tanshu 2020-04-28 16:33:25 +05:30
parent 2d7414f02b
commit c3e7151aef
10 changed files with 70 additions and 179 deletions

3
.gitignore vendored
View File

@ -4,4 +4,5 @@ env
.pydevproject .pydevproject
*/__pycache__/ */__pycache__/
.idea/ .idea/
*.egg-info/ *.egg-info/
.htpasswd

View File

@ -1,17 +1,5 @@
from crypt import crypt from crypt import crypt
import os import os
from pyramid.authentication import BasicAuthAuthenticationPolicy
from pyramid.authorization import ACLAuthorizationPolicy
from pyramid.config import Configurator
from pyramid.security import Authenticated, Allow, Everyone
def main(global_config, **settings):
config = Configurator(settings=settings, authentication_policy=BasicAuthAuthenticationPolicy(htpasswd),
authorization_policy=ACLAuthorizationPolicy(), root_factory='bifrost.RootFactory')
config.add_route('update', '/update')
config.scan()
return config.make_wsgi_app()
def htpasswd(username, password, request): def htpasswd(username, password, request):
@ -27,13 +15,3 @@ def htpasswd(username, password, request):
if username in users and crypt(password, users[username]) == users[username]: if username in users and crypt(password, users[username]) == users[username]:
return [Authenticated] return [Authenticated]
return None return None
class RootFactory(object):
@property
def __acl__(self):
acl = [(Allow, Authenticated, Authenticated)]
return acl
def __init__(self, request):
pass

3
bifrost/__main__.py Normal file
View File

@ -0,0 +1,3 @@
from bifrost.main import init
init()

25
bifrost/main.py Normal file
View File

@ -0,0 +1,25 @@
from sanic import Sanic
from environs import Env
from bifrost.settings import Settings
app = Sanic(__name__, load_env='BIFROST_')
def init():
env = Env()
env.read_env()
app.config.from_object(Settings)
from bifrost.views import update_view
app.add_route(update_view, '/update', methods=['GET'])
app.run(host='0.0.0.0', port=8000, debug=True, access_log=True)
app.run(
host=app.config.HOST,
port=app.config.PORT,
debug=app.config.DEBUG,
access_log=app.config.ACCESS_LOG
)

9
bifrost/settings.py Normal file
View File

@ -0,0 +1,9 @@
from sanic_envconfig import EnvConfig
class Settings(EnvConfig):
DEBUG: bool = True
HOST: str = '0.0.0.0'
PORT: int = 8000
API_URL_BASE: str = 'https://api.digitalocean.com/v2'
API_TOKEN: str = None

View File

@ -1,31 +1,28 @@
import logging from sanic.log import logger
from sanic.response import json
from pyramid.httpexceptions import HTTPUnauthorized
from pyramid.security import forget, Authenticated
from pyramid.view import view_config, forbidden_view_config
from bifrost.digital_ocean import update_domain from bifrost.digital_ocean import update_domain
log = logging.getLogger('bifrost.updater')
api_url_base = 'https://api.digitalocean.com/v2'
api_token = None
async def update_view(request):
@view_config(route_name='update', request_param='domain', renderer='json', permission=Authenticated) logger.info('Here is your log')
def update_view(request): if 'domain' not in request.args or len(request.args['domain']) != 1:
current_ip = request.GET.get('ip', None) return json({"message": "Domain error"})
if current_ip is None: domain = request.args['domain'][0]
if 'ip' in request.args and len(request.args['ip']) == 1:
current_ip = request.args['ip'][0]
else:
if 'X-Forwarded-For' in request.headers: if 'X-Forwarded-For' in request.headers:
current_ip = request.headers['X-Forwarded-For'] current_ip = request.headers['X-Forwarded-For']
else: else:
current_ip = request.remote_addr current_ip = request.ip
name, domain = domain.split('.', maxsplit=1)
name, domain = request.GET['domain'].split('.', maxsplit=1) update_domain(domain, name, current_ip, request.app.config.API_URL_BASE, request.app.config.API_TOKEN, logger)
update_domain(domain, name, current_ip, api_url_base, api_token, log) return json({"domain": domain, "name": name, "current_ip": current_ip, "api_base": request.app.config.API_URL_BASE, "api_token": request.app.config.API_TOKEN})
return {}
@forbidden_view_config() # @forbidden_view_config()
def basic_challenge(request): # def basic_challenge(request):
response = HTTPUnauthorized() # response = HTTPUnauthorized()
response.headers.update(forget(request)) # response.headers.update(forget(request))
return response # return response

View File

@ -1,63 +0,0 @@
###
# app configuration
# http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/environment.html
###
[app:main]
use = egg:bifrost
pyramid.reload_templates = false
pyramid.debug_authorization = false
pyramid.debug_notfound = false
pyramid.debug_routematch = false
pyramid.default_locale_name = en
biforst.file = %(here)s/dns_info
biforst.auth = %(here)s/.htpasswd
webfaction.username =
webfaction.password =
###
# wsgi server configuration
###
[server:main]
use = egg:waitress#main
host = 0.0.0.0
port = 6543
###
# logging configuration
# http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/logging.html
###
[loggers]
keys = root, bifrost, bifrost.updater
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = INFO
handlers = console
[logger_bifrost]
level = DEBUG
handlers =
qualname = bifrost
[logger_bifrost.updater]
level = INFO
handlers =
qualname = bifrost.updater
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s

View File

@ -1,63 +0,0 @@
###
# app configuration
# http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/environment.html
###
[app:main]
use = egg:bifrost
pyramid.reload_templates = false
pyramid.debug_authorization = false
pyramid.debug_notfound = false
pyramid.debug_routematch = false
pyramid.default_locale_name = en
biforst.file = %(here)s/dns_info
biforst.auth = %(here)s/.htpasswd
webfaction.username =
webfaction.password =
###
# wsgi server configuration
###
[server:main]
use = egg:waitress#main
host = 0.0.0.0
port = 6543
###
# logging configuration
# http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/logging.html
###
[loggers]
keys = root, bifrost, bifrost.updater
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARN
handlers = console
[logger_bifrost]
level = WARN
handlers =
qualname = bifrost
[logger_bifrost.updater]
level = INFO
handlers =
qualname = bifrost.updater
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s

View File

@ -1,3 +1,5 @@
pyramid wheel
waitress sanic
requests secure
environs
sanic-envconfig

View File

@ -7,20 +7,22 @@ README = open(os.path.join(here, 'README.txt')).read()
CHANGES = open(os.path.join(here, 'CHANGES.txt')).read() CHANGES = open(os.path.join(here, 'CHANGES.txt')).read()
requires = [ requires = [
'pyramid', 'wheel',
'waitress', 'sanic',
'requests' 'secure',
'environs',
'sanic-envconfig'
] ]
setup(name='bifrost', setup(name='bifrost',
version='1.0', version='2.0',
description='bifrost', description='bifrost',
long_description=README + '\n\n' + CHANGES, long_description=README + '\n\n' + CHANGES,
classifiers=[ classifiers=[
"Programming Language :: Python", "Programming Language :: Python",
"Framework :: Pyramid", "Framework :: Pyramid",
"Topic :: Internet :: WWW/HTTP", "Topic :: Internet :: WWW/HTTP",
"Topic :: Internet :: WWW/HTTP :: WSGI :: Application", "Topic :: Internet :: WWW/HTTP :: ASGI :: Application",
], ],
author='tanshu', author='tanshu',
author_email='programming@tanshu.com', author_email='programming@tanshu.com',