Skip to content

Commit ca2df39

Browse files
author
Edward Muller
committed
/db
1 parent aca7503 commit ca2df39

File tree

19 files changed

+5607
-4
lines changed

19 files changed

+5607
-4
lines changed

main.go

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,64 @@ package main
22

33
import (
44
"bytes"
5+
"database/sql"
6+
"fmt"
57
"log"
68
"net/http"
79
"os"
810
"strconv"
11+
"time"
912

1013
"github.com/gin-gonic/gin"
14+
_ "github.com/lib/pq"
1115
"github.com/russross/blackfriday"
1216
)
1317

1418
var (
1519
repeat int
20+
db *sql.DB
1621
)
1722

18-
func repeatHandler(c *gin.Context) {
23+
func repeatFunc(c *gin.Context) {
1924
var buffer bytes.Buffer
2025
for i := 0; i < repeat; i++ {
21-
buffer.WriteString("Hello from Go!\n")
26+
buffer.WriteString("Hello from Go!")
2227
}
2328
c.String(http.StatusOK, buffer.String())
2429
}
2530

31+
func dbFunc(c *gin.Context) {
32+
if _, err := db.Exec("CREATE TABLE IF NOT EXISTS ticks (tick timestamp)"); err != nil {
33+
c.String(http.StatusInternalServerError,
34+
fmt.Sprintf("Error creating database table: %q", err))
35+
return
36+
}
37+
38+
if _, err := db.Exec("INSERT INTO ticks VALUES (now())"); err != nil {
39+
c.String(http.StatusInternalServerError,
40+
fmt.Sprintf("Error incrementing tick: %q", err))
41+
return
42+
}
43+
44+
rows, err := db.Query("SELECT tick FROM ticks")
45+
if err != nil {
46+
c.String(http.StatusInternalServerError,
47+
fmt.Sprintf("Error reading ticks: %q", err))
48+
return
49+
}
50+
51+
defer rows.Close()
52+
for rows.Next() {
53+
var tick time.Time
54+
if err := rows.Scan(&tick); err != nil {
55+
c.String(http.StatusInternalServerError,
56+
fmt.Sprintf("Error scanning ticks: %q", err))
57+
return
58+
}
59+
c.String(http.StatusOK, fmt.Sprintf("Read from DB: %s\n", tick.String()))
60+
}
61+
}
62+
2663
func main() {
2764
port := os.Getenv("PORT")
2865

@@ -34,10 +71,15 @@ func main() {
3471
tStr := os.Getenv("REPEAT")
3572
repeat, err = strconv.Atoi(tStr)
3673
if err != nil {
37-
log.Printf("Error converting $REPEAT to an int: %q - Using default\n", err)
74+
log.Print("Error converting $REPEAT to an int: %q - Using default", err)
3875
repeat = 5
3976
}
4077

78+
db, err = sql.Open("postgres", os.Getenv("DATABASE_URL"))
79+
if err != nil {
80+
log.Fatalf("Error opening database: %q", err)
81+
}
82+
4183
router := gin.New()
4284
router.Use(gin.Logger())
4385
router.LoadHTMLGlob("templates/*.tmpl.html")
@@ -51,7 +93,8 @@ func main() {
5193
c.String(http.StatusOK, string(blackfriday.MarkdownBasic([]byte("**hi!**"))))
5294
})
5395

54-
router.GET("/repeat", repeatHandler)
96+
router.GET("/repeat", repeatFunc)
97+
router.GET("/db", dbFunc)
5598

5699
router.Run(":" + port)
57100
}

vendor/github.com/lib/pq/CONTRIBUTING.md

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/lib/pq/LICENSE.md

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/lib/pq/README.md

Lines changed: 105 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)