Skip to content

Commit 4b4835f

Browse files
committed
Setup Django REST Framework, start the server, and test the API.
1 parent b4b62ae commit 4b4835f

2 files changed

Lines changed: 23 additions & 7 deletions

File tree

octofit-tracker/backend/octofit_tracker/settings.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,17 @@
2626
DEBUG = True
2727

2828

29-
# Allow all hosts
30-
ALLOWED_HOSTS = ['*']
29+
# Allow codespace URL and localhost
30+
import os
31+
codespace_name = os.environ.get('CODESPACE_NAME')
32+
if codespace_name:
33+
ALLOWED_HOSTS = [
34+
f"{codespace_name}-8000.app.github.dev",
35+
'localhost',
36+
'127.0.0.1',
37+
]
38+
else:
39+
ALLOWED_HOSTS = ['localhost', '127.0.0.1']
3140

3241

3342
# Application definition

octofit-tracker/backend/octofit_tracker/urls.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from .views import TeamViewSet, UserViewSet, ActivityViewSet, WorkoutViewSet, LeaderboardViewSet
2020
from rest_framework.response import Response
2121
from rest_framework.decorators import api_view
22+
import os
2223

2324
router = routers.DefaultRouter()
2425
router.register(r'teams', TeamViewSet)
@@ -29,12 +30,18 @@
2930

3031
@api_view(['GET'])
3132
def api_root(request, format=None):
33+
codespace_name = os.environ.get('CODESPACE_NAME')
34+
if codespace_name:
35+
base_url = f"https://{codespace_name}-8000.app.github.dev/api/"
36+
else:
37+
# fallback to localhost for local development
38+
base_url = "http://localhost:8000/api/"
3239
return Response({
33-
'users': request.build_absolute_uri('users/'),
34-
'teams': request.build_absolute_uri('teams/'),
35-
'activities': request.build_absolute_uri('activities/'),
36-
'workouts': request.build_absolute_uri('workouts/'),
37-
'leaderboard': request.build_absolute_uri('leaderboard/'),
40+
'users': base_url + 'users/',
41+
'teams': base_url + 'teams/',
42+
'activities': base_url + 'activities/',
43+
'workouts': base_url + 'workouts/',
44+
'leaderboard': base_url + 'leaderboard/',
3845
})
3946

4047
urlpatterns = [

0 commit comments

Comments
 (0)