-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.go
78 lines (71 loc) · 2.16 KB
/
server.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
package main
import (
"database/sql"
"github.com/dlmiddlecote/sqlstats"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/xpmatteo/todomvc-golang/db"
"github.com/xpmatteo/todomvc-golang/web"
"html/template"
"log"
_ "modernc.org/sqlite"
"net/http"
"time"
)
const port = "8080"
func main() {
pool, err := sql.Open("sqlite", "development.db")
if err != nil {
log.Fatal(err.Error())
return
}
pool.SetConnMaxLifetime(60 * time.Minute)
pool.SetMaxIdleConns(3)
pool.SetMaxOpenConns(10)
if _, err := pool.Exec(db.CreateTableSQL); err != nil {
panic(err.Error())
}
repository := db.NewTodoRepository(pool)
// publish DB stats with Prometheus
collector := sqlstats.NewStatsCollector("todo_db", pool)
prometheus.MustRegister(collector)
// I have to use a new ServeMux because the default mux is polluted by
// expvar, a package that I have no idea how it gets included in the project.
// Problem is that expvar declares a route for "/debug/vars" that
// conflicts with the route "/", pending a bugfix in go 1.22.*
mux := http.NewServeMux()
templ := template.Must(template.ParseFiles("templates/index.html"))
mux.Handle("GET /{$}",
web.Metrics("index",
web.Logging(
web.IndexHandler(templ, repository))))
mux.Handle("GET /active",
web.Metrics("active",
web.Logging(
web.IndexHandler(templ, repository))))
mux.Handle("GET /completed",
web.Metrics("completed",
web.Logging(
web.IndexHandler(templ, repository))))
mux.Handle("POST /new-todo",
web.Metrics("new-todo",
web.Logging(
web.Slowdown(1000,
web.NewItemHandler(templ, repository)))))
mux.Handle("POST /toggle",
web.Metrics("toggle",
web.Logging(
web.ToggleHandler(templ, repository))))
mux.Handle("POST /edit",
web.Metrics("edit",
web.Logging(
web.EditHandler(templ, repository))))
mux.Handle("POST /destroy",
web.Metrics("destroy",
web.Logging(
web.DestroyHandler(templ, repository))))
mux.Handle("GET /metrics", promhttp.Handler())
mux.Handle("GET /", http.FileServer(http.Dir("./public/")))
log.Println("Listening on port " + port)
web.GracefulListenAndServe(":"+port, mux)
}