From 556d2d589e8430facbe343432662f3efe6b7a02d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Pavl=C3=AD=C4=8Dek?= Date: Wed, 8 Apr 2026 11:02:14 +0200 Subject: [PATCH] add go with db --- godummy/zerops-project-import.yml | 2 +- godummy/zerops-service-import.yml | 2 +- gowithdb/.gitignore | 1 + gowithdb/go.mod | 5 ++ gowithdb/go.sum | 2 + gowithdb/main.go | 73 ++++++++++++++++++++++++++++++ gowithdb/zerops-service-import.yml | 9 ++++ gowithdb/zerops.yml | 19 ++++++++ 8 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 gowithdb/.gitignore create mode 100644 gowithdb/go.mod create mode 100644 gowithdb/go.sum create mode 100644 gowithdb/main.go create mode 100644 gowithdb/zerops-service-import.yml create mode 100644 gowithdb/zerops.yml diff --git a/godummy/zerops-project-import.yml b/godummy/zerops-project-import.yml index 40e6443..582d4fa 100644 --- a/godummy/zerops-project-import.yml +++ b/godummy/zerops-project-import.yml @@ -2,7 +2,7 @@ project: name: test services: - hostname: app - type: alpine@latest + type: ubuntu@24.04 minContainers: 1 maxContainers: 1 buildFromGit: https://github.com/tikinang/godummy diff --git a/godummy/zerops-service-import.yml b/godummy/zerops-service-import.yml index 494c73c..7c380de 100644 --- a/godummy/zerops-service-import.yml +++ b/godummy/zerops-service-import.yml @@ -1,6 +1,6 @@ services: - hostname: app - type: alpine@latest + type: ubuntu@24.04 maxContainers: 1 buildFromGit: https://github.com/tikinang/godummy enableSubdomainAccess: true diff --git a/gowithdb/.gitignore b/gowithdb/.gitignore new file mode 100644 index 0000000..b80f0bd --- /dev/null +++ b/gowithdb/.gitignore @@ -0,0 +1 @@ +app diff --git a/gowithdb/go.mod b/gowithdb/go.mod new file mode 100644 index 0000000..f2168a7 --- /dev/null +++ b/gowithdb/go.mod @@ -0,0 +1,5 @@ +module gowithdb + +go 1.25.7 + +require github.com/lib/pq v1.12.3 diff --git a/gowithdb/go.sum b/gowithdb/go.sum new file mode 100644 index 0000000..c7cd147 --- /dev/null +++ b/gowithdb/go.sum @@ -0,0 +1,2 @@ +github.com/lib/pq v1.12.3 h1:tTWxr2YLKwIvK90ZXEw8GP7UFHtcbTtty8zsI+YjrfQ= +github.com/lib/pq v1.12.3/go.mod h1:/p+8NSbOcwzAEI7wiMXFlgydTwcgTr3OSKMsD2BitpA= diff --git a/gowithdb/main.go b/gowithdb/main.go new file mode 100644 index 0000000..c90c6f5 --- /dev/null +++ b/gowithdb/main.go @@ -0,0 +1,73 @@ +package main + +import ( + "crypto/rand" + "database/sql" + "encoding/hex" + "encoding/json" + "fmt" + "net/http" + "os" + "time" + + _ "github.com/lib/pq" +) + +func main() { + db, err := sql.Open("postgres", os.Getenv("DATABASE_CONNECTION_STRING")) + if err != nil { + panic(err) + } + if err := db.Ping(); err != nil { + panic(err) + } + + _, err = db.Exec(`CREATE TABLE IF NOT EXISTS hits ( + id BIGSERIAL PRIMARY KEY, + ts TIMESTAMPTZ NOT NULL DEFAULT NOW(), + path TEXT NOT NULL, + payload TEXT NOT NULL + )`) + if err != nil { + panic(err) + } + + hostname, _ := os.Hostname() + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + fmt.Printf("%s %s %s %s\n", r.Method, r.URL.Path, r.RemoteAddr, r.UserAgent()) + + buf := make([]byte, 128) + rand.Read(buf) + payload := hex.EncodeToString(buf) + ts := time.Now() + + var id int64 + err := db.QueryRow( + `INSERT INTO hits (ts, path, payload) VALUES ($1, $2, $3) RETURNING id`, + ts, r.URL.Path, payload, + ).Scan(&id) + if err != nil { + fmt.Printf("error inserting: %s\n", err) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusInternalServerError) + json.NewEncoder(w).Encode(map[string]string{"error": err.Error()}) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(map[string]any{ + "id": id, + "ts": ts, + "path": r.URL.Path, + "payload": payload[:16], + "hostname": hostname, + }) + }) + + fmt.Println("listening on :8080") + if err := http.ListenAndServe(":8080", nil); err != nil { + panic(err) + } +} diff --git a/gowithdb/zerops-service-import.yml b/gowithdb/zerops-service-import.yml new file mode 100644 index 0000000..7d2dc42 --- /dev/null +++ b/gowithdb/zerops-service-import.yml @@ -0,0 +1,9 @@ +services: + - hostname: db + type: postgresql@16 + mode: NON_HA + - hostname: app + type: go@latest + minContainers: 1 + maxContainers: 1 + enableSubdomainAccess: true diff --git a/gowithdb/zerops.yml b/gowithdb/zerops.yml new file mode 100644 index 0000000..6e14f4c --- /dev/null +++ b/gowithdb/zerops.yml @@ -0,0 +1,19 @@ +zerops: + - setup: app + build: + os: ubuntu + base: go@latest + buildCommands: + - go build -o app main.go + cache: true + deployFiles: + - app + run: + os: ubuntu + base: ubuntu@latest + envVariables: + DATABASE_CONNECTION_STRING: ${db_connectionString}/${db_dbName} + ports: + - port: 8080 + httpSupport: true + start: ./app