Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/asuc-octo/berkeleytime in…
Browse files Browse the repository at this point in the history
…to izzie-sphinx
  • Loading branch information
izzielau authored and izzielau committed Feb 13, 2020
2 parents 420a797 + d2cba10 commit 33a9086
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 10 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ help:

# The following commands control the docker compose component of the stack.

# Builds the cluster, particularly the search component.
build:
docker-compose -f build/docker-compose.yml build

# Up brings the cluster up by booting a redis, postgres, and server.
up:
docker-compose -f build/docker-compose.yml up
Expand Down
2 changes: 1 addition & 1 deletion berkeleytime/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ rfc3339==6.0

# Python postgres client library. We need this because psycopg2 is not
# supporting python 2 anymore. It requires us to download binary instead.`
psycopg2-binary==2.8.4
psycopg2-binary==2.8.4
49 changes: 40 additions & 9 deletions build/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
version: '3'
services:
backend:
image: berkeleytime/berkeleytime:latest
build:
context: ../berkeleytime
command: ["0.0.0.0:8000"]
volumes:
- ../berkeleytime:/berkeleytime
Expand All @@ -18,19 +19,14 @@ services:
- "SIS_COURSE_APP_KEY=***REMOVED***"
depends_on:
- redis
# - postgres
frontend:
image: berkeleytime/frontend:latest
build:
context: ../frontend
volumes:
- ../frontend:/frontend
- /frontend/node_modules
redis:
image: redis:alpine
# no longer uses the local postgres database
# postgres:
# image: postgres:11
# volumes:
# - ./postgres-data:/var/lib/postgresql/data
nginx:
image: nginx
volumes:
Expand All @@ -40,4 +36,39 @@ services:
depends_on:
- backend
- frontend

search:
build: ../search
ports:
- "5000:5000"
volumes:
- .:/code
environment:
FLASK_ENV: development
depends_on:
- elasticsearch
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.5.0
container_name: elasticsearch
ports:
- '9200:9200'
volumes:
- es_data:/usr/share/elasticsearch/data
environment:
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- cluster.name=docker-cluster
- node.name=elasticsearch
- bootstrap.memory_lock=true
- discovery.seed_hosts=elasticsearch
- cluster.initial_master_nodes=elasticsearch
ulimits:
memlock:
soft: -1
hard: -1
healthcheck:
test: curl --cacert /usr/share/elasticsearch/config/certs/ca/ca.crt -s https://localhost:9200 >/dev/null; if [[ $$? == 52 ]]; then echo 0; else echo 1; fi
interval: 30s
timeout: 10s
retries: 5
volumes:
es_data:
driver: local
11 changes: 11 additions & 0 deletions search/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM python:3.8

EXPOSE 5000

WORKDIR app

COPY . /app/

RUN pip install -r requirements.txt

CMD python python3/flask/app.py
7 changes: 7 additions & 0 deletions search/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
The search server uses Python 3.8, the latest version.

Build the search service and run the app.
> make build && make up
The flask server waits for Elasticsearch to boot up before starting.
This may take awhile.
65 changes: 65 additions & 0 deletions search/python3/flask/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from flask import Flask, escape, request, render_template
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search
import time

es = None
app = None


def maybe_create_index():
if not es.indices.exists(index='catalog'):
print('index doesn\'t exist')
es.indices.create(index='catalog')
else:
print('index exists')


@app.route("/")
def index():
return search()


@app.route('/search')
def search():
return render_template('search.html')


@app.route('/results', methods=['GET'])
def search_and_serve():
if request.method == 'POST':
query = escape(request.form['query'])
s = Search(using=es, index='catalog') \
.query('match', title=query)
res = s.execute()
output = str("Got %d Hits: \n" % res['hits']['total']['value'])
for hit in res['hits']['hits']:
output += hit['_source']['title'] + ', '
return 'Your query: ' + query + ' | Search Results: ' + output
else:
return 'Failed'


@app.route('/add')
def add_class():
return render_template('index_class.html')


@app.route('/index_class', methods=['POST', 'GET'])
def index_class():
if request.method == 'POST':
title = escape(request.form['class_title'])
doc = {'title': title}
es.index(index='catalog', doc_type='class', body=doc)
return 'Class indexed: ' + title
else:
return 'Failed'


if __name__ == '__main__':
es = Elasticsearch(['elasticsearch'])
# Wait for Elasticsearch to boot up
while not es.ping():
time.sleep(2)
app = Flask(__name__)
app.run(debug=True, host='0.0.0.0')
13 changes: 13 additions & 0 deletions search/python3/flask/templates/index_class.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Index a Class</title>
</head>
<body>
<h3>Index a Class Title</h3>
<form action="/index_class" method="post">
<input type="text" name="class_title"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
14 changes: 14 additions & 0 deletions search/python3/flask/templates/search.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Search for Classes</title>
</head>
<body>
<h3>Search for Classes</h3>
<form action="/results" method="post">
<input type="text" name="query"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

3 changes: 3 additions & 0 deletions search/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
flask==1.1.1
elasticsearch
elasticsearch-dsl

0 comments on commit 33a9086

Please sign in to comment.