Skip to content

Commit 217a0cf

Browse files
committed
heroku web server
1 parent 59e53f4 commit 217a0cf

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

src/app.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,27 @@
1515

1616
app = Flask(__name__)
1717
api = Api(app, version="1.0", title="Code challenge - Suggestions API")
18+
searchEngine: PlaceSearchEngine = None
19+
20+
21+
@api.route('/suggestions')
22+
class SuggestionsApiDescriptor(Resource):
23+
@api.marshal_with(SuggestionsDescriptor().createDescription(api))
24+
def get(self) -> dict:
25+
return SuggestionsApi(searchEngine).autocomplete()
1826

1927

2028
class InfraFactory:
2129

22-
async def createSearchEngine(self, db: IDb) -> PlaceSearchEngine:
30+
async def createSeach(self) -> PlaceSearchEngine:
31+
32+
# create the database
33+
db: IDb = await self._createDb()
34+
35+
# create the search engine
36+
return await self._createSearchEngine(db)
37+
38+
async def _createSearchEngine(self, db: IDb) -> PlaceSearchEngine:
2339
strategy = LevenshteinTrieSearchQueryStrategy(db, settings.SCORE_WEIGHT_QUERY_SEARCH)
2440
await strategy.initAsync()
2541

@@ -28,34 +44,17 @@ async def createSearchEngine(self, db: IDb) -> PlaceSearchEngine:
2844
scoreWeightPopulationSize=settings.SCORE_WEIGHT_POPULATION_SIZE,
2945
scoreWeightCoordinatesDistance=settings.SCORE_WEIGHT_COORDINATES_DISTANCE)
3046

31-
async def createDb(self) -> IDb:
47+
async def _createDb(self) -> IDb:
3248
dataReader = TsvPlacesReader(settings.DATA_SOURCE_PATH)
3349
db: IDb = InMemoryDb(dataReader)
3450
await db.initAsync()
3551
return db
3652

3753

38-
async def run():
39-
40-
infra = InfraFactory()
41-
42-
# create the database
43-
db: IDb = await infra.createDb()
44-
45-
# create the search engine
46-
searchEngine = await infra.createSearchEngine(db)
47-
48-
# create the api
49-
@api.route('/suggestions')
50-
class SuggestionsApiDescriptor(Resource):
51-
@api.marshal_with(SuggestionsDescriptor().createDescription(api))
52-
def get(self) -> dict:
53-
return SuggestionsApi(searchEngine).autocomplete()
54-
55-
app.run()
56-
5754
if __name__ == '__main__':
5855

5956
loop = asyncio.get_event_loop()
60-
task = loop.create_task(run())
57+
task = loop.create_task(InfraFactory().createSeach())
6158
loop.run_until_complete(task)
59+
searchEngine = task.result()
60+
app.run()

0 commit comments

Comments
 (0)