README.md 8.07 KB
Newer Older
asmundh's avatar
asmundh committed
1
2
3
4
5
6
7
8
9
10
# SecFit

SecFit (Secure Fitness) is a hybrid mobile application for fitness logging.

## Deploy with Docker

### Prerequisites:

Docker

Haakon Gunleiksrud's avatar
Haakon Gunleiksrud committed
11
12
Python 3.8.10

asmundh's avatar
asmundh committed
13
14
15
16
17
18
19
Git

Windows hosts must use Education or more advanced versions to run Docker \
Download: https://innsida.ntnu.no/wiki/-/wiki/English/Microsoft+Windows+10

### Install:

20
$ git clone https://gitlab.stud.idi.ntnu.no/<your username>/tdt4242-base.git \
asmundh's avatar
asmundh committed
21
22
23
24
25
26
27
$ cd secfit/

### Run:

$ docker-compose up --build \
Hosts the application on http://localhost:9090 with default settings

28
29
30
31
32
33
34
35
36
37
38
39
You might have to run Docker with administrator privileges to get it to run properly:

#### Windows:
Either open the terminal as administrator (Windows key -> Search "cmd" -> "Run as administrator")

or

$ runas /user/Administrator "docker-compose up --build"

#### Unix/OSX:
$ sudo docker-compose up --build

asmundh's avatar
asmundh committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

## Technology
- **deployment** Docker
- **web** Nginx
- **database** Postgre SQL
- **backend** Django 3 with Django REST framework
- **application** 
    - **browser** - HTML5/CSS/JS, Bootstrap v5 (no jQuery dependency)
    - **mobile** Apache Cordova (uses same website)
- **authentication** JWT


## Code and structure

.gitlab-ci.yml - gitlab ci
requirements.txt - Python requirements
package.json - Some node.js requirements, this is needed for cordova

