-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
52 lines (43 loc) · 1.02 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
package main
import (
"database/sql"
"fmt"
"html/template"
"log"
"net/http"
"github.com/infogulch/inject"
"github.com/davecgh/go-spew/spew"
_ "modernc.org/sqlite"
)
type server func()
type listenAddr string
func makeServer(addr listenAddr, h http.Handler, log *log.Logger) server {
return func() {
log.Print(http.ListenAndServe(string(addr), h))
}
}
func main() {
scope, err := inject.New(
log.Default(),
func() (*sql.DB, error) { return sql.Open("sqlite", "file::memory:?cache=shared") },
func() (*template.Template, error) { return template.New("app").Parse(templateString) },
&requestMetrics{},
makeTodosRepository,
[]any{logMiddleware, metricMiddleware},
middlwares,
[]any{home, greet},
[]any{todosList, todosAdd, todosDel},
handlers,
listenAddr("0.0.0.0:8080"),
makeServer,
)
if err != nil {
panic(fmt.Errorf("failed to construct injector: %v", err))
}
srv, err := inject.Get[server](scope)
if err != nil {
spew.Dump(scope)
panic(fmt.Errorf("failed to get server: %v", err))
}
srv()
}