diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..7763f56 --- /dev/null +++ b/.env.example @@ -0,0 +1,9 @@ +# do not use this env file. make a copy of it, save it as .env and make sure to update HE_CLIENT_SECRET +HE_CLIENT_SECRET=your_secret +# set to False in prod +HACKIDE_DEBUG=True +MONGO_INITDB_ROOT_USERNAME=root +MONGO_INITDB_ROOT_PASSWORD=example +MONGO_INITDB_USER=test_user +MONGO_INITDB_PWD=test_password +MONGO_INITDB_DATABASE=codes \ No newline at end of file diff --git a/.gitignore b/.gitignore index 5fad2da..6ee5518 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +.idea/ +.vscode/ + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] @@ -7,6 +10,7 @@ __pycache__/ *.so # Distribution / packaging +.env .Python env/ build/ diff --git a/Dockerfile b/Dockerfile new file mode 100755 index 0000000..2093283 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM python:2.7-buster +RUN apt-get install default-libmysqlclient-dev libcurl4-openssl-dev +# fix mysql error: ‘MYSQL {aka struct st_mysql}’ has no member named ‘reconnect’ +RUN sed '/st_mysql_options options;/a unsigned int reconnect;' /usr/include/mysql/mysql.h -i.bkp + +RUN useradd -ms /bin/bash app_runner +USER app_runner + +ENV PYTHONBUFFERED=1 +ENV PATH "$PATH:/home/app_runner/.local/bin" + +WORKDIR /home/app_runner/code + +COPY requirements.txt /home/app_runner/code/ +RUN pip install -r requirements.txt +COPY . /home/app_runner/code/ +USER root +RUN chown -R app_runner:app_runner * +USER app_runner diff --git a/README.md b/README.md index b688689..3fc177a 100644 --- a/README.md +++ b/README.md @@ -8,44 +8,19 @@ hackIDE is an online code editor, compiler and interpreter based on Django, powe ## Screenshot- ![Screenshot for HackIDE](/hackIDE/static/hackIDE/img/screenshot.png?raw=true "Screenshot for HackIDE") -## Getting set up -Depending on your environment, you may need to install some libraries. On Debian/Ubuntu, you will need to: -```shell -sudo apt-get install libmysqlclient-dev libcurl4-gnutls-dev -``` - -To get your Python environment properly set up, you can create a virtual environment and use the requirements.txt file to install the proper versions of various libraries. - -```shell -# Navigate to a directory of your choosing, where you will store your virtual environment. -$ mkdir virtualEnvs -$ cd virtualEnvs -$ virtualenv hackIDE_venv # We need python2. If you are using 3 by default, type virtualenv -p /usr/bin/python2.7 hackIDE_venv -$ source ~/virtualEnvs/hackIDE_venv/bin/activate -$ cd /location/of/your/copy/of/hackIDE -$ pip install -r requirements.txt -``` - ## How to run the server locally To run the server locally, you will need to do two things: -1. Get a hackerearth API "Client Secret Key" -2. Change the hackIDE_project/settings.py file +1. Get a hackerearth API "Client Secret Key" (see section below) +2. Create a copy of .env.example, save it as .env and replace HE_CLIENT_SECRET with your token ### Get a Client Secret Key Go to https://www.hackerearth.com/api/register/, create a HackerEarth profile and register a URL. You can register http://google.com, for example. Then you will be provided with a Client Secret Key. -### Change the hackIDE_project/settings.py file. -Change the ALLOWED_HOSTS line to -```shell -ALLOWED_HOSTS = ['*'] if not DEBUG else ['*'] -``` - ### Then, startup the server with: ```shell -$ python manage.py collectstatic -$ HE_CLIENT_SECRET=your_token_here python manage.py runserver +$ docker-compose up ``` Then, you can connect to the site at 0.0.0.0:8000. diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100755 index 0000000..15493a7 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,23 @@ +version: "3.9" +# base compose. utilizes overrides +# dev: docker-compose up. deployment: docker-compose -f docker-compose.yml -f docker-compose.prod.yml up +services: + web: + env_file: + - .env + volumes: + - ./:/home/app_runner/code/ + user: app_runner + build: . + image: hackide + command: sh -c "python manage.py collectstatic --no-input && python manage.py runserver 0.0.0.0:8000" + # command: sh -c "python manage.py runserver 0.0.0.0:8000" + ports: + - 8000:8000 + mongo: + image: mongo + env_file: + - .env + restart: always + volumes: + - ./mongo-init.sh:/docker-entrypoint-initdb.d/mongo-init.sh:ro \ No newline at end of file diff --git a/hackIDE/views.py b/hackIDE/views.py index 0ec6319..1ce70e9 100644 --- a/hackIDE/views.py +++ b/hackIDE/views.py @@ -15,10 +15,7 @@ COMPILE_URL = "https://api.hackerearth.com/v3/code/compile/" RUN_URL = "https://api.hackerearth.com/v3/code/run/" -# access config variable -DEBUG = (os.environ.get('HACKIDE_DEBUG') != None) -# DEBUG = (os.environ.get('HACKIDE_DEBUG') or "").lower() == "true" -CLIENT_SECRET = os.environ['HE_CLIENT_SECRET'] if not DEBUG else "" +CLIENT_SECRET = os.environ.get('HE_CLIENT_SECRET', '') permitted_languages = ["C", "CPP", "CSHARP", "CLOJURE", "CSS", "HASKELL", "JAVA", "JAVASCRIPT", "OBJECTIVEC", "PERL", "PHP", "PYTHON", "R", "RUBY", "RUST", "SCALA"] diff --git a/hackIDE_project/settings.py b/hackIDE_project/settings.py index 1bf96a5..58604f2 100644 --- a/hackIDE_project/settings.py +++ b/hackIDE_project/settings.py @@ -87,8 +87,8 @@ } # use your db creds here -# TODO: Update with new host details before making live on heroku -connect(host='mongodb://JSSaini08:hackidecodes@ds011705.mlab.com:11705/codes') +# connect(host='mongodb://JSSaini08:hackidecodes@ds011705.mlab.com:11705/codes') +connect(host='mongodb://{}:{}@mongo:27017/{}'.format(os.environ.get('MONGO_INITDB_USER'), os.environ.get('MONGO_INITDB_PWD'), os.environ.get('MONGO_INITDB_DATABASE'))) # Password validation # https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators diff --git a/mongo-init.sh b/mongo-init.sh new file mode 100644 index 0000000..29a7816 --- /dev/null +++ b/mongo-init.sh @@ -0,0 +1,20 @@ +set -e + +mongo <