add go with db

This commit is contained in:
2026-04-08 11:02:14 +02:00
parent 20a8b93711
commit 556d2d589e
8 changed files with 111 additions and 2 deletions
+1 -1
View File
@@ -2,7 +2,7 @@ project:
name: test name: test
services: services:
- hostname: app - hostname: app
type: alpine@latest type: ubuntu@24.04
minContainers: 1 minContainers: 1
maxContainers: 1 maxContainers: 1
buildFromGit: https://github.com/tikinang/godummy buildFromGit: https://github.com/tikinang/godummy
+1 -1
View File
@@ -1,6 +1,6 @@
services: services:
- hostname: app - hostname: app
type: alpine@latest type: ubuntu@24.04
maxContainers: 1 maxContainers: 1
buildFromGit: https://github.com/tikinang/godummy buildFromGit: https://github.com/tikinang/godummy
enableSubdomainAccess: true enableSubdomainAccess: true
+1
View File
@@ -0,0 +1 @@
app
+5
View File
@@ -0,0 +1,5 @@
module gowithdb
go 1.25.7
require github.com/lib/pq v1.12.3
+2
View File
@@ -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=
+73
View File
@@ -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)
}
}
+9
View File
@@ -0,0 +1,9 @@
services:
- hostname: db
type: postgresql@16
mode: NON_HA
- hostname: app
type: go@latest
minContainers: 1
maxContainers: 1
enableSubdomainAccess: true
+19
View File
@@ -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