Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to create admin account #477

Merged
merged 8 commits into from
Feb 2, 2022
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions .idea/runConfigurations/GraphQL_Schema_Export.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/runConfigurations/Run_Backend.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ FROM openjdk:8-jre-slim

WORKDIR /backend
COPY --from=build /output/* .
ENTRYPOINT ["./bin/backend"]
ENTRYPOINT ["./bin/backend", "execute"]
74 changes: 51 additions & 23 deletions backend/src/main/kotlin/app/ehrenamtskarte/backend/EntryPoint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,63 @@ import app.ehrenamtskarte.backend.stores.importer.LbeDataImporter
import com.expediagroup.graphql.extensions.print
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.ProgramResult
import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.core.subcommands
import com.github.ajalt.clikt.parameters.arguments.argument
import java.io.File


class Entry : CliktCommand() {
private val exportApi by option(help = "Export GraphQL schema. Given ")
private val importFlag by option("--import").flag()
companion object {
fun isProductionEnvironment(): Boolean {
return System.getProperty("app.production", "").isNotEmpty()
}
}

override fun run() {
}

}

class GraphQLExport : CliktCommand(name = "graphql-export") {
private val path by argument(help = "Export GraphQL schema. Given ")
override fun run() {
val schema = GraphQLHandler().graphQLSchema.print()
val file = File(path)
file.writeText(schema)
}
}

class Import : CliktCommand(help = "Imports stores from a configured data source. Various properties must be set.") {
maxammann marked this conversation as resolved.
Show resolved Hide resolved
override fun run() {
val production = System.getProperty("app.production", "").isNotEmpty()

val exportApi = exportApi
if (exportApi != null) {
val schema = GraphQLHandler().graphQLSchema.print()
val file = File(exportApi)
file.writeText(schema)
} else {
Database.setup(!production)
if (importFlag) {
println("Importing data from Lbe")
if (!LbeDataImporter.import(!production)) {
throw ProgramResult(statusCode = 5)
}
} else {
WebService().start(production)
}
val production = Entry.isProductionEnvironment()
Database.setup(!production)
if (!LbeDataImporter.import(!production)) {
throw ProgramResult(statusCode = 5)
}
}
}

fun main(argv: Array<String>) = Entry().main(argv)
class CreateAdmin : CliktCommand(help = "Creates an admin account with the specified email and password") {
private val email by argument(
help = "The email"
)

private val password by argument(
help = "The password"
)

override fun run() {
val production = Entry.isProductionEnvironment()
Database.setup(!production)
Database.createAccount(email, password)
}
}

class Execute : CliktCommand(help = "Starts the webserver") {
override fun run() {
val production = Entry.isProductionEnvironment()
Database.setup(!production)
WebService().start(production)
}
}

fun main(args: Array<String>) = Entry().subcommands(Execute(), Import(), CreateAdmin(), GraphQLExport()).main(args)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package app.ehrenamtskarte.backend.common.database

import app.ehrenamtskarte.backend.auth.database.repos.AdministratorsRepository
import org.jetbrains.exposed.sql.Database.Companion.connect
import org.jetbrains.exposed.sql.StdOutSqlLogger
import org.jetbrains.exposed.sql.addLogger
Expand Down Expand Up @@ -30,6 +31,12 @@ class Database {
}
}

fun createAccount(email: String, password: String) {
transaction {
AdministratorsRepository.insert(email, password)
}
}

fun setup(logging: Boolean) {
Database().db

Expand Down
19 changes: 9 additions & 10 deletions docs/development-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,22 @@ This short guide focuses on setting up the project using IntelliJ instead of And

1. Install docker and docker-compose
2. `sudo docker-compose rm`
2. `sudo docker-compose build`
2. `sudo docker-compose up --force-recreate`
3. Open Adminer: [http://localhost:5001](http://127.0.0.1:5001/?pgsql=db-postgis&username=postgres&db=ehrenamtskarte)

3. `sudo docker-compose build`
4. `sudo docker-compose up --force-recreate`
5. Open Adminer: [http://localhost:5001](http://127.0.0.1:5001/?pgsql=db-postgis&username=postgres&db=ehrenamtskarte)
The credentials are:

|Property|Value|
|---|---|
|Host (within Docker)|db-postgis|
|Username|postgres|
|Password|postgres|
|Database|ehrenamtskarte|
4. Install JDK8
5. Run the backend: `cd backend && ./gradlew run` or `.\backend\gradlew.bat run` on Windows
6. Take a look at the martin endpoints: [http://localhost:5002/tiles/accepting_stores/index.json](http://localhost:5002/tiles/accepting_stores/index.json) and [http://localhost:5002/tiles/accepting_stores/rpc/index.json](http://localhost:5002/tiles/accepting_stores/rpc/index.json). The data shown on the map is fetched from a hardcoded url and is not using the data from the local martin!
7. Take a look at the style by viewing the test map: [http://localhost:5002](http://localhost:5002)
8. Take a look at the backend: [http://localhost:7000](http://localhost:7000) (The public version is available at api.ehrenamtskarte.app)
6. Install JDK8
7. Run the backend: `cd backend && ./gradlew run --args="execute"` or `.\backend\gradlew.bat run --args="execute"` on Windows
8. Create an admin account using `./gradlew run --args="create-admin <email> <password>"`
maxammann marked this conversation as resolved.
Show resolved Hide resolved
9. Take a look at the martin endpoints: [http://localhost:5002/tiles/accepting_stores/index.json](http://localhost:5002/tiles/accepting_stores/index.json) and [http://localhost:5002/tiles/accepting_stores/rpc/index.json](http://localhost:5002/tiles/accepting_stores/rpc/index.json). The data shown on the map is fetched from a hardcoded url and is not using the data from the local martin!
10. Take a look at the style by viewing the test map: [http://localhost:5002](http://localhost:5002)
11. Take a look at the backend: [http://localhost:7000](http://localhost:7000) (The public version is available at api.ehrenamtskarte.app)

## Dumping and restoring the database through docker

Expand Down
2 changes: 1 addition & 1 deletion docs/graphql_generation.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Queries are specified in [frontend/graphql_queries](../frontend/graphql_queries)

## Generate API Files
1. If schema changed: Generate updated GraphQL schema into [schema.graphql](../frontend/schema.graphql)
Run "Generate GraphQL" in Intellij (or ```./gradlew run --args="--export-api=../frontend/schema.graphql"``` in the backend folder)
Run "Generate GraphQL" in Intellij (or `./gradlew run --args="graphql-export ../specs/backend-api.graphql"` in the backend folder)
2. Run "Generate GraphQL Flutter Client" (or first copy `backend/schema.graphql` to `frontend/schema.graphql` and
run ```flutter pub run build_runner build --delete-conflicting-outputs``` in the `frontend` directory
3. Run "Generate GraphQL React Client" (or `npm generate-graphql` in the `administration` directory)
7 changes: 7 additions & 0 deletions docs/staging-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ ogr2ogr -f "PostgreSQL" PG:"dbname=ehrenamtskarte host='localhost' port='5432' u

After starting `docker-compose` with the staging configuration, the setup should be available at port 80: `docker-compose -f docker-compose.yml -f docker-compose.staging.yml up`.

## Admin Account Creation

It is possible to create an admin account by running:
```bash
./gradlew run --args="create-admin <email> <password>"
```

## Importing EAK data on staging

To import the EAK data using docker, run the following:
Expand Down
2 changes: 1 addition & 1 deletion scripts/eak-backend.service
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ StartLimitBurst=5

[Service]
Type=simple
ExecStart=/opt/ehrenamtskarte/backend/bin/backend
ExecStart=/opt/ehrenamtskarte/backend/bin/backend execute
Restart=on-failure
RestartSec=5s

Expand Down