Skip to content

Commit

Permalink
Made Sanic server starting optional.
Browse files Browse the repository at this point in the history
  • Loading branch information
natfarleydev committed Jan 20, 2017
1 parent 3a716c5 commit 1349008
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
13 changes: 10 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
include_callback_query_chat_id)

from skybeard.beards import Beard, BeardChatHandler, SlashCommand
from skybeard import server
from skybeard.help import create_help
import config

Expand Down Expand Up @@ -68,6 +67,10 @@ def delegator_beard_gen(beards):


def main(config):

if pyconfig.get('start_server'):
from skybeard import server

for beard_path in config.beard_paths:
sys.path.insert(0, get_literal_path(beard_path))

Expand Down Expand Up @@ -103,10 +106,11 @@ def main(config):
list(delegator_beard_gen(Beard.beards))
)

if pyconfig.get('start_server'):
asyncio.ensure_future(server.start())

loop = asyncio.get_event_loop()
# TODO DOES NOT WORK
loop.create_task(bot.message_loop())
asyncio.ensure_future(server.start())
print('Listening ...')

loop.run_forever()
Expand All @@ -120,9 +124,12 @@ def main(config):
parser.add_argument('--no-help', action='store_true')
parser.add_argument('-d', '--debug', action='store_const', dest="loglevel",
const=logging.DEBUG, default=logging.INFO)
parser.add_argument('--start-server', action='store_const', const=True, default=False)

parsed = parser.parse_args()

pyconfig.set('start_server', parsed.start_server)

logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=parsed.loglevel)
Expand Down
12 changes: 9 additions & 3 deletions skybeard/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
try:
from sanic_cors import CORS
except ImportError:
assert False, "Missing dependency (sanic_cors). To install: pip install https://github.com/ashleysommer/sanic-cors/"
logging.warning(
"Missing dependency (sanic_cors). To install: "
"pip install https://github.com/ashleysommer/sanic-cors/")

from skybeard.beards import Beard

Expand All @@ -21,8 +23,12 @@

app = Sanic(__name__)

# CORS: https://en.wikipedia.org/wiki/Cross-origin_resource_sharing
CORS(app)
try:
# CORS: https://en.wikipedia.org/wiki/Cross-origin_resource_sharing
CORS(app)
except NameError:
logger.warning("sanic_cors.CORS could not be found. Perhaps missing dependency?")

key_blueprint = Blueprint('key', url_prefix='/key[A-z]+')


Expand Down

0 comments on commit 1349008

Please sign in to comment.