From d72b86e65f6ccfdee5f3af85f9fcc3b1ebca87bd Mon Sep 17 00:00:00 2001
From: Nicolas Pernoud <npernoud@grandlyon.com>
Date: Thu, 26 Jan 2023 10:40:03 +0100
Subject: [PATCH] feat: added persistent data

---
 deployment.yml |  7 +++++++
 main.go        | 22 +++++++++++++++++++++-
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/deployment.yml b/deployment.yml
index 4731502..721baa7 100644
--- a/deployment.yml
+++ b/deployment.yml
@@ -17,6 +17,10 @@ spec:
       labels:
         app: demo-openshift
     spec:
+      volumes:
+        - name: data
+          persistentVolumeClaim:
+            claimName: pvc-01-ns-demo-d01-claim
       containers:
         - name: demo-openshift-app
           image: registry.forge.grandlyon.com/npernoud/demo-openshift:latest
@@ -40,6 +44,9 @@ spec:
             limits:
               memory: "128Mi"
               cpu: 500m
+          volumeMounts:
+            - mountPath: "/app/data"
+              name: "data"
       imagePullSecrets:
         - name: forge-secret
 ---
diff --git a/main.go b/main.go
index 4ebe24d..b65814d 100644
--- a/main.go
+++ b/main.go
@@ -4,12 +4,32 @@ import (
 	"fmt"
 	"log"
 	"net/http"
+	"os"
+	"strings"
 )
 
 func main() {
 	// API routes
 	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
-		fmt.Fprintf(w, "Bonjour la Métropole du Grand Lyon")
+		fmt.Fprintf(w, "Hello world")
+	})
+
+	http.HandleFunc("/writefile/", func(w http.ResponseWriter, r *http.Request) {
+		// create the file
+		f, err := os.Create("data/test.txt")
+		if err != nil {
+			fmt.Println(err)
+		}
+		// close the file with defer
+		defer f.Close()
+
+		// write a string
+		str := strings.TrimPrefix(r.URL.Path, "/writefile/")
+		f.WriteString("Donnée enregistrée : " + str)
+	})
+
+	http.HandleFunc("/readfile/", func(w http.ResponseWriter, r *http.Request) {
+		http.ServeFile(w, r, "data/test.txt")
 	})
 
 	port := ":8080"
-- 
GitLab