- **secfit/** django project folder containing the project modules
  - **<application_name>/** - generic structure of a django application
    - **admins.py** - file contaning definitions to connect models to the django admin panel
    - **urls.py** - contains mapping between urls and views
    - **models.py** - contains data models
    - **permissions.py** - contains custom permissions that govern access
    - **serializers.py** - contains serializer definitions for sending data between backend and frontend
    - **parsers.py** - contains custom parsers for parsing the body of HTTP requests
    - **tests/** - contains tests for the module. [View Testing in Django](https://docs.djangoproject.com/en/2.1/topics/testing/) for more.
    - **views.py** - Controller in MVC. Methods for rendering and accepting user data
    - **forms.py**  -  definitions of forms. Used to render html forms and verify user input
    - **settings.py** - Contains important settings at the application and/or project level
    - **Procfile** - Procfile for backend heroku deployment
  - **media/** - directory for file uploads (need to commit it for heroku)
  - **comments/** - application handling user comments and reactions
  - **secfit/** - The projects main module containing project-level settings.
  - **users/** - application handling users and requests
  - **workouts/** - application handling exercises and workouts
  - **manage.py** - entry point for running the project.
  - **seed.json** - contains seed data for the project to get it up and running quickly (coming soon)



## Local setup
It's recommended to have a look at: https://www.djangoproject.com/start/
Just as important is the Django REST guide: https://www.django-rest-framework.org/

Create a virtualenv https://docs.python-guide.org/dev/virtualenvs/


### Django

Installation with examples for Ubuntu. Windows and OSX is mostly the same

Fork the project and clone it to your machine.

#### Setup and activation of virtualenv (env that prevents python packages from being installed globaly on the machine)
Naviagate into the project folder, and create your own virtual environment


#### Install python requirements

`pip install -r requirements.txt`


#### Migrate database

`python manage.py migrate`


#### Create superuser

Create a local admin user by entering the following command:

`python manage.py createsuperuser`

Only username and password is required


#### Start the app

`python manage.py runserver`


#### Add initial data

You can add initial data either by going to the url the app is running on locally and adding `/admin` to the url.

Add some categories and you should be all set.

Or by entering 

`python manage.py loaddata seed.json`

### Cordova
Cordova CLI guide: https://cordova.apache.org/docs/en/latest/guide/cli/
If you want to run this as a mobile application
- Navigate to the frontend directory
- For android, do `cordova run android`
- For ios, do `cordova run ios`
- For browser, do `cordova run browser`

It's possible you will need to add the platforms you want to run and build.
The following documentation can be used to run the application in an Android emulator: \
https://cordova.apache.org/docs/en/latest/guide/platforms/android/index.html
143
144
145
146
147
148

## Continuous integration
WARNING: Do not perform penetration testing on Heroku applications

Continuous integration will build the code pushed to master and push it to your heroku app so you get a live version of your latest code by just pushing your code to GitLab.

Haakon Gunleiksrud's avatar
Haakon Gunleiksrud committed
149
150
151
152
1. Make sure you have the right python version (3.8.10) and have all other requirements installed.
2. Create a heroku account and an app for both the frontend and the backend. (Choose Europe as your region)
3. Select buildpacks for the two apps. The backend uses Python while the frontend uses nodejs.
   * Heroku > Settings > Buildpacks  > Add buildpack
153
   * Both applications need the buildpack https://github.com/heroku/heroku-buildpack-multi-procfile.git too.
Haakon Gunleiksrud's avatar
Haakon Gunleiksrud committed
154
4. Set the two projects in the .gitlab-cs.yml file by replacing `<Your-herokuproject-name>` with the name of the Heroku apps you created. Commit and push your change.
155
`- dpl --provider=heroku --app=<Your-herokuproject-name> --api-key=$HEROKU_STAGING_API_KEY`
Haakon Gunleiksrud's avatar
Haakon Gunleiksrud committed
156
157
158
159
160
161
5. Set/Add varibles at GitLab
   * GitLab > settings > ci > Variables > Expand
   * Key = `HEROKU_STAGING_API_KEY`
   * Value = Your API Key from Heroku (Heroku > Account Settings > API Key > Reveal)
   * Type = Variable, Environment scope = All(default)
   * Protect should not be enabled and Mask should be enabled
162
6. Add heroku database for the backend
Haakon Gunleiksrud's avatar
Haakon Gunleiksrud committed
163
164
165
166
   * Heroku > Resources > Add ons > search for `postgres` > add "Heroku Postgres"
   * Choose the free plan.
7. Set variables for the backend on Heroku. Heroku > Settings > Config vars > Reveal vars
   * `DATABASE_URL` = Should be set by default. If not here is where you can find it: Heroku > Resources > postgress > settings > view credentials > URI
167
   * `IS_HEROKU` = `IS_HEROKU`
Haakon Gunleiksrud's avatar
Haakon Gunleiksrud committed
168
169
   * `PROCFILE` = `Procfile`
8. Set variables for the frontend on heroku. Heroku > Settings > Config vars > Reveal vars. Insert the URL for your backend app and the path to your Procfile. (Example: `https://secfit-group01-v22-backend.herokuapp.com`)
170
171
   * `BACKEND_HOST` = `https://<SECFIT_BACKEND>.herokuapp.com`
   * `PROCFILE` = `frontend/Procfile`
Haakon Gunleiksrud's avatar
Haakon Gunleiksrud committed
172
173
174
175
176
9. Push the repository to both of the heroku applications 
   * Follow the guides on https://devcenter.heroku.com/articles/git#for-an-existing-heroku-app 
      * Make sure you install the heroku CLI https://devcenter.heroku.com/articles/heroku-cli
      * Make sure you log in to the heroku CLI ($ heroku login)
      * Make sure you create a remote for the frontend and backend apps (Example: heroku git:remote -a secfit-group01-v22-frontend)
Haakon Gunleiksrud's avatar
Haakon Gunleiksrud committed
177
      * Push your code to both apps (Your URLs can also be found on the settings page on your Heroku apps)	
Haakon Gunleiksrud's avatar
Haakon Gunleiksrud committed
178
179
         * Example: $ git push https://git.heroku.com/secfit-group01-v22-frontend.git master
         * Make sure you use `master` and not `main` as your target branch
Haakon Gunleiksrud's avatar
Haakon Gunleiksrud committed
180
181
182
10. On GitLab go to CI / CD in the repository menu and select `Run Pipeline` if it has not already started. When both stages complete the app should be available on heroku. The log should state that the app was deployed.
11. Setup the applications database.
   * Log in to the Heroku CLI by entering `heroku login` if you have not already done this. This opens a webbrowser and you accept the login request.
183
   * Migrate database by entering
Haakon Gunleiksrud's avatar
Haakon Gunleiksrud committed
184
185
   `heroku run python backend/secfit/manage.py migrate -a <heroku-app-name>`. `Heroku run` will run the folowing command on your heroku instance. Remember to replace `<heroku-app-name>` with your backend app name
   * and create an admin account for the backend by running
186
   `heroku run python backend/secfit/manage.py createsuperuser -a <heroku-app-name>`.
187
   * seed database `heroku run python backend/secfit/manage.py loaddata backend/secfit/seed.json -a <heroku-app-name>`