From d780797e78a1f907f5bb34f2d5ca4d3e3c5fda2d Mon Sep 17 00:00:00 2001 From: Oscar Veldman Date: Fri, 16 Aug 2024 18:10:48 +0200 Subject: [PATCH 01/13] Add postgresql db to docker compose --- src/docker-compose.yml | 45 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/docker-compose.yml b/src/docker-compose.yml index a8c445e..9821f9d 100644 --- a/src/docker-compose.yml +++ b/src/docker-compose.yml @@ -27,6 +27,9 @@ services: build: context: . dockerfile: Server.Controllers.Api.Grpc/Dockerfile + depends_on: + postgres: + condition: service_healthy ports: - "8084:8080" - "8085:8081" @@ -37,6 +40,9 @@ services: build: context: . dockerfile: Server.Controllers.Api.MessageBus/Dockerfile + depends_on: + postgres: + condition: service_healthy ports: - "8086:8080" - "8087:8081" @@ -72,5 +78,42 @@ services: - "5341:80" volumes: - seq-data:/data + postgres: + image: postgres:latest + container_name: postgres + environment: + - POSTGRES_DB=postgres + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=Secret1234 + - PGUSER=postgres + healthcheck: + test: [ "CMD-SHELL", "pg_isready", "-d", "db_prod" ] + interval: 30s + timeout: 60s + retries: 5 + start_period: 80s + ports: + - "5432:5432" + networks: + - postgres + volumes: + - postgres-data:/var/lib/postgresql/data + pgadmin: + image: dpage/pgadmin4:latest + container_name: pgadmin + environment: + - PGADMIN_DEFAULT_EMAIL=test@test.nl + - PGADMIN_DEFAULT_PASSWORD=Secret1234 + ports: + - "9080:80" + - "9081:443" + networks: + - postgres + volumes: + - pgadmin-data:/var/lib/pgadmin volumes: - seq-data: \ No newline at end of file + postgres-data: + pgadmin-data: + seq-data: +networks: + postgres: \ No newline at end of file From bb67d3c0ae4790a81384889a2e71b7cc5382fe35 Mon Sep 17 00:00:00 2001 From: Oscar Veldman Date: Fri, 16 Aug 2024 22:08:01 +0200 Subject: [PATCH 02/13] Start with postgresql in kubernetes --- .../MantaRayPlanCloud/templates/postgres.yaml | 9 ++++++++ docs/setup.md | 21 +++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 deployment/MantaRayPlanCloud/templates/postgres.yaml diff --git a/deployment/MantaRayPlanCloud/templates/postgres.yaml b/deployment/MantaRayPlanCloud/templates/postgres.yaml new file mode 100644 index 0000000..2adcb93 --- /dev/null +++ b/deployment/MantaRayPlanCloud/templates/postgres.yaml @@ -0,0 +1,9 @@ +apiVersion: postgresql.cnpg.io/v1 +kind: Cluster +metadata: + name: postgres + namespace: {{ .Values.namespace }} +spec: + instances: 3 + storage: + size: 1Gi diff --git a/docs/setup.md b/docs/setup.md index ef73991..95ce6fc 100644 --- a/docs/setup.md +++ b/docs/setup.md @@ -2,10 +2,26 @@ This document describes how to setup the Manta Ray Plan in production. ## Kubernetes -1. Download the helm chart to your server. +1. Install kubernetes from our [kubernetes.md](kubernetes.md) guide. +2. Download the helm chart to your server. 2. Change every values in the `environments/values-production.yaml` file to match your requirements. 3. Install or upgrade the cluster +## Postgresql Install +Use this install command to install the postgresql chart: +```shell +helm repo add cnpg https://cloudnative-pg.github.io/charts +helm upgrade --install cnpg \ + --namespace cnpg-system \ + --create-namespace \ + cnpg/cloudnative-pg +``` +## Values-production.yaml +*TODO* + +## Deploy +Deploy now to kubernetes and wait for the pods to be ready. + ## Seq 1. Visit your seq url 2. Navigate to the general settings page @@ -17,6 +33,3 @@ This document describes how to setup the Manta Ray Plan in production. 1. Add a retention policy to match your requirements. At the moment there is no policy in place. 4. Navigate to your account settings page 1. Change your default password - -## Values-production.yaml -*TODO* \ No newline at end of file From c050ebab8dcc832000316d37c84121e447235cb0 Mon Sep 17 00:00:00 2001 From: Oscar Veldman Date: Mon, 19 Aug 2024 21:19:11 +0200 Subject: [PATCH 03/13] Set location of database --- .../environments/values-production.yaml | 7 ++- .../MantaRayPlanCloud/templates/postgres.yaml | 54 ++++++++++++++++++- deployment/MantaRayPlanCloud/values.yaml | 5 ++ docs/setup.md | 34 ++++++++++++ 4 files changed, 98 insertions(+), 2 deletions(-) diff --git a/deployment/MantaRayPlanCloud/environments/values-production.yaml b/deployment/MantaRayPlanCloud/environments/values-production.yaml index 9695984..88770a0 100644 --- a/deployment/MantaRayPlanCloud/environments/values-production.yaml +++ b/deployment/MantaRayPlanCloud/environments/values-production.yaml @@ -27,4 +27,9 @@ logging: enable: true host: external: "seq.mad-world.nl" - firstTimePassword: "QFCKH3NTSBQ5zmsH9DpXTB2YefIavEGJKa4SshKb11AXX8b4o4KPjuo9bd6WBfqDkpxKqzNjaOrCsE49ph369Wx84mIrksnJx5OtRcxAOjff" \ No newline at end of file + firstTimePassword: "QFCKH3NTSBQ5zmsH9DpXTB2YefIavEGJKa4SshKb11AXX8b4o4KPjuo9bd6WBfqDkpxKqzNjaOrCsE49ph369Wx84mIrksnJx5OtRcxAOjff" + +database: + size: + single: 10Gi + combined: 35Gi \ No newline at end of file diff --git a/deployment/MantaRayPlanCloud/templates/postgres.yaml b/deployment/MantaRayPlanCloud/templates/postgres.yaml index 2adcb93..4a0ee3d 100644 --- a/deployment/MantaRayPlanCloud/templates/postgres.yaml +++ b/deployment/MantaRayPlanCloud/templates/postgres.yaml @@ -1,3 +1,34 @@ +apiVersion: hostpathprovisioner.kubevirt.io/v1beta1 +kind: HostPathProvisioner +metadata: + name: hostpath-provisioner + namespace: hostpath-provisioner +spec: + imagePullPolicy: Always + storagePools: + - name: "postgres-pool" + pvcTemplate: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: {{ .Values.database.size.combined }} + path: "/MantaRayPlan/Postgres" + workload: + nodeSelector: + kubernetes.io/os: linux +--- +apiVersion: storage.k8s.io/v1 +kind: StorageClass +metadata: + name: postgres-csi + namespace: hostpath-provisioner +provisioner: kubevirt.io.hostpath-provisioner +reclaimPolicy: Delete +volumeBindingMode: WaitForFirstConsumer +parameters: + storagePool: postgres-pool +--- apiVersion: postgresql.cnpg.io/v1 kind: Cluster metadata: @@ -5,5 +36,26 @@ metadata: namespace: {{ .Values.namespace }} spec: instances: 3 + primaryUpdateStrategy: unsupervised storage: - size: 1Gi + size: {{ .Values.database.size.single }} + pvcTemplate: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: {{ .Values.database.size.single }} + storageClassName: postgres-csi +--- +apiVersion: postgresql.cnpg.io/v1 +kind: ScheduledBackup +metadata: + name: backup-example + namespace: {{ .Values.namespace }} +spec: + schedule: "0 0 0 * * *" + backupOwnerReference: self + cluster: + name: postgres + immediate: true + diff --git a/deployment/MantaRayPlanCloud/values.yaml b/deployment/MantaRayPlanCloud/values.yaml index 5c4e2cc..e542120 100644 --- a/deployment/MantaRayPlanCloud/values.yaml +++ b/deployment/MantaRayPlanCloud/values.yaml @@ -78,3 +78,8 @@ logging: secrets: name: "seq-secrets" firstTimePassword: "QFCKH3NTSBQ5zmsH9DpXTB2YefIavEGJKa4SshKb11AXX8b4o4KPjuo9bd6WBfqDkpxKqzNjaOrCsE49ph369Wx84mIrksnJx5OtRcxAOjff" + +database: + size: + single: 1Gi + combined: 4Gi diff --git a/docs/setup.md b/docs/setup.md index 95ce6fc..7c5363b 100644 --- a/docs/setup.md +++ b/docs/setup.md @@ -16,6 +16,36 @@ helm upgrade --install cnpg \ --create-namespace \ cnpg/cloudnative-pg ``` +As of version 0.11 the hostpath provisioner operator now requires [cert manager](https://github.com/cert-manager/cert-manager) to be installed before deploying the operator. This is because the operator now has a validating webhook that verifies the contents of the CR are valid. +Before deploying the operator, you need to install cert manager: + +```bash +kubectl create -f https://github.com/cert-manager/cert-manager/releases/download/v1.7.1/cert-manager.yaml +``` + +Please ensure the cert manager is fully operational before installing the hostpath provisioner operator: + +```bash +kubectl wait --for=condition=Available -n cert-manager --timeout=120s --all deployments +``` + +Next, you need to create the hostpath provisioner namespace: + +```bash +kubectl create -f https://raw.githubusercontent.com/kubevirt/hostpath-provisioner-operator/main/deploy/namespace.yaml +``` + +Followed by the webhook: +```bash +kubectl create -f https://raw.githubusercontent.com/kubevirt/hostpath-provisioner-operator/main/deploy/webhook.yaml -n hostpath-provisioner +``` + +And then you can create the operator: + +```bash +kubectl create -f https://raw.githubusercontent.com/kubevirt/hostpath-provisioner-operator/main/deploy/operator.yaml -n hostpath-provisioner +``` + ## Values-production.yaml *TODO* @@ -33,3 +63,7 @@ Deploy now to kubernetes and wait for the pods to be ready. 1. Add a retention policy to match your requirements. At the moment there is no policy in place. 4. Navigate to your account settings page 1. Change your default password + +## Reference +[Install Guide Cloud Native PG](https://cloudnative-pg.io/documentation/1.23/installation_upgrade/)\ +[Install Hostpath Provisioner](https://github.com/kubevirt/hostpath-provisioner-operator) \ No newline at end of file From 810bf06206e0c6a4cb7962371f7d6e52756f477e Mon Sep 17 00:00:00 2001 From: Oscar Veldman Date: Tue, 20 Aug 2024 14:05:11 +0200 Subject: [PATCH 04/13] Update pg admin to make it function --- .../environments/values-production.yaml | 7 +- .../MantaRayPlanCloud/templates/ingress.yaml | 13 +++ .../MantaRayPlanCloud/templates/pg-admin.yaml | 88 +++++++++++++++++++ deployment/MantaRayPlanCloud/values.yaml | 9 ++ 4 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 deployment/MantaRayPlanCloud/templates/pg-admin.yaml diff --git a/deployment/MantaRayPlanCloud/environments/values-production.yaml b/deployment/MantaRayPlanCloud/environments/values-production.yaml index 88770a0..313a460 100644 --- a/deployment/MantaRayPlanCloud/environments/values-production.yaml +++ b/deployment/MantaRayPlanCloud/environments/values-production.yaml @@ -32,4 +32,9 @@ logging: database: size: single: 10Gi - combined: 35Gi \ No newline at end of file + combined: 35Gi + +pgadmin: + outsideAccess: + enabled: true + host: "database.mad-world.nl" \ No newline at end of file diff --git a/deployment/MantaRayPlanCloud/templates/ingress.yaml b/deployment/MantaRayPlanCloud/templates/ingress.yaml index a965abd..bc4f3a3 100644 --- a/deployment/MantaRayPlanCloud/templates/ingress.yaml +++ b/deployment/MantaRayPlanCloud/templates/ingress.yaml @@ -19,6 +19,7 @@ spec: - {{ .Values.viewer.bff.host }} - {{ .Values.viewer.web.host }} - {{ .Values.logging.seq.host.external }} + - {{ .Values.pgadmin.host }} secretName: madworld-tls {{ end }} rules: @@ -83,4 +84,16 @@ spec: name: {{ .Values.logging.seq.loadBalancer }} port: number: 80 + {{ end }} + {{- if .Values.pgadmin.outsideAccess.enabled }} + - host: {{ .Values.pgadmin.host }} + http: + paths: + - path: / + pathType: Prefix + backend: + service: + name: {{ .Values.pgadmin.loadBalancer }} + port: + number: 80 {{ end }} \ No newline at end of file diff --git a/deployment/MantaRayPlanCloud/templates/pg-admin.yaml b/deployment/MantaRayPlanCloud/templates/pg-admin.yaml new file mode 100644 index 0000000..82fefc5 --- /dev/null +++ b/deployment/MantaRayPlanCloud/templates/pg-admin.yaml @@ -0,0 +1,88 @@ +--- +kind: PersistentVolume +apiVersion: v1 +metadata: + name: pgadmin-pv-volume + namespace: {{ .Values.namespace }} + labels: + type: local + app: {{ .Values.pgadmin.app }} +spec: + storageClassName: manual + capacity: + storage: 5Gi + volumeMode: Filesystem + accessModes: + - ReadWriteMany + hostPath: + path: "/MantaRayPlan/Pgadmin" +--- +kind: PersistentVolumeClaim +apiVersion: v1 +metadata: + name: pgadmin-pv-claim + namespace: {{ .Values.namespace }} + labels: + app: {{ .Values.pgadmin.app }} +spec: + storageClassName: manual + accessModes: + - ReadWriteMany + resources: + requests: + storage: 5Gi +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ .Values.pgadmin.deployment }} + namespace: {{ .Values.namespace }} + labels: + app: {{ .Values.pgadmin.app }} + name: {{ .Values.pgadmin.name }} +spec: + replicas: 1 + selector: + matchLabels: + app: {{ .Values.pgadmin.app }} + task: {{ .Values.pgadmin.name }} + template: + metadata: + labels: + app: {{ .Values.pgadmin.app }} + task: {{ .Values.pgadmin.name }} + spec: + containers: + - name: {{ .Values.pgadmin.app }} + image: "dpage/pgadmin4:latest" + imagePullPolicy: Always + env: + - name: PGADMIN_DEFAULT_EMAIL + value: "test@test.nl" + - name: PGADMIN_DEFAULT_PASSWORD + value: "Secret1234" + ports: + - containerPort: 80 + volumeMounts: + - mountPath: "/var/lib/pgadmin" + name: pgadminvolume + volumes: + - name: pgadminvolume + persistentVolumeClaim: + claimName: pgadmin-pv-claim +--- +apiVersion: v1 +kind: Service +metadata: + name: {{ .Values.pgadmin.loadBalancer }} + namespace: {{ .Values.namespace }} +spec: + selector: + app: {{ .Values.pgadmin.app }} + task: {{ .Values.pgadmin.name }} + ports: + - protocol: TCP + name: http + port: 80 + targetPort: 80 + type: LoadBalancer \ No newline at end of file diff --git a/deployment/MantaRayPlanCloud/values.yaml b/deployment/MantaRayPlanCloud/values.yaml index e542120..843481b 100644 --- a/deployment/MantaRayPlanCloud/values.yaml +++ b/deployment/MantaRayPlanCloud/values.yaml @@ -83,3 +83,12 @@ database: size: single: 1Gi combined: 4Gi + +pgadmin: + outsideAccess: + enabled: true + app: pgadmin + deployment: pgadmin-deployment + loadBalancer: "pgadmin-loadbalancer" + name: pgadmin + host: "database.mantarayplan" From 31478dab7ee0c3910ef5bdb478f4258dbbaf532f Mon Sep 17 00:00:00 2001 From: Oscar Veldman Date: Wed, 21 Aug 2024 10:44:00 +0200 Subject: [PATCH 05/13] Connect pgadmin with postgres --- .../MantaRayPlanCloud/templates/pg-admin.yaml | 7 +++-- .../MantaRayPlanCloud/templates/postgres.yaml | 28 +++++++++++-------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/deployment/MantaRayPlanCloud/templates/pg-admin.yaml b/deployment/MantaRayPlanCloud/templates/pg-admin.yaml index 82fefc5..7f8d831 100644 --- a/deployment/MantaRayPlanCloud/templates/pg-admin.yaml +++ b/deployment/MantaRayPlanCloud/templates/pg-admin.yaml @@ -13,7 +13,7 @@ spec: storage: 5Gi volumeMode: Filesystem accessModes: - - ReadWriteMany + - ReadWriteOnce hostPath: path: "/MantaRayPlan/Pgadmin" --- @@ -27,7 +27,7 @@ metadata: spec: storageClassName: manual accessModes: - - ReadWriteMany + - ReadWriteOnce resources: requests: storage: 5Gi @@ -56,6 +56,9 @@ spec: - name: {{ .Values.pgadmin.app }} image: "dpage/pgadmin4:latest" imagePullPolicy: Always + securityContext: + runAsUser: 0 + runAsGroup: 0 env: - name: PGADMIN_DEFAULT_EMAIL value: "test@test.nl" diff --git a/deployment/MantaRayPlanCloud/templates/postgres.yaml b/deployment/MantaRayPlanCloud/templates/postgres.yaml index 4a0ee3d..46dbf69 100644 --- a/deployment/MantaRayPlanCloud/templates/postgres.yaml +++ b/deployment/MantaRayPlanCloud/templates/postgres.yaml @@ -1,3 +1,13 @@ +apiVersion: v1 +data: + username: YXBw + password: U2VjcmV0MTIzNA== +kind: Secret +metadata: + name: postgres-secret + namespace: {{ .Values.namespace }} +type: kubernetes.io/basic-auth +--- apiVersion: hostpathprovisioner.kubevirt.io/v1beta1 kind: HostPathProvisioner metadata: @@ -37,6 +47,12 @@ metadata: spec: instances: 3 primaryUpdateStrategy: unsupervised + bootstrap: + initdb: + database: maintenance + owner: app + secret: + name: postgres-secret storage: size: {{ .Values.database.size.single }} pvcTemplate: @@ -46,16 +62,4 @@ spec: requests: storage: {{ .Values.database.size.single }} storageClassName: postgres-csi ---- -apiVersion: postgresql.cnpg.io/v1 -kind: ScheduledBackup -metadata: - name: backup-example - namespace: {{ .Values.namespace }} -spec: - schedule: "0 0 0 * * *" - backupOwnerReference: self - cluster: - name: postgres - immediate: true From 02e7cae0e9c027a33c9c4ac61f09bb16da9d61f6 Mon Sep 17 00:00:00 2001 From: Oscar Veldman Date: Wed, 21 Aug 2024 17:31:09 +0200 Subject: [PATCH 06/13] Add secret values in the yaml files --- .../environments/values-production.yaml | 3 +- .../MantaRayPlanCloud/templates/pg-admin.yaml | 28 +++++++++++++++++-- .../MantaRayPlanCloud/templates/postgres.yaml | 20 +++++++++---- deployment/MantaRayPlanCloud/values.yaml | 5 ++++ 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/deployment/MantaRayPlanCloud/environments/values-production.yaml b/deployment/MantaRayPlanCloud/environments/values-production.yaml index 313a460..ecde643 100644 --- a/deployment/MantaRayPlanCloud/environments/values-production.yaml +++ b/deployment/MantaRayPlanCloud/environments/values-production.yaml @@ -37,4 +37,5 @@ database: pgadmin: outsideAccess: enabled: true - host: "database.mad-world.nl" \ No newline at end of file + host: "database.mad-world.nl" + username: admin@mad-world.nl \ No newline at end of file diff --git a/deployment/MantaRayPlanCloud/templates/pg-admin.yaml b/deployment/MantaRayPlanCloud/templates/pg-admin.yaml index 7f8d831..0ba0e83 100644 --- a/deployment/MantaRayPlanCloud/templates/pg-admin.yaml +++ b/deployment/MantaRayPlanCloud/templates/pg-admin.yaml @@ -1,3 +1,21 @@ +{{- $pgadminSecrets := (lookup "v1" "Secret" .Values.namespace .Values.pgadmin.name ) -}} +apiVersion: v1 +kind: Secret +metadata: + name: {{ .Values.pgadmin.secret.name }} + namespace: {{ .Values.namespace }} +type: kubernetes.io/basic-auth +data: + username: {{ .Values.pgadmin.username | b64enc }} +{{- if and $pgadminSecrets $pgadminSecrets.data }} + {{- if $pgadminSecrets.data.password }} + password: {{ $pgadminSecrets.data.password }} + {{- else }} + password: {{ "nonExistingKey1234" | b64enc }} + {{- end }} +{{- else }} + password: {{ "nonExistingKey1234" | b64enc }} +{{- end }} --- kind: PersistentVolume apiVersion: v1 @@ -61,9 +79,15 @@ spec: runAsGroup: 0 env: - name: PGADMIN_DEFAULT_EMAIL - value: "test@test.nl" + valueFrom: + secretKeyRef: + name: {{ .Values.pgadmin.secret.name }} + key: username - name: PGADMIN_DEFAULT_PASSWORD - value: "Secret1234" + valueFrom: + secretKeyRef: + name: {{ .Values.pgadmin.secret.name }} + key: password ports: - containerPort: 80 volumeMounts: diff --git a/deployment/MantaRayPlanCloud/templates/postgres.yaml b/deployment/MantaRayPlanCloud/templates/postgres.yaml index 46dbf69..c94601a 100644 --- a/deployment/MantaRayPlanCloud/templates/postgres.yaml +++ b/deployment/MantaRayPlanCloud/templates/postgres.yaml @@ -1,12 +1,21 @@ +{{- $postgresSecrets := (lookup "v1" "Secret" .Values.namespace .Values.database.name ) -}} apiVersion: v1 -data: - username: YXBw - password: U2VjcmV0MTIzNA== kind: Secret metadata: name: postgres-secret namespace: {{ .Values.namespace }} type: kubernetes.io/basic-auth +data: + username: {{ "app" | b64enc }} +{{- if and $postgresSecrets $postgresSecrets.data }} + {{- if $postgresSecrets.data.password }} + password: {{ $postgresSecrets.data.password }} + {{- else }} + password: {{ "nonExistingKey1234" | b64enc }} + {{- end }} +{{- else }} + password: {{ "nonExistingKey1234" | b64enc }} +{{- end }} --- apiVersion: hostpathprovisioner.kubevirt.io/v1beta1 kind: HostPathProvisioner @@ -39,10 +48,11 @@ volumeBindingMode: WaitForFirstConsumer parameters: storagePool: postgres-pool --- +{{- if .Values.database.enabled }} apiVersion: postgresql.cnpg.io/v1 kind: Cluster metadata: - name: postgres + name: {{ .Values.database.name }} namespace: {{ .Values.namespace }} spec: instances: 3 @@ -62,4 +72,4 @@ spec: requests: storage: {{ .Values.database.size.single }} storageClassName: postgres-csi - +{{ end }} \ No newline at end of file diff --git a/deployment/MantaRayPlanCloud/values.yaml b/deployment/MantaRayPlanCloud/values.yaml index 843481b..23b3c3f 100644 --- a/deployment/MantaRayPlanCloud/values.yaml +++ b/deployment/MantaRayPlanCloud/values.yaml @@ -80,6 +80,8 @@ logging: firstTimePassword: "QFCKH3NTSBQ5zmsH9DpXTB2YefIavEGJKa4SshKb11AXX8b4o4KPjuo9bd6WBfqDkpxKqzNjaOrCsE49ph369Wx84mIrksnJx5OtRcxAOjff" database: + enabled: true + name: postgres size: single: 1Gi combined: 4Gi @@ -91,4 +93,7 @@ pgadmin: deployment: pgadmin-deployment loadBalancer: "pgadmin-loadbalancer" name: pgadmin + username: test@test.nl host: "database.mantarayplan" + secret: + name: pgadmin-secret From 501f33754fd26708a8c4740a90f01618499a4513 Mon Sep 17 00:00:00 2001 From: Oscar Veldman Date: Wed, 21 Aug 2024 17:32:31 +0200 Subject: [PATCH 07/13] Update ReadMe --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 64abe62..1cfa957 100644 --- a/README.md +++ b/README.md @@ -77,5 +77,6 @@ Want to see the applications in action? Check out the links below: ## Acknowledgements I would like to thank the following sources and individuals for their templates, which I used in this application: - [Bootstrap 5 theme - Elegant](https://themewagon.com/themes/free-bootstrap-5-html-5-admin-dashboard-website-template-elegant/) - Made by: Freebibuges +- [Cloud Native PG](https://cloudnative-pg.io/) - Made by: The CloudNativePG Contributors. Your work has been incredibly helpful, and I greatly appreciate your contributions and generosity in sharing these resources. \ No newline at end of file From a5b1336db739b64eeb6178aeb8a11b7caec9b79c Mon Sep 17 00:00:00 2001 From: Oscar Veldman Date: Wed, 21 Aug 2024 21:26:46 +0200 Subject: [PATCH 08/13] Start postgres in C# --- src/Directory.Packages.props | 7 +++++-- src/MadWorldNL.MantaRayPlan.sln | 7 +++++++ src/Server.Controllers.Api.Grpc/Api.Grpc.csproj | 8 ++++++++ .../Api.MessageBus.csproj | 11 +++++++++++ src/Server.DataAccess.DataBase/DataBase.csproj | 16 ++++++++++++++++ 5 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 src/Server.DataAccess.DataBase/DataBase.csproj diff --git a/src/Directory.Packages.props b/src/Directory.Packages.props index b6e8661..6d2c1f0 100644 --- a/src/Directory.Packages.props +++ b/src/Directory.Packages.props @@ -2,15 +2,18 @@ + + + - + @@ -23,7 +26,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + all diff --git a/src/MadWorldNL.MantaRayPlan.sln b/src/MadWorldNL.MantaRayPlan.sln index 8292f1d..7d35a2b 100644 --- a/src/MadWorldNL.MantaRayPlan.sln +++ b/src/MadWorldNL.MantaRayPlan.sln @@ -106,6 +106,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Clients.Viewer.Bff.Integrat EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MadWorldNL.AspNetCore", "MadWorldNL.AspNetCore\MadWorldNL.AspNetCore.csproj", "{0A7B29AB-26C2-450E-BDDD-BA9630102056}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataBase", "Server.DataAccess.DataBase\DataBase.csproj", "{83D6CB96-9E05-446D-8C2D-4D8AFD83DF66}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -144,6 +146,7 @@ Global {84EF736D-8B49-4B8B-8319-E28B71B483D2} = {3A4CBE5B-7767-48D2-888C-B26D8DE50F1B} {8E9BB031-53C2-4596-9263-9B6E2DE047AD} = {766C958F-70F5-4901-80C7-CA586DB93AFC} {0A7B29AB-26C2-450E-BDDD-BA9630102056} = {74D4AB3E-1584-45CB-B683-3507C4CFE299} + {83D6CB96-9E05-446D-8C2D-4D8AFD83DF66} = {B2F8D186-415B-4F36-BCDC-D13A1159B8C2} EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {C3AFAE1F-DDC9-4DB1-931D-1B934F9C9DF7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU @@ -202,5 +205,9 @@ Global {0A7B29AB-26C2-450E-BDDD-BA9630102056}.Debug|Any CPU.Build.0 = Debug|Any CPU {0A7B29AB-26C2-450E-BDDD-BA9630102056}.Release|Any CPU.ActiveCfg = Release|Any CPU {0A7B29AB-26C2-450E-BDDD-BA9630102056}.Release|Any CPU.Build.0 = Release|Any CPU + {83D6CB96-9E05-446D-8C2D-4D8AFD83DF66}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {83D6CB96-9E05-446D-8C2D-4D8AFD83DF66}.Debug|Any CPU.Build.0 = Debug|Any CPU + {83D6CB96-9E05-446D-8C2D-4D8AFD83DF66}.Release|Any CPU.ActiveCfg = Release|Any CPU + {83D6CB96-9E05-446D-8C2D-4D8AFD83DF66}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/src/Server.Controllers.Api.Grpc/Api.Grpc.csproj b/src/Server.Controllers.Api.Grpc/Api.Grpc.csproj index caa7074..e48a918 100644 --- a/src/Server.Controllers.Api.Grpc/Api.Grpc.csproj +++ b/src/Server.Controllers.Api.Grpc/Api.Grpc.csproj @@ -12,6 +12,14 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Server.Controllers.Api.MessageBus/Api.MessageBus.csproj b/src/Server.Controllers.Api.MessageBus/Api.MessageBus.csproj index 6e48df8..02b69da 100644 --- a/src/Server.Controllers.Api.MessageBus/Api.MessageBus.csproj +++ b/src/Server.Controllers.Api.MessageBus/Api.MessageBus.csproj @@ -20,4 +20,15 @@ + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/src/Server.DataAccess.DataBase/DataBase.csproj b/src/Server.DataAccess.DataBase/DataBase.csproj new file mode 100644 index 0000000..3d7c4b4 --- /dev/null +++ b/src/Server.DataAccess.DataBase/DataBase.csproj @@ -0,0 +1,16 @@ + + + + net8.0 + MadWorldNL.MantaRayPlan.DataAccess.Database + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + From 7e9351adccee99bfee8ed6c98fa474c19ab4b2cf Mon Sep 17 00:00:00 2001 From: Oscar Veldman Date: Thu, 22 Aug 2024 21:05:18 +0200 Subject: [PATCH 09/13] Start with database in C# --- docs/database.md | 22 ++++++++++++ src/MadWorldNL.MantaRayPlan.sln | 15 ++++++++ .../DataBase.csproj | 12 +++++++ .../MantaRayPlanDbContext.cs | 20 +++++++++++ .../MessageBuses/MessageBusRepository.cs | 35 +++++++++++++++++++ ...MessageBusStatusEntityTypeConfiguration.cs | 12 +++++++ src/Server.Logic.Domain/Domain.csproj | 8 +++++ .../MessageBuses/IMessageBusRepository.cs | 8 +++++ .../MessageBuses/MessageBusStatus.cs | 12 +++++++ src/Server.Logic.Functions/Functions.csproj | 9 +++++ 10 files changed, 153 insertions(+) create mode 100644 docs/database.md create mode 100644 src/Server.DataAccess.DataBase/MantaRayPlanDbContext.cs create mode 100644 src/Server.DataAccess.DataBase/MessageBuses/MessageBusRepository.cs create mode 100644 src/Server.DataAccess.DataBase/MessageBuses/MessageBusStatusEntityTypeConfiguration.cs create mode 100644 src/Server.Logic.Domain/Domain.csproj create mode 100644 src/Server.Logic.Domain/MessageBuses/IMessageBusRepository.cs create mode 100644 src/Server.Logic.Domain/MessageBuses/MessageBusStatus.cs create mode 100644 src/Server.Logic.Functions/Functions.csproj diff --git a/docs/database.md b/docs/database.md new file mode 100644 index 0000000..ec3a139 --- /dev/null +++ b/docs/database.md @@ -0,0 +1,22 @@ +# Dotnet Database +## Pre-requisites +```bash +dotnet tool install --global dotnet-ef +# If already installed +dotnet tool update --global dotnet-ef +``` +## Migrations +### Create Migration +Default add migrations command: +```bash +dotnet ef migrations add InitialCreate +``` + +When the migrations is saved in another project: +```bash +dotnet ef migrations add InitialCreate --context MantaRayPlanDbContext --project ../Server.DataAccess.Database -o ../Server.DataAccess.Database/Migrations +``` +### Remove Migration +```bash +dotnet ef migrations remove +``` \ No newline at end of file diff --git a/src/MadWorldNL.MantaRayPlan.sln b/src/MadWorldNL.MantaRayPlan.sln index 7d35a2b..1aa941a 100644 --- a/src/MadWorldNL.MantaRayPlan.sln +++ b/src/MadWorldNL.MantaRayPlan.sln @@ -84,6 +84,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{D7968674-C ..\docs\dns.md = ..\docs\dns.md ..\docs\server.md = ..\docs\server.md ..\docs\setup.md = ..\docs\setup.md + ..\docs\database.md = ..\docs\database.md EndProjectSection EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DefaultStyle", "Clients.DefaultStyle\DefaultStyle.csproj", "{F97B03DB-2196-4E50-9211-C4D65A1D1A56}" @@ -108,6 +109,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MadWorldNL.AspNetCore", "Ma EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataBase", "Server.DataAccess.DataBase\DataBase.csproj", "{83D6CB96-9E05-446D-8C2D-4D8AFD83DF66}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Domain", "Server.Logic.Domain\Domain.csproj", "{1803C1EF-6193-4CE5-A37A-16F920F2388D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Functions", "Server.Logic.Functions\Functions.csproj", "{3952C3C4-FB97-4441-9608-A5B7C3BC4E53}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -147,6 +152,8 @@ Global {8E9BB031-53C2-4596-9263-9B6E2DE047AD} = {766C958F-70F5-4901-80C7-CA586DB93AFC} {0A7B29AB-26C2-450E-BDDD-BA9630102056} = {74D4AB3E-1584-45CB-B683-3507C4CFE299} {83D6CB96-9E05-446D-8C2D-4D8AFD83DF66} = {B2F8D186-415B-4F36-BCDC-D13A1159B8C2} + {1803C1EF-6193-4CE5-A37A-16F920F2388D} = {A7B2EEF8-045E-4473-9DE2-6452F5E14CE3} + {3952C3C4-FB97-4441-9608-A5B7C3BC4E53} = {A7B2EEF8-045E-4473-9DE2-6452F5E14CE3} EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {C3AFAE1F-DDC9-4DB1-931D-1B934F9C9DF7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU @@ -209,5 +216,13 @@ Global {83D6CB96-9E05-446D-8C2D-4D8AFD83DF66}.Debug|Any CPU.Build.0 = Debug|Any CPU {83D6CB96-9E05-446D-8C2D-4D8AFD83DF66}.Release|Any CPU.ActiveCfg = Release|Any CPU {83D6CB96-9E05-446D-8C2D-4D8AFD83DF66}.Release|Any CPU.Build.0 = Release|Any CPU + {1803C1EF-6193-4CE5-A37A-16F920F2388D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1803C1EF-6193-4CE5-A37A-16F920F2388D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1803C1EF-6193-4CE5-A37A-16F920F2388D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1803C1EF-6193-4CE5-A37A-16F920F2388D}.Release|Any CPU.Build.0 = Release|Any CPU + {3952C3C4-FB97-4441-9608-A5B7C3BC4E53}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3952C3C4-FB97-4441-9608-A5B7C3BC4E53}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3952C3C4-FB97-4441-9608-A5B7C3BC4E53}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3952C3C4-FB97-4441-9608-A5B7C3BC4E53}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/src/Server.DataAccess.DataBase/DataBase.csproj b/src/Server.DataAccess.DataBase/DataBase.csproj index 3d7c4b4..ed55981 100644 --- a/src/Server.DataAccess.DataBase/DataBase.csproj +++ b/src/Server.DataAccess.DataBase/DataBase.csproj @@ -13,4 +13,16 @@ + + + + + + + + + + + + diff --git a/src/Server.DataAccess.DataBase/MantaRayPlanDbContext.cs b/src/Server.DataAccess.DataBase/MantaRayPlanDbContext.cs new file mode 100644 index 0000000..9f83361 --- /dev/null +++ b/src/Server.DataAccess.DataBase/MantaRayPlanDbContext.cs @@ -0,0 +1,20 @@ +using MadWorldNL.MantaRayPlan.MessageBuses; +using Microsoft.EntityFrameworkCore; + +namespace MadWorldNL.MantaRayPlan; + +public class MantaRayPlanDbContext : DbContext +{ + public MantaRayPlanDbContext(DbContextOptions options) : base(options) + { + } + + public DbSet MessageBusStatus { get; set; } = null!; + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + + modelBuilder.ApplyConfiguration( new MessageBusStatusEntityTypeConfiguration()); + } +} \ No newline at end of file diff --git a/src/Server.DataAccess.DataBase/MessageBuses/MessageBusRepository.cs b/src/Server.DataAccess.DataBase/MessageBuses/MessageBusRepository.cs new file mode 100644 index 0000000..1aeed89 --- /dev/null +++ b/src/Server.DataAccess.DataBase/MessageBuses/MessageBusRepository.cs @@ -0,0 +1,35 @@ +using Microsoft.EntityFrameworkCore; + +namespace MadWorldNL.MantaRayPlan.MessageBuses; + +public class MessageBusRepository : IMessageBusRepository +{ + private readonly MantaRayPlanDbContext _dbContext; + + public MessageBusRepository(MantaRayPlanDbContext dbContext) + { + _dbContext = dbContext; + } + + public async Task CreateAsync(MessageBusStatus status) + { + await _dbContext.MessageBusStatus + .AddAsync(status); + + await _dbContext.SaveChangesAsync(); + } + + public async Task FindStatusAsync() + { + return await _dbContext.MessageBusStatus + .LastOrDefaultAsync(); + } + + public async Task UpdateAsync(MessageBusStatus status) + { + _dbContext.MessageBusStatus + .Update(status); + + await _dbContext.SaveChangesAsync(); + } +} \ No newline at end of file diff --git a/src/Server.DataAccess.DataBase/MessageBuses/MessageBusStatusEntityTypeConfiguration.cs b/src/Server.DataAccess.DataBase/MessageBuses/MessageBusStatusEntityTypeConfiguration.cs new file mode 100644 index 0000000..c48ed58 --- /dev/null +++ b/src/Server.DataAccess.DataBase/MessageBuses/MessageBusStatusEntityTypeConfiguration.cs @@ -0,0 +1,12 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace MadWorldNL.MantaRayPlan.MessageBuses; + +public class MessageBusStatusEntityTypeConfiguration : IEntityTypeConfiguration +{ + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(c => c.Id); + } +} \ No newline at end of file diff --git a/src/Server.Logic.Domain/Domain.csproj b/src/Server.Logic.Domain/Domain.csproj new file mode 100644 index 0000000..3b4344d --- /dev/null +++ b/src/Server.Logic.Domain/Domain.csproj @@ -0,0 +1,8 @@ + + + + net8.0 + MadWorldNL.MantaRayPlan.Logic.Domain + + + diff --git a/src/Server.Logic.Domain/MessageBuses/IMessageBusRepository.cs b/src/Server.Logic.Domain/MessageBuses/IMessageBusRepository.cs new file mode 100644 index 0000000..c367283 --- /dev/null +++ b/src/Server.Logic.Domain/MessageBuses/IMessageBusRepository.cs @@ -0,0 +1,8 @@ +namespace MadWorldNL.MantaRayPlan.MessageBuses; + +public interface IMessageBusRepository +{ + Task CreateAsync(MessageBusStatus status); + Task FindStatusAsync(); + Task UpdateAsync(MessageBusStatus status); +} \ No newline at end of file diff --git a/src/Server.Logic.Domain/MessageBuses/MessageBusStatus.cs b/src/Server.Logic.Domain/MessageBuses/MessageBusStatus.cs new file mode 100644 index 0000000..c599f1c --- /dev/null +++ b/src/Server.Logic.Domain/MessageBuses/MessageBusStatus.cs @@ -0,0 +1,12 @@ +namespace MadWorldNL.MantaRayPlan.MessageBuses; + +public class MessageBusStatus +{ + public int Id { get; private set; } + public int Count { get; private set; } + + public void IncrementCount() + { + Count++; + } +} \ No newline at end of file diff --git a/src/Server.Logic.Functions/Functions.csproj b/src/Server.Logic.Functions/Functions.csproj new file mode 100644 index 0000000..3a63532 --- /dev/null +++ b/src/Server.Logic.Functions/Functions.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + From b7e4d7e3e0730789b7b9ac4f65f4f2ea6a2c89f3 Mon Sep 17 00:00:00 2001 From: Oscar Veldman Date: Thu, 22 Aug 2024 22:08:25 +0200 Subject: [PATCH 10/13] Add migrations postgres in C# --- src/Directory.Packages.props | 2 + src/MadWorldNL.MantaRayPlan.sln | 14 +++--- .../Api.Grpc.csproj | 1 + src/Server.Controllers.Api.Grpc/Program.cs | 7 ++- .../appsettings.Development.json | 7 +++ .../appsettings.json | 7 +++ .../Api.MessageBus.IntegrationTests.csproj | 0 .../HealthCheckTests.cs | 0 .../Api.MessageBus.csproj | 1 + .../Program.cs | 6 +++ .../appsettings.Development.json | 7 +++ .../appsettings.json | 7 +++ .../DataBase.csproj | 2 + .../DatabaseOptions.cs | 14 ++++++ .../ApplicationBuilderExtensions.cs | 14 ++++++ .../HostApplicationBuilderExtensions.cs | 25 +++++++++++ .../20240822194557_InitialCreate.Designer.cs | 45 +++++++++++++++++++ .../20240822194557_InitialCreate.cs | 35 +++++++++++++++ .../MantaRayPlanDbContextModelSnapshot.cs | 42 +++++++++++++++++ .../Application/OptionNotFoundException.cs | 10 +++++ 20 files changed, 237 insertions(+), 9 deletions(-) rename src/{Server.Controller.Api.MessageBus.IntegrationTests => Server.Controllers.Api.MessageBus.IntegrationTests}/Api.MessageBus.IntegrationTests.csproj (100%) rename src/{Server.Controller.Api.MessageBus.IntegrationTests => Server.Controllers.Api.MessageBus.IntegrationTests}/HealthCheckTests.cs (100%) create mode 100644 src/Server.DataAccess.DataBase/DatabaseOptions.cs create mode 100644 src/Server.DataAccess.DataBase/Extensions/ApplicationBuilderExtensions.cs create mode 100644 src/Server.DataAccess.DataBase/Extensions/HostApplicationBuilderExtensions.cs create mode 100644 src/Server.DataAccess.DataBase/Migrations/20240822194557_InitialCreate.Designer.cs create mode 100644 src/Server.DataAccess.DataBase/Migrations/20240822194557_InitialCreate.cs create mode 100644 src/Server.DataAccess.DataBase/Migrations/MantaRayPlanDbContextModelSnapshot.cs create mode 100644 src/Server.Logic.Domain/Application/OptionNotFoundException.cs diff --git a/src/Directory.Packages.props b/src/Directory.Packages.props index 6d2c1f0..d729b0b 100644 --- a/src/Directory.Packages.props +++ b/src/Directory.Packages.props @@ -4,7 +4,9 @@ + + diff --git a/src/MadWorldNL.MantaRayPlan.sln b/src/MadWorldNL.MantaRayPlan.sln index 1aa941a..4be719d 100644 --- a/src/MadWorldNL.MantaRayPlan.sln +++ b/src/MadWorldNL.MantaRayPlan.sln @@ -95,8 +95,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Api.MessageBus", "Server.Co EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Api.Grpc.IntegrationTests", "Server.Controllers.Api.Grpc.IntegrationTests\Api.Grpc.IntegrationTests.csproj", "{2312404C-3D81-4942-84E9-1BEE99BC92BA}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Api.MessageBus.IntegrationTests", "Server.Controller.Api.MessageBus.IntegrationTests\Api.MessageBus.IntegrationTests.csproj", "{CBD2D0B8-49D0-4951-9511-74152FEC8A3F}" -EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Admin", "Admin", "{3A4CBE5B-7767-48D2-888C-B26D8DE50F1B}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Viewer", "Viewer", "{766C958F-70F5-4901-80C7-CA586DB93AFC}" @@ -113,6 +111,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Domain", "Server.Logic.Doma EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Functions", "Server.Logic.Functions\Functions.csproj", "{3952C3C4-FB97-4441-9608-A5B7C3BC4E53}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Api.MessageBus.IntegrationTests", "Server.Controllers.Api.MessageBus.IntegrationTests\Api.MessageBus.IntegrationTests.csproj", "{78883E90-1F9F-4FA4-A1BC-573AF4AF432B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -145,7 +145,6 @@ Global {2FFBF41F-559A-4EBC-9E2D-746FA6CFCEF6} = {BBAAA2F8-0C03-4EAF-B64F-B326EB7E72B4} {92E686EE-A916-494A-A6E3-CC7B89E12E6A} = {D3B456F8-34CA-4F60-8BFC-4794EED8D9A7} {2312404C-3D81-4942-84E9-1BEE99BC92BA} = {FCC26FAC-D27A-4E9C-97AB-225E3131FA52} - {CBD2D0B8-49D0-4951-9511-74152FEC8A3F} = {FCC26FAC-D27A-4E9C-97AB-225E3131FA52} {3A4CBE5B-7767-48D2-888C-B26D8DE50F1B} = {A8D5940E-5887-46F8-B85A-5210A2E2A2FC} {766C958F-70F5-4901-80C7-CA586DB93AFC} = {A8D5940E-5887-46F8-B85A-5210A2E2A2FC} {84EF736D-8B49-4B8B-8319-E28B71B483D2} = {3A4CBE5B-7767-48D2-888C-B26D8DE50F1B} @@ -154,6 +153,7 @@ Global {83D6CB96-9E05-446D-8C2D-4D8AFD83DF66} = {B2F8D186-415B-4F36-BCDC-D13A1159B8C2} {1803C1EF-6193-4CE5-A37A-16F920F2388D} = {A7B2EEF8-045E-4473-9DE2-6452F5E14CE3} {3952C3C4-FB97-4441-9608-A5B7C3BC4E53} = {A7B2EEF8-045E-4473-9DE2-6452F5E14CE3} + {78883E90-1F9F-4FA4-A1BC-573AF4AF432B} = {FCC26FAC-D27A-4E9C-97AB-225E3131FA52} EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {C3AFAE1F-DDC9-4DB1-931D-1B934F9C9DF7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU @@ -196,10 +196,6 @@ Global {2312404C-3D81-4942-84E9-1BEE99BC92BA}.Debug|Any CPU.Build.0 = Debug|Any CPU {2312404C-3D81-4942-84E9-1BEE99BC92BA}.Release|Any CPU.ActiveCfg = Release|Any CPU {2312404C-3D81-4942-84E9-1BEE99BC92BA}.Release|Any CPU.Build.0 = Release|Any CPU - {CBD2D0B8-49D0-4951-9511-74152FEC8A3F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {CBD2D0B8-49D0-4951-9511-74152FEC8A3F}.Debug|Any CPU.Build.0 = Debug|Any CPU - {CBD2D0B8-49D0-4951-9511-74152FEC8A3F}.Release|Any CPU.ActiveCfg = Release|Any CPU - {CBD2D0B8-49D0-4951-9511-74152FEC8A3F}.Release|Any CPU.Build.0 = Release|Any CPU {84EF736D-8B49-4B8B-8319-E28B71B483D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {84EF736D-8B49-4B8B-8319-E28B71B483D2}.Debug|Any CPU.Build.0 = Debug|Any CPU {84EF736D-8B49-4B8B-8319-E28B71B483D2}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -224,5 +220,9 @@ Global {3952C3C4-FB97-4441-9608-A5B7C3BC4E53}.Debug|Any CPU.Build.0 = Debug|Any CPU {3952C3C4-FB97-4441-9608-A5B7C3BC4E53}.Release|Any CPU.ActiveCfg = Release|Any CPU {3952C3C4-FB97-4441-9608-A5B7C3BC4E53}.Release|Any CPU.Build.0 = Release|Any CPU + {78883E90-1F9F-4FA4-A1BC-573AF4AF432B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {78883E90-1F9F-4FA4-A1BC-573AF4AF432B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {78883E90-1F9F-4FA4-A1BC-573AF4AF432B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {78883E90-1F9F-4FA4-A1BC-573AF4AF432B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/src/Server.Controllers.Api.Grpc/Api.Grpc.csproj b/src/Server.Controllers.Api.Grpc/Api.Grpc.csproj index e48a918..3bce6f5 100644 --- a/src/Server.Controllers.Api.Grpc/Api.Grpc.csproj +++ b/src/Server.Controllers.Api.Grpc/Api.Grpc.csproj @@ -34,6 +34,7 @@ + diff --git a/src/Server.Controllers.Api.Grpc/Program.cs b/src/Server.Controllers.Api.Grpc/Program.cs index e9fdd9c..fea3082 100644 --- a/src/Server.Controllers.Api.Grpc/Program.cs +++ b/src/Server.Controllers.Api.Grpc/Program.cs @@ -1,14 +1,17 @@ +using MadWorldNL.MantaRayPlan.Extensions; using MadWorldNL.MantaRayPlan.OpenTelemetry; using Server.Controllers.Api.Grpc.Services; var builder = WebApplication.CreateBuilder(args); -var openTelemetryConfig = builder.Configuration.GetSection(OpenTelemetryConfig.Key).Get() ?? - new OpenTelemetryConfig(); +var openTelemetryConfig = builder.Configuration.GetSection(OpenTelemetryConfig.Key).Get() + ?? new OpenTelemetryConfig(); builder.AddDefaultOpenTelemetry(openTelemetryConfig); builder.Services.AddGrpc(); +builder.AddDatabase(); + builder.Services.AddHealthChecks(); var app = builder.Build(); diff --git a/src/Server.Controllers.Api.Grpc/appsettings.Development.json b/src/Server.Controllers.Api.Grpc/appsettings.Development.json index f4a6e5e..26cce75 100644 --- a/src/Server.Controllers.Api.Grpc/appsettings.Development.json +++ b/src/Server.Controllers.Api.Grpc/appsettings.Development.json @@ -1,4 +1,11 @@ { + "Database": { + "Host": "localhost", + "Port": 5432, + "User": "postgres", + "Password": "Secret1234", + "DbName": "MantaRayPlan" + }, "Logging": { "LogLevel": { "Default": "Information", diff --git a/src/Server.Controllers.Api.Grpc/appsettings.json b/src/Server.Controllers.Api.Grpc/appsettings.json index d56465e..0be6fb0 100644 --- a/src/Server.Controllers.Api.Grpc/appsettings.json +++ b/src/Server.Controllers.Api.Grpc/appsettings.json @@ -1,5 +1,12 @@ { "AllowedHosts": "*", + "Database": { + "Host": "localhost", + "Port": 5432, + "User": "postgres", + "Password": "Secret1234", + "DbName": "MantaRayPlan" + }, "Logging": { "LogLevel": { "Default": "Information", diff --git a/src/Server.Controller.Api.MessageBus.IntegrationTests/Api.MessageBus.IntegrationTests.csproj b/src/Server.Controllers.Api.MessageBus.IntegrationTests/Api.MessageBus.IntegrationTests.csproj similarity index 100% rename from src/Server.Controller.Api.MessageBus.IntegrationTests/Api.MessageBus.IntegrationTests.csproj rename to src/Server.Controllers.Api.MessageBus.IntegrationTests/Api.MessageBus.IntegrationTests.csproj diff --git a/src/Server.Controller.Api.MessageBus.IntegrationTests/HealthCheckTests.cs b/src/Server.Controllers.Api.MessageBus.IntegrationTests/HealthCheckTests.cs similarity index 100% rename from src/Server.Controller.Api.MessageBus.IntegrationTests/HealthCheckTests.cs rename to src/Server.Controllers.Api.MessageBus.IntegrationTests/HealthCheckTests.cs diff --git a/src/Server.Controllers.Api.MessageBus/Api.MessageBus.csproj b/src/Server.Controllers.Api.MessageBus/Api.MessageBus.csproj index 02b69da..767f695 100644 --- a/src/Server.Controllers.Api.MessageBus/Api.MessageBus.csproj +++ b/src/Server.Controllers.Api.MessageBus/Api.MessageBus.csproj @@ -18,6 +18,7 @@ + diff --git a/src/Server.Controllers.Api.MessageBus/Program.cs b/src/Server.Controllers.Api.MessageBus/Program.cs index 69e328f..52c3e5f 100644 --- a/src/Server.Controllers.Api.MessageBus/Program.cs +++ b/src/Server.Controllers.Api.MessageBus/Program.cs @@ -1,5 +1,7 @@ // See https://aka.ms/new-console-template for more information +using MadWorldNL.MantaRayPlan; +using MadWorldNL.MantaRayPlan.Extensions; using MadWorldNL.MantaRayPlan.OpenTelemetry; var builder = WebApplication.CreateBuilder(); @@ -9,12 +11,16 @@ builder.AddDefaultOpenTelemetry(openTelemetryConfig); +builder.AddDatabase(); + builder.Services.AddHealthChecks(); var app = builder.Build(); app.MapHealthChecks("/healthz"); +app.Services.MigrateDatabase(); + await app.RunAsync(); public abstract partial class Program { } \ No newline at end of file diff --git a/src/Server.Controllers.Api.MessageBus/appsettings.Development.json b/src/Server.Controllers.Api.MessageBus/appsettings.Development.json index a408e01..25629a4 100644 --- a/src/Server.Controllers.Api.MessageBus/appsettings.Development.json +++ b/src/Server.Controllers.Api.MessageBus/appsettings.Development.json @@ -1,4 +1,11 @@ { + "Database": { + "Host": "localhost", + "Port": 5432, + "User": "postgres", + "Password": "Secret1234", + "DbName": "MantaRayPlan" + }, "Logging": { "LogLevel": { "Default": "Information", diff --git a/src/Server.Controllers.Api.MessageBus/appsettings.json b/src/Server.Controllers.Api.MessageBus/appsettings.json index f97f345..aa2163c 100644 --- a/src/Server.Controllers.Api.MessageBus/appsettings.json +++ b/src/Server.Controllers.Api.MessageBus/appsettings.json @@ -1,5 +1,12 @@ { "AllowedHosts": "*", + "Database": { + "Host": "localhost", + "Port": 5432, + "User": "postgres", + "Password": "Secret1234", + "DbName": "MantaRayPlan" + }, "Logging": { "LogLevel": { "Default": "Information", diff --git a/src/Server.DataAccess.DataBase/DataBase.csproj b/src/Server.DataAccess.DataBase/DataBase.csproj index ed55981..649768e 100644 --- a/src/Server.DataAccess.DataBase/DataBase.csproj +++ b/src/Server.DataAccess.DataBase/DataBase.csproj @@ -10,6 +10,8 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + + diff --git a/src/Server.DataAccess.DataBase/DatabaseOptions.cs b/src/Server.DataAccess.DataBase/DatabaseOptions.cs new file mode 100644 index 0000000..52796fa --- /dev/null +++ b/src/Server.DataAccess.DataBase/DatabaseOptions.cs @@ -0,0 +1,14 @@ +namespace MadWorldNL.MantaRayPlan; + +public class DatabaseOptions +{ + public const string Key = "Database"; + + public string Host { get; init; } = string.Empty; + public int Port { get; init; } + public string User { get; init; } = string.Empty; + public string Password { get; init; } = string.Empty; + public string DbName { get; init; } = string.Empty; + + public string ConnectionString => $"Host={Host};Port={Port};Username={User};Password={Password};Database={DbName};"; +} \ No newline at end of file diff --git a/src/Server.DataAccess.DataBase/Extensions/ApplicationBuilderExtensions.cs b/src/Server.DataAccess.DataBase/Extensions/ApplicationBuilderExtensions.cs new file mode 100644 index 0000000..ad6bd1e --- /dev/null +++ b/src/Server.DataAccess.DataBase/Extensions/ApplicationBuilderExtensions.cs @@ -0,0 +1,14 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; + +namespace MadWorldNL.MantaRayPlan.Extensions; + +public static class ApplicationBuilderExtensions +{ + public static void MigrateDatabase(this IServiceProvider services) where TDbContext : DbContext + { + using var serviceScope = services.GetService()!.CreateScope(); + var context = serviceScope.ServiceProvider.GetRequiredService(); + context.Database.Migrate(); + } +} \ No newline at end of file diff --git a/src/Server.DataAccess.DataBase/Extensions/HostApplicationBuilderExtensions.cs b/src/Server.DataAccess.DataBase/Extensions/HostApplicationBuilderExtensions.cs new file mode 100644 index 0000000..6cf3688 --- /dev/null +++ b/src/Server.DataAccess.DataBase/Extensions/HostApplicationBuilderExtensions.cs @@ -0,0 +1,25 @@ +using MadWorldNL.MantaRayPlan.Application; +using MadWorldNL.MantaRayPlan.MessageBuses; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Configuration; + +namespace MadWorldNL.MantaRayPlan.Extensions; + +public static class HostApplicationBuilderExtensions +{ + public static void AddDatabase(this IHostApplicationBuilder builder) + { + var databaseOptions = builder.Configuration + .GetSection(DatabaseOptions.Key) + .Get() ?? throw new OptionNotFoundException(DatabaseOptions.Key); + + builder.Services.AddDbContext(options => + options.UseNpgsql( + databaseOptions.ConnectionString, + b => b.MigrationsAssembly("MadWorldNL.MantaRayPlan.DataAccess.Database"))); + + builder.Services.AddScoped(); + } +} \ No newline at end of file diff --git a/src/Server.DataAccess.DataBase/Migrations/20240822194557_InitialCreate.Designer.cs b/src/Server.DataAccess.DataBase/Migrations/20240822194557_InitialCreate.Designer.cs new file mode 100644 index 0000000..4a79481 --- /dev/null +++ b/src/Server.DataAccess.DataBase/Migrations/20240822194557_InitialCreate.Designer.cs @@ -0,0 +1,45 @@ +// +using MadWorldNL.MantaRayPlan; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace MadWorldNL.MantaRayPlan.Migrations +{ + [DbContext(typeof(MantaRayPlanDbContext))] + [Migration("20240822194557_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.8") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("MadWorldNL.MantaRayPlan.MessageBuses.MessageBusStatus", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.ToTable("MessageBusStatus"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/Server.DataAccess.DataBase/Migrations/20240822194557_InitialCreate.cs b/src/Server.DataAccess.DataBase/Migrations/20240822194557_InitialCreate.cs new file mode 100644 index 0000000..ecc1d06 --- /dev/null +++ b/src/Server.DataAccess.DataBase/Migrations/20240822194557_InitialCreate.cs @@ -0,0 +1,35 @@ +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace MadWorldNL.MantaRayPlan.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "MessageBusStatus", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Count = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_MessageBusStatus", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "MessageBusStatus"); + } + } +} diff --git a/src/Server.DataAccess.DataBase/Migrations/MantaRayPlanDbContextModelSnapshot.cs b/src/Server.DataAccess.DataBase/Migrations/MantaRayPlanDbContextModelSnapshot.cs new file mode 100644 index 0000000..2be9459 --- /dev/null +++ b/src/Server.DataAccess.DataBase/Migrations/MantaRayPlanDbContextModelSnapshot.cs @@ -0,0 +1,42 @@ +// +using MadWorldNL.MantaRayPlan; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace MadWorldNL.MantaRayPlan.Migrations +{ + [DbContext(typeof(MantaRayPlanDbContext))] + partial class MantaRayPlanDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.8") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("MadWorldNL.MantaRayPlan.MessageBuses.MessageBusStatus", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.ToTable("MessageBusStatus"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/Server.Logic.Domain/Application/OptionNotFoundException.cs b/src/Server.Logic.Domain/Application/OptionNotFoundException.cs new file mode 100644 index 0000000..792165f --- /dev/null +++ b/src/Server.Logic.Domain/Application/OptionNotFoundException.cs @@ -0,0 +1,10 @@ +namespace MadWorldNL.MantaRayPlan.Application; + +public class OptionNotFoundException : Exception +{ + public readonly string Key; + public OptionNotFoundException(string key) + { + Key = key; + } +} \ No newline at end of file From 445e1e39bc7cfb52c9154a78eb35d9be6d2612e1 Mon Sep 17 00:00:00 2001 From: Oscar Veldman Date: Fri, 23 Aug 2024 18:03:06 +0200 Subject: [PATCH 11/13] Add example endpoint to connect with the database --- src/Directory.Packages.props | 1 + .../Api.Grpc.csproj | 3 +- src/Server.Controllers.Api.Grpc/Program.cs | 13 +++++-- .../Protos/greet.proto | 21 ------------ .../Protos/messagebus.proto | 16 +++++++++ .../Services/GreeterService.cs | 22 ------------ .../Services/MessageBusServiceProxy.cs | 34 +++++++++++++++++++ .../appsettings.json | 2 +- .../MessageBuses/MessageBusRepository.cs | 1 + 9 files changed, 65 insertions(+), 48 deletions(-) delete mode 100644 src/Server.Controllers.Api.Grpc/Protos/greet.proto create mode 100644 src/Server.Controllers.Api.Grpc/Protos/messagebus.proto delete mode 100644 src/Server.Controllers.Api.Grpc/Services/GreeterService.cs create mode 100644 src/Server.Controllers.Api.Grpc/Services/MessageBusServiceProxy.cs diff --git a/src/Directory.Packages.props b/src/Directory.Packages.props index d729b0b..2c6f02e 100644 --- a/src/Directory.Packages.props +++ b/src/Directory.Packages.props @@ -1,5 +1,6 @@ + diff --git a/src/Server.Controllers.Api.Grpc/Api.Grpc.csproj b/src/Server.Controllers.Api.Grpc/Api.Grpc.csproj index 3bce6f5..046bf93 100644 --- a/src/Server.Controllers.Api.Grpc/Api.Grpc.csproj +++ b/src/Server.Controllers.Api.Grpc/Api.Grpc.csproj @@ -7,11 +7,12 @@ - + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/Server.Controllers.Api.Grpc/Program.cs b/src/Server.Controllers.Api.Grpc/Program.cs index fea3082..5ba4c3e 100644 --- a/src/Server.Controllers.Api.Grpc/Program.cs +++ b/src/Server.Controllers.Api.Grpc/Program.cs @@ -1,6 +1,6 @@ using MadWorldNL.MantaRayPlan.Extensions; using MadWorldNL.MantaRayPlan.OpenTelemetry; -using Server.Controllers.Api.Grpc.Services; +using MadWorldNL.MantaRayPlan.Services; var builder = WebApplication.CreateBuilder(args); @@ -10,14 +10,21 @@ builder.AddDefaultOpenTelemetry(openTelemetryConfig); builder.Services.AddGrpc(); +builder.Services.AddGrpcReflection(); + builder.AddDatabase(); builder.Services.AddHealthChecks(); var app = builder.Build(); -// Configure the HTTP request pipeline. -app.MapGrpcService(); +app.MapGrpcService(); + +if (app.Environment.IsDevelopment()) +{ + app.MapGrpcReflectionService(); +} + app.MapHealthChecks("/healthz"); app.MapGet("/", diff --git a/src/Server.Controllers.Api.Grpc/Protos/greet.proto b/src/Server.Controllers.Api.Grpc/Protos/greet.proto deleted file mode 100644 index 6ced199..0000000 --- a/src/Server.Controllers.Api.Grpc/Protos/greet.proto +++ /dev/null @@ -1,21 +0,0 @@ -syntax = "proto3"; - -option csharp_namespace = "Server.Controllers.Api.Grpc"; - -package greet; - -// The greeting service definition. -service Greeter { - // Sends a greeting - rpc SayHello (HelloRequest) returns (HelloReply); -} - -// The request message containing the user's name. -message HelloRequest { - string name = 1; -} - -// The response message containing the greetings. -message HelloReply { - string message = 1; -} diff --git a/src/Server.Controllers.Api.Grpc/Protos/messagebus.proto b/src/Server.Controllers.Api.Grpc/Protos/messagebus.proto new file mode 100644 index 0000000..4b72ca3 --- /dev/null +++ b/src/Server.Controllers.Api.Grpc/Protos/messagebus.proto @@ -0,0 +1,16 @@ +syntax = "proto3"; + +option csharp_namespace = "MantaRayPlan"; + +package messagebus; + +import "google/protobuf/empty.proto"; + +service MessageBusService { + rpc GetStatus (google.protobuf.Empty) returns (MessageBusStatusReply); +} + +message MessageBusStatusReply { + string message = 1; + int32 counter = 2; +} \ No newline at end of file diff --git a/src/Server.Controllers.Api.Grpc/Services/GreeterService.cs b/src/Server.Controllers.Api.Grpc/Services/GreeterService.cs deleted file mode 100644 index 28fa151..0000000 --- a/src/Server.Controllers.Api.Grpc/Services/GreeterService.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Grpc.Core; -using Server.Controllers.Api.Grpc; - -namespace Server.Controllers.Api.Grpc.Services; - -public class GreeterService : Greeter.GreeterBase -{ - private readonly ILogger _logger; - - public GreeterService(ILogger logger) - { - _logger = logger; - } - - public override Task SayHello(HelloRequest request, ServerCallContext context) - { - return Task.FromResult(new HelloReply - { - Message = "Hello " + request.Name - }); - } -} \ No newline at end of file diff --git a/src/Server.Controllers.Api.Grpc/Services/MessageBusServiceProxy.cs b/src/Server.Controllers.Api.Grpc/Services/MessageBusServiceProxy.cs new file mode 100644 index 0000000..61f1d90 --- /dev/null +++ b/src/Server.Controllers.Api.Grpc/Services/MessageBusServiceProxy.cs @@ -0,0 +1,34 @@ +using Google.Protobuf.WellKnownTypes; +using Grpc.Core; +using MadWorldNL.MantaRayPlan.MessageBuses; +using MantaRayPlan; + +namespace MadWorldNL.MantaRayPlan.Services; + +public class MessageBusServiceProxy(IMessageBusRepository messageBusRepository, ILogger logger) + : MessageBusService.MessageBusServiceBase +{ + public override async Task GetStatus(Empty request, ServerCallContext context) + { + try + { + var status = await messageBusRepository.FindStatusAsync() ?? new MessageBusStatus(); + + return new MessageBusStatusReply() + { + Counter = status.Count + }; + } + catch (Exception ex) + { + const string message = "Database error"; + + logger.LogError(ex, message); + + return new MessageBusStatusReply() + { + Message = message + }; + } + } +} \ No newline at end of file diff --git a/src/Server.Controllers.Api.Grpc/appsettings.json b/src/Server.Controllers.Api.Grpc/appsettings.json index 0be6fb0..f8f53df 100644 --- a/src/Server.Controllers.Api.Grpc/appsettings.json +++ b/src/Server.Controllers.Api.Grpc/appsettings.json @@ -20,7 +20,7 @@ }, "Kestrel": { "EndpointDefaults": { - "Protocols": "Http1AndHttp2" + "Protocols": "Http2" } } } diff --git a/src/Server.DataAccess.DataBase/MessageBuses/MessageBusRepository.cs b/src/Server.DataAccess.DataBase/MessageBuses/MessageBusRepository.cs index 1aeed89..69d1a04 100644 --- a/src/Server.DataAccess.DataBase/MessageBuses/MessageBusRepository.cs +++ b/src/Server.DataAccess.DataBase/MessageBuses/MessageBusRepository.cs @@ -22,6 +22,7 @@ await _dbContext.MessageBusStatus public async Task FindStatusAsync() { return await _dbContext.MessageBusStatus + .OrderBy(s => s.Id) .LastOrDefaultAsync(); } From d4c3d3a714a615e9b4e226886be0ed5f3f3f00e3 Mon Sep 17 00:00:00 2001 From: Oscar Veldman Date: Fri, 23 Aug 2024 19:04:08 +0200 Subject: [PATCH 12/13] Finishing touch with postgres --- .../environments/values-production.yaml | 3 ++- .../MantaRayPlanCloud/templates/api-grpc.yaml | 16 ++++++++++++++++ .../templates/api-message-bus.yaml | 16 ++++++++++++++++ .../MantaRayPlanCloud/templates/postgres.yaml | 2 +- deployment/MantaRayPlanCloud/values.yaml | 2 ++ docs/kubernetes.md | 2 +- .../MessageBuses/MessageBusRepository.cs | 19 ++++++------------- 7 files changed, 44 insertions(+), 16 deletions(-) diff --git a/deployment/MantaRayPlanCloud/environments/values-production.yaml b/deployment/MantaRayPlanCloud/environments/values-production.yaml index ecde643..c791fa8 100644 --- a/deployment/MantaRayPlanCloud/environments/values-production.yaml +++ b/deployment/MantaRayPlanCloud/environments/values-production.yaml @@ -1,7 +1,7 @@ namespace: "manta-ray-plan-production" image: - tag: "v0.4.6" + tag: "v0.4.7" clusterIssuer: enabled: true @@ -30,6 +30,7 @@ logging: firstTimePassword: "QFCKH3NTSBQ5zmsH9DpXTB2YefIavEGJKa4SshKb11AXX8b4o4KPjuo9bd6WBfqDkpxKqzNjaOrCsE49ph369Wx84mIrksnJx5OtRcxAOjff" database: + enabled: true size: single: 10Gi combined: 35Gi diff --git a/deployment/MantaRayPlanCloud/templates/api-grpc.yaml b/deployment/MantaRayPlanCloud/templates/api-grpc.yaml index 228eb37..b992ba0 100644 --- a/deployment/MantaRayPlanCloud/templates/api-grpc.yaml +++ b/deployment/MantaRayPlanCloud/templates/api-grpc.yaml @@ -31,6 +31,22 @@ spec: timeoutSeconds: 1 failureThreshold: 3 env: + - name: Database__Host + value: "postgres-ro" + - name: Database__Port + value: "5432" + - name: Database__DbName + value: "MantaRayPlan" + - name: Database__User + valueFrom: + secretKeyRef: + name: {{ .Values.database.secret.name }} + key: username + - name: Database__Password + valueFrom: + secretKeyRef: + name: {{ .Values.database.secret.name }} + key: password - name: OpenTelemetry__LoggerEndpoint value: "{{ .Values.logging.seq.host.internal }}" - name: OpenTelemetry__LoggerApiKey diff --git a/deployment/MantaRayPlanCloud/templates/api-message-bus.yaml b/deployment/MantaRayPlanCloud/templates/api-message-bus.yaml index e2b8ffd..2eb10ea 100644 --- a/deployment/MantaRayPlanCloud/templates/api-message-bus.yaml +++ b/deployment/MantaRayPlanCloud/templates/api-message-bus.yaml @@ -31,6 +31,22 @@ spec: timeoutSeconds: 1 failureThreshold: 3 env: + - name: Database__Host + value: "postgres-rw" + - name: Database__Port + value: "5432" + - name: Database__DbName + value: "MantaRayPlan" + - name: Database__User + valueFrom: + secretKeyRef: + name: {{ .Values.database.secret.name }} + key: username + - name: Database__Password + valueFrom: + secretKeyRef: + name: {{ .Values.database.secret.name }} + key: password - name: OpenTelemetry__LoggerEndpoint value: "{{ .Values.logging.seq.host.internal }}" - name: OpenTelemetry__LoggerApiKey diff --git a/deployment/MantaRayPlanCloud/templates/postgres.yaml b/deployment/MantaRayPlanCloud/templates/postgres.yaml index c94601a..7555db4 100644 --- a/deployment/MantaRayPlanCloud/templates/postgres.yaml +++ b/deployment/MantaRayPlanCloud/templates/postgres.yaml @@ -2,7 +2,7 @@ apiVersion: v1 kind: Secret metadata: - name: postgres-secret + name: {{ .Values.database.secret.name }} namespace: {{ .Values.namespace }} type: kubernetes.io/basic-auth data: diff --git a/deployment/MantaRayPlanCloud/values.yaml b/deployment/MantaRayPlanCloud/values.yaml index 23b3c3f..28ef5c3 100644 --- a/deployment/MantaRayPlanCloud/values.yaml +++ b/deployment/MantaRayPlanCloud/values.yaml @@ -85,6 +85,8 @@ database: size: single: 1Gi combined: 4Gi + secret: + name: postgres-secret pgadmin: outsideAccess: diff --git a/docs/kubernetes.md b/docs/kubernetes.md index f7b6ff1..297086e 100644 --- a/docs/kubernetes.md +++ b/docs/kubernetes.md @@ -75,7 +75,7 @@ helm install -f environments/values-development.yaml manta-ray-plan-cloud . Upgrade the cluster when you have a new version: ```shell -helm upgrade -f environments/values-development.yaml manta-ray-plan-cloud . + helm upgrade -f environments/values-development.yaml manta-ray-plan-cloud . ``` Remove the cluster from Kubernetes: diff --git a/src/Server.DataAccess.DataBase/MessageBuses/MessageBusRepository.cs b/src/Server.DataAccess.DataBase/MessageBuses/MessageBusRepository.cs index 69d1a04..8e243d7 100644 --- a/src/Server.DataAccess.DataBase/MessageBuses/MessageBusRepository.cs +++ b/src/Server.DataAccess.DataBase/MessageBuses/MessageBusRepository.cs @@ -2,35 +2,28 @@ namespace MadWorldNL.MantaRayPlan.MessageBuses; -public class MessageBusRepository : IMessageBusRepository +public class MessageBusRepository(MantaRayPlanDbContext dbContext) : IMessageBusRepository { - private readonly MantaRayPlanDbContext _dbContext; - - public MessageBusRepository(MantaRayPlanDbContext dbContext) - { - _dbContext = dbContext; - } - public async Task CreateAsync(MessageBusStatus status) { - await _dbContext.MessageBusStatus + await dbContext.MessageBusStatus .AddAsync(status); - await _dbContext.SaveChangesAsync(); + await dbContext.SaveChangesAsync(); } public async Task FindStatusAsync() { - return await _dbContext.MessageBusStatus + return await dbContext.MessageBusStatus .OrderBy(s => s.Id) .LastOrDefaultAsync(); } public async Task UpdateAsync(MessageBusStatus status) { - _dbContext.MessageBusStatus + dbContext.MessageBusStatus .Update(status); - await _dbContext.SaveChangesAsync(); + await dbContext.SaveChangesAsync(); } } \ No newline at end of file From 655bc4a6c11dd49492aece3dec1091305ba624fc Mon Sep 17 00:00:00 2001 From: Oscar Veldman Date: Fri, 23 Aug 2024 19:12:05 +0200 Subject: [PATCH 13/13] Revert file --- docs/kubernetes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/kubernetes.md b/docs/kubernetes.md index 297086e..f7b6ff1 100644 --- a/docs/kubernetes.md +++ b/docs/kubernetes.md @@ -75,7 +75,7 @@ helm install -f environments/values-development.yaml manta-ray-plan-cloud . Upgrade the cluster when you have a new version: ```shell - helm upgrade -f environments/values-development.yaml manta-ray-plan-cloud . +helm upgrade -f environments/values-development.yaml manta-ray-plan-cloud . ``` Remove the cluster from Kubernetes: