diff --git a/Makefile b/Makefile index 9c9fdd5553ad8210824832662fd1c715ba53eb85..78981207f035d362e4553d75ec541341d8fac1cf 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,10 @@ install: pip install -e . run: - SECRET_KEY=local FLASK_DEBUG=1 FLASK_APP=./openlobby/server.py flask run -p 8010 + DEBUG=1 python manage.py runserver 8010 + +migrate: + DEBUG=1 python manage.py migrate build: docker build -t openlobby/openlobby-server:latest . diff --git a/README.md b/README.md index 7bd58cfcf606dd5f74ff130a4af2234c97e9de56..cd0bddfd4bd26efcae83ea55387bab206fa5eaf4 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,9 @@ prepared Elasticsearch Docker container with Czech support at ## Configuration Configuration is done by environment variables: - - `SECRET_KEY` - long random secret string (required) + - `DEBUG` - Set to any value to turn on debug mode. Don't use in production! + - `SECRET_KEY` - long random secret string (required if not in debug mode) + - `DATABASE_DSN` - DSN of PostgreSQL database (default: `postgresql://db:db@localhost:5432/openlobby`) - `ELASTICSEARCH_DSN` - DSN of Elasticsearch cluster (default: `http://localhost:9200`) - `SITE_NAME` - site name for OpenID authentication (default: `Open Lobby`) - `ES_INDEX` - Elasticsearch index (default: `openlobby`) @@ -44,12 +46,13 @@ You need to have Python 3 installed. Clone this repository and run: 1. `make init-env` - prepares Python virtualenv in dir `.env` 2. `source .env/bin/activate` - activates virtualenv 3. `make install` - installs requirements and server in development mode -4. `make run` - runs development server on port `8010` +4. `make migrate` - runs database migrations +5. `make run` - runs development server on port `8010` Now you can use GraphQL API endpoint and GraphiQL web interface at `http://localhost:8010/graphql` -Next time you can just do steps 2 and 4. +Next time you can just do steps 2 and 5. Development server assumes that you have [openlobby/openlobby-es-czech](https://github.com/openlobby/openlobby-es-czech). diff --git a/manage.py b/manage.py new file mode 100755 index 0000000000000000000000000000000000000000..de2d50d9e35c30f66f7de58d9efb1b4f2faa96e0 --- /dev/null +++ b/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +import os +import sys + +if __name__ == "__main__": + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "openlobby.settings") + try: + from django.core.management import execute_from_command_line + except ImportError: + # The above import may fail for some other reason. Ensure that the + # issue is really that Django is missing to avoid masking other + # exceptions on Python 2. + try: + import django + except ImportError: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) + raise + execute_from_command_line(sys.argv) diff --git a/openlobby/core/__init__.py b/openlobby/core/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/openlobby/core/admin.py b/openlobby/core/admin.py new file mode 100644 index 0000000000000000000000000000000000000000..8c38f3f3dad51e4585f3984282c2a4bec5349c1e --- /dev/null +++ b/openlobby/core/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/openlobby/core/apps.py b/openlobby/core/apps.py new file mode 100644 index 0000000000000000000000000000000000000000..26f78a8e673340121f68a92930e2830bc58d269d --- /dev/null +++ b/openlobby/core/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class CoreConfig(AppConfig): + name = 'core' diff --git a/openlobby/core/migrations/__init__.py b/openlobby/core/migrations/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/openlobby/core/models.py b/openlobby/core/models.py new file mode 100644 index 0000000000000000000000000000000000000000..71a836239075aa6e6e4ecb700e9c42c95c022d91 --- /dev/null +++ b/openlobby/core/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/openlobby/core/templates/core/index.html b/openlobby/core/templates/core/index.html new file mode 100644 index 0000000000000000000000000000000000000000..25a2631d8efe95cb3150315a930342f8bed9981a --- /dev/null +++ b/openlobby/core/templates/core/index.html @@ -0,0 +1,6 @@ +<html> + <body> + <h1>Open Lobby Server</h1> + <p>GraphQL API endpoint and GraphiQL is at: <a href="/graphql">/graphql</a></p> + </body> +</html> diff --git a/openlobby/core/tests.py b/openlobby/core/tests.py new file mode 100644 index 0000000000000000000000000000000000000000..7ce503c2dd97ba78597f6ff6e4393132753573f6 --- /dev/null +++ b/openlobby/core/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/openlobby/core/views.py b/openlobby/core/views.py new file mode 100644 index 0000000000000000000000000000000000000000..da70d4976607a0aa2c8e4e87036c6768262c1487 --- /dev/null +++ b/openlobby/core/views.py @@ -0,0 +1,5 @@ +from django.views.generic.base import TemplateView + + +class IndexView(TemplateView): + template_name = 'core/index.html' diff --git a/openlobby/settings.py b/openlobby/settings.py index c1a8b8b7e0653c64d1b4c383bebefb0ef4b65770..3aef4d9c642311fcbf0418fd0244c85c1157d626 100644 --- a/openlobby/settings.py +++ b/openlobby/settings.py @@ -1,4 +1,134 @@ import os +import dsnparse + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = 'DEBUG' in os.environ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = os.environ.get('SECRET_KEY') +if not SECRET_KEY: + if DEBUG: + SECRET_KEY = 'not-secret-at-all' + else: + raise RuntimeError('Missing SECRET_KEY environment variable.') + +ALLOWED_HOSTS = ['*'] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.staticfiles', + 'openlobby.core', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'openlobby.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'openlobby.wsgi.application' + +DATABASE_DSN = os.environ.get('DATABASE_DSN', 'postgresql://db:db@localhost:5432/openlobby') +db = dsnparse.parse(DATABASE_DSN) +assert db.scheme == 'postgresql' + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql', + 'NAME': db.paths[0], + 'USER': db.username, + 'PASSWORD': db.password, + 'HOST': db.host, + 'PORT': db.port, + } +} + + +# Password validation + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization + +LANGUAGE_CODE = 'cs' + +TIME_ZONE = 'Europe/Prague' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) + +STATIC_URL = '/static/' + + +LOGGING = { + 'version': 1, + 'disable_existing_loggers': False, + 'handlers': { + 'console': { + 'class': 'logging.StreamHandler', + }, + }, + 'loggers': { + 'django': { + 'handlers': ['console'], + 'level': 'INFO' if DEBUG else 'WARNING', + }, + }, +} + + +############################################################################### +# Custom settings # Elasticsearch index ES_INDEX = os.environ.get('ES_INDEX', 'openlobby') @@ -6,11 +136,6 @@ ES_INDEX = os.environ.get('ES_INDEX', 'openlobby') # default analyzer for text fields ES_TEXT_ANALYZER = os.environ.get('ES_TEXT_ANALYZER', 'czech') -# secret key for signing tokens -SECRET_KEY = os.environ.get('SECRET_KEY') -if not SECRET_KEY: - raise RuntimeError('Missing SECRET_KEY environment variable.') - # signature algorithm JSON Web Tokens JWT_ALGORITHM = 'HS512' diff --git a/openlobby/urls.py b/openlobby/urls.py new file mode 100644 index 0000000000000000000000000000000000000000..eff7f1a8b059de8ae113054282548b368b7ba494 --- /dev/null +++ b/openlobby/urls.py @@ -0,0 +1,6 @@ +from django.urls import path +from openlobby.core.views import IndexView + +urlpatterns = [ + path('', IndexView.as_view(), name='index'), +] diff --git a/openlobby/wsgi.py b/openlobby/wsgi.py new file mode 100644 index 0000000000000000000000000000000000000000..5f399cc41e6446c3d2f303983b18fd88301d45c9 --- /dev/null +++ b/openlobby/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for openlobby project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "openlobby.settings") + +application = get_wsgi_application() diff --git a/requirements.txt b/requirements.txt index 13ed9ee43cec7473f77dbdc30fc658f2534af996..1503bd640656bacb2e6567e84f90b5479d4a6381 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,12 +1,16 @@ +Django>=2,<2.1 graphene>=2.0,<3.0 flask>=0.12,<0.13 flask_graphQL>=1.4,<1.5 elasticsearch-dsl>=5.3.0,<6.0.0 pytest>=3.2.3,<3.3.0 +pytest-django>=3.1.2,<3.2 pytest-env>=0.6.2,<0.7 oic>=0.12.0,<0.13 pyjwt>=1.5.3,<1.6 iso8601>=0.1.12,<0.2 -arrow>=0.10.0,<0.11 +arrow>=0.12.0,<0.13 bleach>=2.1.1,<2.2 snapshottest>=0.5.0,<0.6 +psycopg2>=2.7,<2.8 +dsnparse>=0.1.11,<0.2