This GitHub Repository represents the backend of the YACOID project. The frontend GitHub Repository can be found here: yacoid-frontend. The backend uses MongoDB as a database. To create the different REST endpoints the framework fiber was used. To authenticate users and manage users and roles the provider Authorizer was used.
There are two workflows: Development and Production. The main difference between those is that the Production workflow creates the Docker image of the API automatically and launches the API, Authorizer and MongoDB as Docker containers. The API and Authorizer then use the same MongoDB. In the Development workflow the API uses the locally running MongoDB and Authorizer uses a MongoDB inside a Docker container.
- You have MongoDB installed and running. The Community Server can be downloaded here: MongoDB Community Server. It is also recommended to have MongoDB Compass installed, because you can view the data in a GUI.
- You have Docker installed and running. Docker can be downloaded here: Docker. On windows you may have to add Hyper-V as a feature and enable virtualization in your BIOS. This StackOverflow Thread can help.
- You have Go installed.
- (optional) You may want to have Insomnia installed to easily test requests. A template file, that can be imported into Insomnia, can be found inside the folder
misc
.
-
Entering an URL in a
.env
fileProblem: If you configure a
.env
file and need to enter a URL, then you need to remove the last slash at the end of the URL. Otherwise there can be wrong redirect or the authentication does not work.Solution: Remove the last slash in the URL. For example, change
http://localhost:8080/
tohttp://localhost:8080
-
Entering a password in a
.env
fileProblem: If you configure a
.env
file, need to enter a password (for example like inADMIN_SECRET
) and your password contains a "#" character, the "#" character and the characters after that will be interpreted as a comment.Solution: Enclose your password with " characters. For example: Change your current line:
ADMIN_SECRET=abc#123
toADMIN_SECRET="abc#123"
- Copy file
.env.auth.sample
and rename it to.env.auth
- Configure the auth environment in the file like here:
# https://docs.authorizer.dev/core/env
ADMIN_SECRET=
ENV=development
DATABASE_TYPE=mongodb
DATABASE_URL=mongodb://database:27017 # database is the name of the docker container
ROLES=user
PROTECTED_ROLES=moderator,admin
- Go into the scripts folder and execute the
start_authorizer.bat
script (docker must be running) - The Authorizer Dashboard is now available under http://localhost:8080/
- When logging in enter the password specified in
ADMIN_SECRET
- The Authorizer Instance should now be successfully configured and can be accessed
- Copy file
.env.sample
and rename it to.env
- Configure the api environment in the file like here:
DATABASE_URL=mongodb://localhost:27017
REST_PORT=3000
AUTH_CLIENT_ID=
AUTH_ADMIN_SECRET=
AUTH_URL=http://localhost:8080
AUTH_REDIRECT_URL=http://127.0.0.1:5173
-
To fill
AUTH_CLIENT_ID
do the following:- Navigate into
Environment > OAuth Config
in the Authorizer Dashboard - Copy the client id
- Navigate into
-
AUTH_ADMIN_SECRET
needs to be the same asADMIN_SECRET
from the.env.auth
file
- Run the command
go run .
orair
in the project root folder - The individual API endpoints can be accessed under http://localhost:3000/
- Copy file
.env.auth.sample
and rename it to.env.prod.auth
- Configure the auth environment in the file like here:
# https://docs.authorizer.dev/core/env
ADMIN_SECRET=
ENV=production
DATABASE_TYPE=mongodb
DATABASE_URL=mongodb://database:27017 # database is the name of the docker container
ROLES=user
PROTECTED_ROLES=moderator,admin
- Copy file
.env.sample
and rename it to.env.prod
- Configure the api environment in the file like here:
DATABASE_URL="mongodb://database:27017"
REST_PORT=3000
AUTH_CLIENT_ID=
AUTH_ADMIN_SECRET=
AUTH_URL=http://authorizer:8080
AUTH_REDIRECT_URL=http://127.0.0.1:5173
- If you already successfully started Authorizer in development, then you can fill the existing
AUTH_CLIENT_ID
. Otherwise this will be done in the next steps. AUTH_ADMIN_SECRET
needs to be the same asADMIN_SECRET
from the.env.prod.auth
file
- Go into the scripts folder and execute the
start_production_system.bat
script (docker must be running) - If you already filled
AUTH_CLIENT_ID
:- The system is now successfully running
- The Authorizer Dashboard is available under http://localhost:8080/
- When logging in enter the password specified in
ADMIN_SECRET
in the.env.prod.auth
file
- When logging in enter the password specified in
- The API endpoints are available under http://localhost:3000/
- If you didn't fill
AUTH_CLIENT_ID
:- Then the first time the Docker container
yacoid-api-container
will fail to start, because theAUTH_CLIENT_ID
is missing in the.env.prod
file. - To fill
AUTH_CLIENT_ID
do the following:- The Authorizer Dashboard is still available under http://localhost:8080/
- Navigate into
Environment > OAuth Config
in the Authorizer Dashboard - Copy the client id
- Now terminate the current process and repeat the current section
Start the system
- Then the first time the Docker container