diff --git a/api-mssql-go/README.md b/api-mssql-go/README.md
deleted file mode 100644
index c0e51e3f1..000000000
--- a/api-mssql-go/README.md
+++ /dev/null
@@ -1,178 +0,0 @@
-# Golang MS SQL database API
-
-## Overview
-
-This sample provides a Golang API endpoint for communication with the MS SQL databases provided in the `database-mssql` and `database-azure-mssql` directories. You can also enable an Event Trigger for both examples.
-
-## MS SQL database example
-
-For the `database-mssql` example, use the `deployment.yaml` file. It provides the Deployment definition as well as an APIRule to expose the Function without authentication. The Deployment also contains a ConfigMap and a Secret with the following parameters for the `database-mssql` example that you can configure to modify the default options:
-
-| Parameter | Value |
-| ------------ | ----------------------------- |
-| **database** | `DemoDB` |
-| **host** | `mssql.dev.svc.cluster.local` |
-| **password** | `Yukon900` |
-| **username** | `sa` |
-| **port** | `1433` |
-
-This sample demonstrates how to:
-
-- Create a development Namespace in the Kyma runtime.
-- Deploy the following Kubernetes resources:
- - API deployment written in GO
- - API Rule
- - Service
- - Secret
-
-## Azure MS SQL database example
-
-For the `database-azure-mssql` example, use the `deployment-servicebinding.yaml` file. It defines the Deployment definition as well as an APIRule to expose the Function without authentication. It also defines a ServiceBinding and ServiceBindingUsage that configure the Function to use the `database-azure-mssql` ServiceInstance.
-
-This sample demonstrates how to:
-
-- Create a development Namespace in the Kyma runtime.
-- Deploy the following Kubernetes resources:
- - API deployment written in GO
- - API Rule
- - Service
- - Event Subscription
- - ServiceBinding
- - ServiceBindingUsage
-
-## Prerequisites
-
-- SAP BTP, Kyma runtime instance
-- [Docker](https://www.docker.com/)
-- [Go](https://golang.org/doc/install)
-- [kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/) configured to use the `KUBECONFIG` file downloaded from the Kyma runtime
-
-## Steps
-
-### Run the API locally
-
-1. Set the environment variables required to connect with the database:
-
- ```shell script
- export MYAPP_username=sa
- export MYAPP_password=Yukon900
- export MYAPP_database=DemoDB
- export MYAPP_host=localhost
- export MYAPP_port=1433
- ```
-
-2. Run the application:
-
- ```shell script
- go run ./cmd/api
- ```
-
-### Build the Docker image
-
-1. Build and push the image to your Docker repository:
-
- ```shell script
- docker build -t {your-docker-account}/api-mssql-go -f docker/Dockerfile .
- docker push {your-docker-account}/api-mssql-go
- ```
-
-2. To run the image locally, run:
-
- ```shell script
- docker run -p 8000:8000 -d {your-docker-account}/api-mssql-go:latest
- ```
-
-### Deploy the API - MS SQL database example
-
-1. Create a new `dev` Namespace:
-
- ```shell script
- kubectl create namespace dev
- kubectl label namespaces dev istio-injection=enabled
- ```
-
-2. Apply the ConfigMap:
-
- ```shell script
- kubectl -n dev apply -f ./k8s/configmap.yaml
- ```
-
-3. Apply the Secret:
-
- ```shell script
- kubectl -n dev apply -f ./k8s/secret.yaml
- ```
-
-4. Apply the Deployment:
-
- ```shell script
- kubectl -n dev apply -f ./k8s/deployment.yaml
- ```
-
-5. Apply the APIRule:
-
- ```shell script
- kubectl -n dev apply -f ./k8s/apirule.yaml
- ```
-
-6. Verify that the Deployment is up and running:
-
- ```shell script
- kubectl -n dev get deployment api-mssql-go
- ```
-
-7. Use the APIRule:
-
- - `https://api-mssql-go.{cluster-domain}/orders`
- - `https://api-mssql-go.{cluster-domain}/orders/10000001`
-
-### Deploy the API - Azure MS SQL database example
-
-1. Create a new `dev` Namespace:
-
- ```shell script
- kubectl create namespace dev
- ```
-
-2. Get the name of the ServiceInstance:
-
- ```shell script
- kubectl -n dev get serviceinstances
- ```
-
- For example:
-
- | NAME | CLASS | PLAN | STATUS | AGE |
- | ------------------------------------- | --------------------------- | ----- | ------ | --- |
- | **_azure-sql-12-0-unkempt-entrance_** | ServiceClass/azure-sql-12-0 | basic | Ready | 63m |
-
-3. Within the `deployment-servicebinding.yaml`, adjust the name of the **instanceRef** property of the corresponding ServiceBinding:
-
- ```yaml
- apiVersion: servicecatalog.k8s.io/v1beta1
- kind: ServiceBinding
- metadata:
- name: azure-sql
- spec:
- instanceRef: name:azure-sql-12-0-unkempt-entrance
- ```
-
-4. Apply the Deployment:
-
- ```shell script
- kubectl -n dev apply -f ./k8s/deployment-servicebinding.yaml
- ```
-
-### Deploy the Event Subscription
-
-The Event Subscription works for both samples. It expects that either SAP Commerce Cloud or the Commerce Mock application is connected and configured within the Namespace. You can find a blog post with details on the Commerce Mock setup [here](https://blogs.sap.com/2020/06/17/sap-cloud-platform-extension-factory-kyma-runtime-commerce-mock-events-and-apis/).
-
-The subscription and code within the Golang application are set up for the `order.created` event. Before you deploy the subscription, verify that the value of `spec.filter.filters.eventType.value` is correct for the name of your application.
-
-1. Apply the Deployment:
-
- ```shell script
- kubectl -n dev apply -f ./k8s/event.yaml
- ```
-
-2. Within the mock application, submit the `order.created` event. This populates the database with the submitted order code and the `order received from event` notification.
diff --git a/api-mssql-go/go.mod b/api-mssql-go/go.mod
deleted file mode 100644
index ce721b7d4..000000000
--- a/api-mssql-go/go.mod
+++ /dev/null
@@ -1,10 +0,0 @@
-module github.com/SAP-samples/kyma-runtime-extension-samples/api-mssql-go
-
-go 1.14
-
-require (
- github.com/denisenkom/go-mssqldb v0.0.0-20200910202707-1e08a3fab204
- github.com/gorilla/mux v1.8.0
- github.com/vrischmann/envconfig v1.3.0
- golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
-)
diff --git a/api-mssql-go/k8s/configmap.yaml b/api-mssql-go/k8s/configmap.yaml
deleted file mode 100644
index 70565526f..000000000
--- a/api-mssql-go/k8s/configmap.yaml
+++ /dev/null
@@ -1,10 +0,0 @@
-apiVersion: v1
-kind: ConfigMap
-metadata:
- name: api-mssql-go
- labels:
- app: api-mssql-go
-data:
- database: DemoDB
- host: mssql.dev.svc.cluster.local
- port: "1433"
diff --git a/api-mssql-go/k8s/deployment-servicebinding.yaml b/api-mssql-go/k8s/deployment-servicebinding.yaml
deleted file mode 100644
index 06f18c7a1..000000000
--- a/api-mssql-go/k8s/deployment-servicebinding.yaml
+++ /dev/null
@@ -1,56 +0,0 @@
----
-apiVersion: apps/v1
-kind: Deployment
-metadata:
- name: api-mssql-go
- labels:
- app: api-mssql-go
-spec:
- replicas: 1
- selector:
- matchLabels:
- app: api-mssql-go
- template:
- metadata:
- labels:
- app: api-mssql-go
- spec:
- containers:
- # replace the repository URL with your own repository
- - image: jcawley5/api-mssql-go:latest
- imagePullPolicy: Always
- name: api-mssql-go
- ports:
- - name: http
- containerPort: 8000
----
-apiVersion: v1
-kind: Service
-metadata:
- name: api-mssql-go
- labels:
- app: api-mssql-go
-spec:
- ports:
- - name: http
- port: 80
- targetPort: 8000
- protocol: TCP
- selector:
- app: api-mssql-go
----
-apiVersion: gateway.kyma-project.io/v2
-kind: APIRule
-metadata:
- name: api-mssql-go
-spec:
- gateway: kyma-system/kyma-gateway
- hosts:
- - api-mssql-go
- service:
- name: api-mssql-go
- port: 80
- rules:
- - path: /orders/{**}
- noAuth: true
- methods: ["GET", "POST", "PUT", "DELETE"]
\ No newline at end of file
diff --git a/api-mssql-go/k8s/secret.yaml b/api-mssql-go/k8s/secret.yaml
deleted file mode 100644
index 9b357dd7e..000000000
--- a/api-mssql-go/k8s/secret.yaml
+++ /dev/null
@@ -1,11 +0,0 @@
-apiVersion: v1
-kind: Secret
-metadata:
- name: mssql
- labels:
- app: api-mssql-go
-type: Opaque
-data:
- #sa:Yukon900
- username: c2E=
- password: WXVrb245MDA=
diff --git a/api-postgres-go/README.md b/api-postgres-go/README.md
new file mode 100644
index 000000000..702dffed2
--- /dev/null
+++ b/api-postgres-go/README.md
@@ -0,0 +1,106 @@
+# Golang PostgreSQL database API
+
+## Overview
+
+This sample provides a Golang API endpoint for communication with the PostgreSQL database provisioned in the `postgres-provision` tutorial. The service exposes CRUD endpoints for the `Orders` table created in `DemoDB`. An optional Kyma event subscription can insert orders from an `order.created` event.
+
+Default connection values used throughout the sample:
+
+| Parameter | Value |
+| ------------ | ----------------------------------- |
+| **database** | `DemoDB` |
+| **host** | `postgres.dev.svc.cluster.local` |
+| **password** | `Yukon900` |
+| **username** | `postgres` |
+| **port** | `5432` (or forwarded port `15432`) |
+
+## Prerequisites
+
+- SAP BTP, Kyma runtime instance
+- [Docker](https://www.docker.com/)
+- [Go](https://golang.org/doc/install)
+- [kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/) configured to use the `KUBECONFIG` file downloaded from the Kyma runtime
+- PostgreSQL provisioned via `postgres-provision/postgres-provision.md` (Secret `postgres` and Service `postgres` in namespace `dev`)
+
+## Steps
+
+### Run the API locally
+
+1. Set the environment variables required to connect with the database (adjust host/port to match your setup or forwarded port):
+
+ ```shell script
+ export POSTGRES_USER=postgres
+ export POSTGRES_PASSWORD=Yukon900
+ export POSTGRES_DB=DemoDB
+ export POSTGRES_HOST=localhost
+ export POSTGRES_PORT=15432
+ export POSTGRES_SSLMODE=disable
+ ```
+
+2. Run the application:
+
+ ```shell script
+ go run ./cmd/api
+ ```
+
+### Build the Docker image
+
+1. Build and push the image to your Docker repository:
+
+ ```shell script
+ docker build -t {your-docker-account}/api-postgres-go -f docker/Dockerfile .
+ docker push {your-docker-account}/api-postgres-go
+ ```
+
+2. To run the image locally, run:
+
+ ```shell script
+ docker run -p 8000:8000 -d --name api-postgres-go \
+ -e POSTGRES_USER=postgres \
+ -e POSTGRES_PASSWORD=Yukon900 \
+ -e POSTGRES_DB=DemoDB \
+ -e POSTGRES_HOST=host.docker.internal \
+ -e POSTGRES_PORT=15432 \
+ -e POSTGRES_SSLMODE=disable \
+ {your-docker-account}/api-postgres-go:latest
+ ```
+
+### Deploy the API
+
+1. Create a new `dev` Namespace (skip if already created during provisioning):
+
+ ```shell script
+ kubectl create namespace dev
+ kubectl label namespaces dev istio-injection=enabled
+ ```
+
+2. Apply the ConfigMap and Secret (skip the Secret if it already exists from the provisioning tutorial):
+
+ ```shell script
+ kubectl -n dev apply -f ./k8s/configmap.yaml
+ kubectl -n dev apply -f ./k8s/secret.yaml
+ ```
+
+3. Apply the Deployment and APIRule:
+
+ ```shell script
+ kubectl -n dev apply -f ./k8s/deployment.yaml
+ kubectl -n dev apply -f ./k8s/apirule.yaml
+ ```
+
+4. Verify that the Deployment is up and running:
+
+ ```shell script
+ kubectl -n dev get deployment api-postgres-go
+ ```
+
+5. Optional: apply the Event Subscription to insert orders from events:
+
+ ```shell script
+ kubectl -n dev apply -f ./k8s/event.yaml
+ ```
+
+6. Use the APIRule:
+
+ - `https://api-postgres-go.{cluster-domain}/orders`
+ - `https://api-postgres-go.{cluster-domain}/orders/10000001`
diff --git a/api-mssql-go/cmd/api/main.go b/api-postgres-go/cmd/api/main.go
similarity index 88%
rename from api-mssql-go/cmd/api/main.go
rename to api-postgres-go/cmd/api/main.go
index 7958e2bb7..9df47fa5e 100644
--- a/api-mssql-go/cmd/api/main.go
+++ b/api-postgres-go/cmd/api/main.go
@@ -6,7 +6,7 @@ import (
"github.com/gorilla/mux"
- "github.com/SAP-samples/kyma-runtime-extension-samples/api-mssql-go/internal/api"
+ "github.com/SAP-samples/kyma-runtime-extension-samples/api-postgres-go/internal/api"
)
func main() {
diff --git a/api-mssql-go/docker/Dockerfile b/api-postgres-go/docker/Dockerfile
similarity index 52%
rename from api-mssql-go/docker/Dockerfile
rename to api-postgres-go/docker/Dockerfile
index 524b99f9b..ea1fb7dcb 100644
--- a/api-mssql-go/docker/Dockerfile
+++ b/api-postgres-go/docker/Dockerfile
@@ -1,4 +1,4 @@
-FROM golang:1.14 as builder
+FROM golang:1.21 as builder
ENV GO111MODULE=on
@@ -12,11 +12,11 @@ COPY cmd ./cmd
COPY internal ./internal
RUN ls /app/
-RUN CGO_ENABLED=0 GOOS=linux go build -v -a -o api-mssql-go ./cmd/api
+RUN CGO_ENABLED=0 GOOS=linux go build -v -trimpath -o api-postgres-go ./cmd/api
FROM scratch
WORKDIR /app
-COPY --from=builder /app/api-mssql-go /app/
+COPY --from=builder /app/api-postgres-go /app/
EXPOSE 8000
-ENTRYPOINT ["/app/api-mssql-go"]
\ No newline at end of file
+ENTRYPOINT ["/app/api-postgres-go"]
\ No newline at end of file
diff --git a/api-postgres-go/go.mod b/api-postgres-go/go.mod
new file mode 100644
index 000000000..4977bd0fd
--- /dev/null
+++ b/api-postgres-go/go.mod
@@ -0,0 +1,9 @@
+module github.com/SAP-samples/kyma-runtime-extension-samples/api-postgres-go
+
+go 1.21
+
+require (
+ github.com/gorilla/mux v1.8.0
+ github.com/lib/pq v1.10.9
+ github.com/vrischmann/envconfig v1.3.0
+)
diff --git a/api-mssql-go/go.sum b/api-postgres-go/go.sum
similarity index 54%
rename from api-mssql-go/go.sum
rename to api-postgres-go/go.sum
index 04b111f69..af59615b3 100644
--- a/api-mssql-go/go.sum
+++ b/api-postgres-go/go.sum
@@ -2,17 +2,13 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
-github.com/denisenkom/go-mssqldb v0.0.0-20200910202707-1e08a3fab204 h1:tI48fqaIkxxYuIylVv1tdDfBp6836GKSfmmzgSyP1CY=
-github.com/denisenkom/go-mssqldb v0.0.0-20200910202707-1e08a3fab204/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
-github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY=
-github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
-github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
-github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
+github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
+github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@@ -21,17 +17,7 @@ github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/vrischmann/envconfig v1.3.0 h1:4XIvQTXznxmWMnjouj0ST5lFo/WAYf5Exgl3x82crEk=
github.com/vrischmann/envconfig v1.3.0/go.mod h1:bbvxFYJdRSpXrhS63mBFtKJzkDiNkyArOLXtY6q0kuI=
-golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
-golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c h1:Vj5n4GlwjmQteupaxJ9+0FNOmBrHfq7vN4btdGoDZgI=
-golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
-golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
-golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
-golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
-golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
-golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
-golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
-gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ=
diff --git a/api-mssql-go/internal/api/events.go b/api-postgres-go/internal/api/events.go
similarity index 84%
rename from api-mssql-go/internal/api/events.go
rename to api-postgres-go/internal/api/events.go
index d1570371a..8e528673d 100644
--- a/api-mssql-go/internal/api/events.go
+++ b/api-postgres-go/internal/api/events.go
@@ -20,6 +20,11 @@ func (s *server) ConsumeOrderCode(w http.ResponseWriter, r *http.Request) {
return
}
+ if order.OrderCode == "" {
+ http.Error(w, "orderCode is required", http.StatusBadRequest)
+ return
+ }
+
_, err = s.db.AddOrder(order.OrderCode, "order received from event")
if err != nil {
diff --git a/api-mssql-go/internal/api/server.go b/api-postgres-go/internal/api/server.go
similarity index 71%
rename from api-mssql-go/internal/api/server.go
rename to api-postgres-go/internal/api/server.go
index 2bf9705ff..074ed45df 100644
--- a/api-mssql-go/internal/api/server.go
+++ b/api-postgres-go/internal/api/server.go
@@ -3,9 +3,10 @@ package api
import (
"encoding/json"
"net/http"
- "strings"
- "github.com/SAP-samples/kyma-runtime-extension-samples/api-mssql-go/internal/db"
+ "github.com/gorilla/mux"
+
+ "github.com/SAP-samples/kyma-runtime-extension-samples/api-postgres-go/internal/db"
)
type orderData struct {
@@ -24,8 +25,11 @@ func InitAPIServer() *server {
}
func (s *server) GetOrder(w http.ResponseWriter, r *http.Request) {
-
- order_id := strings.Split(r.URL.Path, "/")[2]
+ order_id := mux.Vars(r)["id"]
+ if order_id == "" {
+ http.Error(w, "missing order id", http.StatusBadRequest)
+ return
+ }
orders, err := s.db.GetOrder(order_id)
if err != nil {
@@ -58,7 +62,14 @@ func (s *server) EditOrder(w http.ResponseWriter, r *http.Request) {
var order orderData
defer r.Body.Close()
- err := json.NewDecoder(r.Body).Decode(&order)
+ if err := json.NewDecoder(r.Body).Decode(&order); err != nil {
+ http.Error(w, err.Error(), http.StatusBadRequest)
+ return
+ }
+ if order.Orderid == "" {
+ http.Error(w, "order_id is required", http.StatusBadRequest)
+ return
+ }
rowsEffected, err := s.db.EditOrder(order.Orderid, order.Description)
@@ -78,10 +89,12 @@ func (s *server) AddOrder(w http.ResponseWriter, r *http.Request) {
var order orderData
defer r.Body.Close()
- err := json.NewDecoder(r.Body).Decode(&order)
-
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
+ if err := json.NewDecoder(r.Body).Decode(&order); err != nil {
+ http.Error(w, err.Error(), http.StatusBadRequest)
+ return
+ }
+ if order.Orderid == "" {
+ http.Error(w, "order_id is required", http.StatusBadRequest)
return
}
@@ -99,7 +112,11 @@ func (s *server) AddOrder(w http.ResponseWriter, r *http.Request) {
}
func (s *server) DeleteOrder(w http.ResponseWriter, r *http.Request) {
- order_id := strings.Split(r.URL.Path, "/")[2]
+ order_id := mux.Vars(r)["id"]
+ if order_id == "" {
+ http.Error(w, "missing order id", http.StatusBadRequest)
+ return
+ }
rowsEffected, err := s.db.DeleteOrder(order_id)
if err != nil {
diff --git a/api-mssql-go/internal/config/config.go b/api-postgres-go/internal/config/config.go
similarity index 54%
rename from api-mssql-go/internal/config/config.go
rename to api-postgres-go/internal/config/config.go
index 35ed36fa4..2631c7bd5 100644
--- a/api-mssql-go/internal/config/config.go
+++ b/api-postgres-go/internal/config/config.go
@@ -9,16 +9,17 @@ import (
var appConfig Config
-//Config struct to hold the app config
+// Config struct to hold the app config
type Config struct {
- Server string `envconfig:"MYAPP_host"`
- Port string `envconfig:"MYAPP_port,default=1433"`
- Username string `envconfig:"MYAPP_username"`
- Password string `envconfig:"MYAPP_password"`
- Database string `envconfig:"MYAPP_database"`
+ Host string `envconfig:"POSTGRES_HOST"`
+ Port string `envconfig:"POSTGRES_PORT,default=5432"`
+ Username string `envconfig:"POSTGRES_USER"`
+ Password string `envconfig:"POSTGRES_PASSWORD"`
+ Database string `envconfig:"POSTGRES_DB"`
+ SSLMode string `envconfig:"POSTGRES_SSLMODE,default=disable"`
}
-//InitConfig initializes the AppConfig
+// InitConfig initializes the AppConfig
func initConfig() {
log.Println("initilizing db configuration....")
appConfig = Config{}
@@ -32,7 +33,7 @@ func initConfig() {
}
}
-//AppConfig returns the current AppConfig
+// AppConfig returns the current AppConfig
func GetConfig() Config {
if appConfig == (Config{}) {
initConfig()
diff --git a/api-mssql-go/internal/db/connection.go b/api-postgres-go/internal/db/connection.go
similarity index 56%
rename from api-mssql-go/internal/db/connection.go
rename to api-postgres-go/internal/db/connection.go
index 85b7173b8..d7f9dcdb1 100644
--- a/api-mssql-go/internal/db/connection.go
+++ b/api-postgres-go/internal/db/connection.go
@@ -6,8 +6,8 @@ import (
"log"
"time"
- "github.com/SAP-samples/kyma-runtime-extension-samples/api-mssql-go/internal/config"
- _ "github.com/denisenkom/go-mssqldb"
+ "github.com/SAP-samples/kyma-runtime-extension-samples/api-postgres-go/internal/config"
+ _ "github.com/lib/pq"
)
// var db *sql.DB
@@ -16,7 +16,7 @@ type Server struct {
db *sql.DB
}
-//InitDatabase - sets database connection configuration
+// InitDatabase - sets database connection configuration
func InitDatabase() *Server {
var err error
@@ -25,7 +25,7 @@ func InitDatabase() *Server {
log.Printf("Setting connection to db with configuration: %s \n", connString)
server := &Server{}
- server.db, err = sql.Open("sqlserver", connString)
+ server.db, err = sql.Open("postgres", connString)
if err != nil {
log.Fatal("Error opening connection: ", err.Error())
}
@@ -35,18 +35,23 @@ func InitDatabase() *Server {
return server
}
-//gets configuration and returns appropiate connection string
+// gets configuration and returns appropiate connection string
func getConnString() string {
config := config.GetConfig()
- connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%s;database=%s;",
- config.Server, config.Username, config.Password, config.Port, config.Database)
+ connString := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=%s",
+ config.Username,
+ config.Password,
+ config.Host,
+ config.Port,
+ config.Database,
+ config.SSLMode)
return connString
}
-//will verify the connection is available or generate a new one
+// will verify the connection is available or generate a new one
func (s *Server) getConnection() {
err := s.db.Ping()
diff --git a/api-mssql-go/internal/db/order.go b/api-postgres-go/internal/db/order.go
similarity index 72%
rename from api-mssql-go/internal/db/order.go
rename to api-postgres-go/internal/db/order.go
index b22813ac5..0ae1bd590 100644
--- a/api-mssql-go/internal/db/order.go
+++ b/api-postgres-go/internal/db/order.go
@@ -6,7 +6,7 @@ import (
"time"
)
-//Order -
+// Order -
type Order struct {
Orderid string `json:"order_id"`
Description string `json:"description"`
@@ -18,33 +18,33 @@ type RowsAffected struct {
}
func (s *Server) GetOrder(order_id string) ([]Order, error) {
- tsql := fmt.Sprintf("SELECT * FROM Orders WHERE order_id=@p1;")
+ tsql := fmt.Sprintf("SELECT order_id, description, created FROM Orders WHERE order_id=$1;")
return s.query(tsql, order_id)
}
func (s *Server) GetOrders() ([]Order, error) {
- tsql := fmt.Sprintf("SELECT * FROM Orders;")
- return s.query(tsql, nil)
+ tsql := fmt.Sprintf("SELECT order_id, description, created FROM Orders;")
+ return s.query(tsql)
}
func (s *Server) AddOrder(order_id string, description string) ([]Order, error) {
- tsql := fmt.Sprintf("INSERT INTO Orders(order_id, description) VALUES(@p1,@p2);")
+ tsql := fmt.Sprintf("INSERT INTO Orders(order_id, description) VALUES($1,$2);")
_, err := s.exec(tsql, order_id, description)
if err != nil {
return nil, err
}
- tsql = fmt.Sprintf("SELECT * FROM Orders WHERE order_id=@p1;")
- return s.query(tsql, order_id, description)
+ tsql = fmt.Sprintf("SELECT order_id, description, created FROM Orders WHERE order_id=$1;")
+ return s.query(tsql, order_id)
}
func (s *Server) EditOrder(order_id string, description string) (RowsAffected, error) {
- tsql := fmt.Sprintf("UPDATE Orders SET description=@p2 WHERE order_id=@p1")
+ tsql := fmt.Sprintf("UPDATE Orders SET description=$2 WHERE order_id=$1")
return s.exec(tsql, order_id, description)
}
func (s *Server) DeleteOrder(order_id string) (RowsAffected, error) {
- tsql := fmt.Sprintf("DELETE FROM Orders WHERE order_id=@p1")
+ tsql := fmt.Sprintf("DELETE FROM Orders WHERE order_id=$1")
return s.exec(tsql, order_id)
}
@@ -56,7 +56,7 @@ func (s *Server) exec(tsql string, args ...interface{}) (RowsAffected, error) {
rowsAffectedResult.RowsAffected = 0
log.Printf("Executing SQL: %s \n", tsql)
- log.Printf("With args: %s \n", args...)
+ log.Printf("With args: %v \n", args)
result, err := s.db.Exec(tsql, args...)
if err != nil {
@@ -78,7 +78,7 @@ func (s *Server) query(tsql string, args ...interface{}) ([]Order, error) {
orders := []Order{}
log.Printf("Executing SQL: %s \n", tsql)
- log.Printf("With args: %s \n", args...)
+ log.Printf("With args: %v \n", args)
rows, err := s.db.Query(tsql, args...)
@@ -90,8 +90,7 @@ func (s *Server) query(tsql string, args ...interface{}) ([]Order, error) {
defer rows.Close()
for rows.Next() {
- err := rows.Scan(&order.Orderid, &order.Description, &order.Created)
- if err != nil {
+ if err := rows.Scan(&order.Orderid, &order.Description, &order.Created); err != nil {
return nil, err
}
orders = append(orders, order)
diff --git a/api-mssql-go/k8s/apirule.yaml b/api-postgres-go/k8s/apirule.yaml
similarity index 84%
rename from api-mssql-go/k8s/apirule.yaml
rename to api-postgres-go/k8s/apirule.yaml
index 4fe985046..356f1db2a 100644
--- a/api-mssql-go/k8s/apirule.yaml
+++ b/api-postgres-go/k8s/apirule.yaml
@@ -1,12 +1,12 @@
apiVersion: gateway.kyma-project.io/v2
kind: APIRule
metadata:
- name: api-mssql-go
+ name: api-postgres-go
spec:
hosts:
- - api-mssql-go
+ - api-postgres-go
service:
- name: api-mssql-go
+ name: api-postgres-go
port: 80
gateway: kyma-system/kyma-gateway
corsPolicy:
@@ -20,6 +20,7 @@ spec:
- OPTIONS
allowOrigins:
- regex: ".*{YOUR_DOMAIN}"
+ - exact: http://localhost:8080
rules:
- path: "/orders"
methods:
diff --git a/api-mssql-go/k8s/authorizationpolicy.yaml b/api-postgres-go/k8s/authorizationpolicy.yaml
similarity index 91%
rename from api-mssql-go/k8s/authorizationpolicy.yaml
rename to api-postgres-go/k8s/authorizationpolicy.yaml
index 73f6750cc..1640daaac 100644
--- a/api-mssql-go/k8s/authorizationpolicy.yaml
+++ b/api-postgres-go/k8s/authorizationpolicy.yaml
@@ -6,7 +6,7 @@ metadata:
spec:
selector:
matchLabels:
- app: api-mssql-go
+ app: api-postgres-go
action: ALLOW
rules:
- from:
diff --git a/api-postgres-go/k8s/configmap.yaml b/api-postgres-go/k8s/configmap.yaml
new file mode 100644
index 000000000..06a88b897
--- /dev/null
+++ b/api-postgres-go/k8s/configmap.yaml
@@ -0,0 +1,10 @@
+apiVersion: v1
+kind: ConfigMap
+metadata:
+ name: api-postgres-go
+ labels:
+ app: api-postgres-go
+data:
+ database: DemoDB
+ host: postgres.dev.svc.cluster.local
+ port: "5432"
diff --git a/api-mssql-go/k8s/deployment.yaml b/api-postgres-go/k8s/deployment.yaml
similarity index 57%
rename from api-mssql-go/k8s/deployment.yaml
rename to api-postgres-go/k8s/deployment.yaml
index 83dc63977..85841170c 100644
--- a/api-mssql-go/k8s/deployment.yaml
+++ b/api-postgres-go/k8s/deployment.yaml
@@ -1,59 +1,59 @@
apiVersion: apps/v1
kind: Deployment
metadata:
- name: api-mssql-go
+ name: api-postgres-go
labels:
- app: api-mssql-go
+ app: api-postgres-go
spec:
replicas: 1
selector:
matchLabels:
- app: api-mssql-go
+ app: api-postgres-go
template:
metadata:
labels:
- app: api-mssql-go
+ app: api-postgres-go
spec:
containers:
- - image: jcawley5/api-mssql-go:latest #change it to your image
+ - image: /api-postgres-go:latest #change it to your image
imagePullPolicy: Always
- name: api-mssql-go
+ name: api-postgres-go
ports:
- name: http
containerPort: 8000
env:
- - name: MYAPP_database
+ - name: POSTGRES_DB
valueFrom:
configMapKeyRef:
- name: api-mssql-go
+ name: api-postgres-go
key: database
- - name: MYAPP_host
+ - name: POSTGRES_HOST
valueFrom:
configMapKeyRef:
- name: api-mssql-go
+ name: api-postgres-go
key: host
- - name: MYAPP_port
+ - name: POSTGRES_PORT
valueFrom:
configMapKeyRef:
- name: api-mssql-go
+ name: api-postgres-go
key: port
- - name: MYAPP_username
+ - name: POSTGRES_USER
valueFrom:
secretKeyRef:
key: username
- name: mssql
- - name: MYAPP_password
+ name: postgres
+ - name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
key: password
- name: mssql
+ name: postgres
---
apiVersion: v1
kind: Service
metadata:
- name: api-mssql-go
+ name: api-postgres-go
labels:
- app: api-mssql-go
+ app: api-postgres-go
spec:
ports:
- name: http
@@ -61,4 +61,4 @@ spec:
targetPort: 8000
protocol: TCP
selector:
- app: api-mssql-go
+ app: api-postgres-go
diff --git a/api-mssql-go/k8s/event.yaml b/api-postgres-go/k8s/event.yaml
similarity index 56%
rename from api-mssql-go/k8s/event.yaml
rename to api-postgres-go/k8s/event.yaml
index 9b361a70e..584d28a09 100644
--- a/api-mssql-go/k8s/event.yaml
+++ b/api-postgres-go/k8s/event.yaml
@@ -1,9 +1,9 @@
apiVersion: eventing.kyma-project.io/v1alpha2
kind: Subscription
metadata:
- name: api-mssql-go-event-sub
+ name: api-postgres-go-event-sub
spec:
- sink: 'http://api-mssql-go.dev.svc.cluster.local:80/orderCodeEvent'
+ sink: 'http://api-postgres-go.dev.svc.cluster.local:80/orderCodeEvent'
source: mp-commerce-mock
types:
- order.created.v1
\ No newline at end of file