go-api-example/main.go
Julien Leicher 51e9830876
All checks were successful
continuous-integration/drone/push Build is passing
Test
2023-05-12 19:05:35 +02:00

86 lines
1.3 KiB
Go

package main
import (
"database/sql"
"net/http"
"os"
"time"
"github.com/gin-gonic/gin"
_ "github.com/lib/pq"
)
func main() {
dsn := os.Getenv("DSN")
if dsn == "" {
panic("db connection string should be set with the DSN env variable!!")
}
db, err := sql.Open("postgres", dsn)
if err != nil {
panic(err)
}
if err := db.Ping(); err != nil {
panic(err)
}
if _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS runs (
id SERIAL PRIMARY KEY,
date TIMESTAMP WITHOUT TIME ZONE NOT NULL
);
INSERT INTO runs(date) VALUES (now());
`); err != nil {
panic(err)
}
r := gin.Default()
r.GET("/", func(ctx *gin.Context) {
rows, err := db.Query("SELECT id, date FROM runs")
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
defer rows.Close()
var results []data
for rows.Next() {
var d data
if err := rows.Scan(&d.ID, &d.Date); err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
results = append(results, d)
}
ctx.JSON(http.StatusOK, response{
Env: os.Environ(),
Runs: results,
})
})
if err := r.Run(":8080"); err != nil {
panic(err)
}
}
type data struct {
ID int `json:"id"`
Date time.Time `json:"date"`
}
type response struct {
Env []string `json:"env"`
Runs []data `json:"runs"`
}