Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions dev-manuals/login-backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Login Backend
This Markdown file contains all necessary informations for developers about the Login Backend repository.

## Requirements
- [Go](https://go.dev) 1.16 or higher
- [GoLand](https://www.jetbrains.com/go/) as IDE
- [Docker](https://www.docker.com/) to run docker containers
- ([Docker Desktop](https://www.docker.com/products/docker-desktop) to build and run docker containers graphically)
- ([Postgres](https://www.postgresql.org/) database)

## Starting with this repository
First, make sure Goland knows your Docker path: Check in
`Files -> Settings -> Build, Execution, Deployment -> Docker`
If in the left column no Docker is registered, add Docker by clicking the top left add Button. The system Docker (suggested by default) should suffice.

## Project structure

| File / Directory | Description |
|--------------------------------------|-------------------------------------------------------------------------------------------|
| `main.go` | Entry point of the program |
| `swagger/` | Contains the API specification (Swagger) |
| `prisma/` | Database schema (Prisma) |
| `src/handlers/` | Our HTTP handler implementations (Go) |
| `src/handlers/handlers.go` | Adds all our handler functions to the server |
| `src/gen/` | Generated files from Prisma and Swagger. Do not edit or commit generated files! |
| `src/gen/restapi/configure_login.go` | Configuration for the HTTP server. The only file in `src/gen/` that can be safely edited. |

This repository contains run configurations which are loaded automatically when you open the project in GoLand. To run the project, select the go build configuration in the top right toolbar.

## Run application
You can run the application in three ways.
- Directly from GoLand
- Command line with go commands
- Build docker image and run a container

### In IDE
The `go build` configuration (top right of GoLand) performs the following tasks for you:

- starts a postgres database with docker
- generates the Swagger code for the server
- generates the Prisma database client
- compiles & runs the project

### From command line
You need to run your own postgres database.
```sh
export PORT=4000
export POSTGRES_URL=postgresql://postgres:password@localhost:5432/postgres # Update credentials
export JWT_KEY=secretKey

go generate
go run github.com/prisma/prisma-client-go migrate deploy # When there is new database scheme, it needs to mirgrate
go run .

```

### From Dockerfile
You need to run your own postgres database.

#### Build docker image
```sh
docker build -t login-backend .
```
#### Start container
```sh
docker run --name login-backend -p 4000:4000 -e "POSTGRES_URL=postgresql://postgres:password@localhost:5432/postgres" -e "JWT_KEY=secretKey" login-backend
```


## Migrating the database
When changes to the database are made, you can create a migration by running
```sh
POSTGRES_URL=postgresql://postgres:password@localhost:5432/postgres go run github.com/prisma/prisma-client-go migrate dev --name $MIGRATION_NAME
```
inside the project root dir.\
To apply all pending migrations, simply run
```sh
POSTGRES_URL=postgresql://postgres:password@localhost:5432/postgres go run github.com/prisma/prisma-client-go migrate deploy
```
inside the project root dir.

## JWT (Json Web Tokens)
To encode a JWT, a secret key is needed.
This secret key needs to be stored under the environment variable `JWT_KEY`.

## Go-Swagger
This Rest-API uses Go-Swagger. So the `swagger/swagger.yml` is the base of the rest api. You find documentation about the _swagger.yml_ [here](https://swagger.io/docs/specification/basic-structure/).

After changes were made in the `swagger.yml` file you should run `go generate` so that the files will be generated. You can work with these generated objects.
58 changes: 58 additions & 0 deletions user-manuals/login-backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Login Backend
This Markdown file contains all necessary informations for end users about the Login Backend repository.

## Requirements
- [Go](https://go.dev) 1.16 or higher
- [Docker](https://www.docker.com/) to run docker containers
- [Postgres](https://www.postgresql.org/) database

## Run application
You can run the application in two ways.
- Command line with go commands
- Build docker image and run a container

### From command line
You need to run your own postgres database.
```sh
export PORT=4000
export POSTGRES_URL=postgresql://<user>:<password>@<host>:<port>/<database> # Update key words in '<>' with your credentials
export JWT_KEY=<some secret key>

go generate
go run github.com/prisma/prisma-client-go migrate deploy # Migrate database scheme if it has changed
go run .
```

### From Dockerfile

#### Build docker image
```sh
docker build -t login-backend .
```
Or get it from our Github Package Registry if you have access to it
```sh
docker pull ghcr.io/gamify-it/login-backend:latest
```

#### Start container
If you build your image yourself, run following
```sh
docker run --name login-backend -p 4000:4000 -e "POSTGRES_URL=postgresql://<user>:<password>@<host>:<port>/<database>" -e "JWT_KEY=<secret key>" login-backend
```
Or in case you pulled from Github Package Registry, run
```sh
docker run --name login-backend -p 4000:4000 -e "POSTGRES_URL=postgresql://<user>:<password>@<host>:<port>/<database>" -e "JWT_KEY=<secret key>" ghcr.io/gamify-it/login-backend:latest
```
Replace `<.*>` with the corresponding credentials.


## Updating
When you update the login backend, changes to the database could have been made, so make sure to migrate the database structure simply by running
```sh
POSTGRES_URL=postgresql://<user>:<password>@<host>:<port>/<database> go run github.com/prisma/prisma-client-go migrate deploy
```

## JWT (Json Web Tokens)
To encode a JWT, a secret key is needed.
This secret key needs to be stored under the environment variable `JWT_KEY`.
KEEP IT PRIVATE!