-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy path1
170 lines (144 loc) · 4.84 KB
/
1
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
_ "github.com/lib/pq"
)
// Define Prometheus metrics
var (
addGoalCounter = prometheus.NewCounter(prometheus.CounterOpts{
Name: "add_goal_requests_total",
Help: "Total number of add goal requests",
})
removeGoalCounter = prometheus.NewCounter(prometheus.CounterOpts{
Name: "remove_goal_requests_total",
Help: "Total number of remove goal requests",
})
httpRequestsCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "http_requests_total",
Help: "Total number of HTTP requests",
},
[]string{"path"},
)
)
func init() {
// Register Prometheus metrics
prometheus.MustRegister(addGoalCounter)
prometheus.MustRegister(removeGoalCounter)
prometheus.MustRegister(httpRequestsCounter)
}
func createConnection() (*sql.DB, error) {
connStr := fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=disable",
os.Getenv("DB_USERNAME"),
os.Getenv("DB_PASSWORD"),
os.Getenv("DB_HOST"),
os.Getenv("DB_PORT"),
os.Getenv("DB_NAME"),
)
db, err := sql.Open("postgres", connStr)
if err != nil {
return nil, err
}
err = db.Ping()
if err != nil {
return nil, err
}
return db, nil
}
func main() {
router := gin.Default()
router.LoadHTMLGlob("templates/*")
router.GET("/", func(c *gin.Context) {
db, err := createConnection()
if err != nil {
log.Println("Error connecting to PostgreSQL", err)
c.String(http.StatusInternalServerError, "Error connecting to the PostgreSQL database")
return
}
defer db.Close()
rows, err := db.Query("SELECT * FROM goals")
if err != nil {
log.Println("Error querying database", err)
c.String(http.StatusInternalServerError, "Error querying the database")
return
}
defer rows.Close()
var goals []struct {
ID int
Name string
}
for rows.Next() {
var goal struct {
ID int
Name string
}
if err := rows.Scan(&goal.ID, &goal.Name); err != nil {
log.Println("Error scanning row", err)
continue
}
goals = append(goals, goal)
}
httpRequestsCounter.WithLabelValues("/").Inc()
c.HTML(http.StatusOK, "index.html", gin.H{
"goals": goals,
})
})
router.POST("/add_goal", func(c *gin.Context) {
goalName := c.PostForm("goal_name")
if goalName != "" {
db, err := createConnection()
if err != nil {
log.Println("Error connecting to PostgreSQL", err)
c.String(http.StatusInternalServerError, "Error connecting to the PostgreSQL database")
return
}
defer db.Close()
_, err = db.Exec("INSERT INTO goals (goal_name) VALUES ($1)", goalName)
if err != nil {
log.Println("Error inserting goal", err)
c.String(http.StatusInternalServerError, "Error inserting goal into the database")
return
}
// Increment the add goal counter
addGoalCounter.Inc()
httpRequestsCounter.WithLabelValues("/add_goal").Inc()
}
c.Redirect(http.StatusFound, "/")
})
router.POST("/remove_goal", func(c *gin.Context) {
goalID := c.PostForm("goal_id")
if goalID != "" {
db, err := createConnection()
if err != nil {
log.Println("Error connecting to PostgreSQL", err)
c.String(http.StatusInternalServerError, "Error connecting to the PostgreSQL database")
return
}
defer db.Close()
_, err = db.Exec("DELETE FROM goals WHERE id = $1", goalID)
if err != nil {
log.Println("Error deleting goal", err)
c.String(http.StatusInternalServerError, "Error deleting goal from the database")
return
}
// Increment the remove goal counter
removeGoalCounter.Inc()
httpRequestsCounter.WithLabelValues("/remove_goal").Inc()
}
c.Redirect(http.StatusFound, "/")
})
router.GET("/health", func(c *gin.Context) {
httpRequestsCounter.WithLabelValues("/health").Inc()
c.String(http.StatusOK, "OK")
})
// Expose metrics endpoint
router.GET("/metrics", gin.WrapH(promhttp.Handler()))
router.Run(":8080")
}