-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
133 lines (114 loc) · 3.13 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"os"
"github.com/gin-gonic/gin"
_ "github.com/heroku/x/hmetrics/onload"
_ "github.com/lib/pq"
)
type Item struct {
ID string `json:"id"`
Title string `json:"title"`
Details string `json:"details"`
Image string `json:"image"`
Url string `json:"url"`
Likes int `json:"likes"`
}
func getNews(db *sql.DB) gin.HandlerFunc {
return func(c *gin.Context) {
var items []Item
rows, err := db.Query("SELECT * FROM news ORDER BY likes DESC")
if err != nil {
c.String(http.StatusInternalServerError,
fmt.Sprintf("Error reading news: %q", err))
return
}
defer rows.Close()
for rows.Next() {
var item Item
if err := rows.Scan(&item.ID, &item.Title, &item.Details, &item.Image, &item.Url, &item.Likes); err != nil {
c.String(http.StatusInternalServerError,
fmt.Sprintf("Error scanning news: %q", err))
return
}
items = append(items, item)
}
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": items})
}
}
func getNewsRecent(db *sql.DB) gin.HandlerFunc {
return func(c *gin.Context) {
var items []Item
rows, err := db.Query("SELECT * FROM news ORDER BY id DESC LIMIT 6")
if err != nil {
c.String(http.StatusInternalServerError,
fmt.Sprintf("Error reading news: %q", err))
return
}
defer rows.Close()
for rows.Next() {
var item Item
if err := rows.Scan(&item.ID, &item.Title, &item.Details, &item.Image, &item.Url, &item.Likes); err != nil {
c.String(http.StatusInternalServerError,
fmt.Sprintf("Error scanning news: %q", err))
return
}
items = append(items, item)
}
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": items})
}
}
func upVote(db *sql.DB) gin.HandlerFunc {
return func(c *gin.Context) {
var id = c.Param("id")
var likes int
rows, err := db.Query("SELECT id, likes FROM news WHERE id = $1", id)
if err != nil {
c.String(http.StatusInternalServerError,
fmt.Sprintf("Error reading news: %q", err))
return
}
defer rows.Close()
for rows.Next() {
var item Item
if err := rows.Scan(&item.ID, &item.Likes); err != nil {
c.String(http.StatusInternalServerError,
fmt.Sprintf("Error scanning news: %q", err))
return
}
likes = item.Likes + 1
}
res, err := db.Exec("UPDATE news SET likes = $1 WHERE id = $2", likes, id)
if err != nil {
c.String(http.StatusInternalServerError,
fmt.Sprintf("Error putting news: %q", err))
return
}
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": likes, "res": res})
}
}
func main() {
port := os.Getenv("PORT")
if port == "" {
log.Fatal("$PORT must be set")
}
db, err := sql.Open("postgres", os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatalf("Error opening database: %q", err)
}
defer db.Close()
router := gin.New()
router.Use(gin.Logger())
router.LoadHTMLGlob("templates/*.tmpl.html")
router.Static("/static", "static")
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl.html", nil)
})
router.GET("/api/", getNews(db))
router.GET("/recent/", getNewsRecent(db))
router.PUT("/up/:id", upVote(db))
router.Run(":" + port)
}