-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
42 lines (36 loc) · 803 Bytes
/
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
package main
import (
"fmt"
_ "github.com/jackc/pgx/v4/stdlib"
"github.com/jmoiron/sqlx"
"github.com/kzon/technopark-sem2-db/pkg/api"
"github.com/kzon/technopark-sem2-db/pkg/api/repository"
"github.com/valyala/fasthttp"
"log"
"os"
)
const PORT = "5000"
func main() {
db, err := NewDB()
if err != nil {
log.Fatal(err)
}
repo := repository.NewRepository(db)
usecase := api.NewUsecase(repo)
handler := api.NewHandler(usecase)
fmt.Println("listening port " + PORT)
log.Fatal(fasthttp.ListenAndServe(":"+PORT, handler.GetHandleFunc()))
}
func NewDB() (*sqlx.DB, error) {
db, err := sqlx.Open("pgx", os.Getenv("POSTGRES_DSN"))
if err != nil {
return nil, err
}
db.SetMaxOpenConns(8)
db.SetMaxIdleConns(8)
err = db.Ping()
if err != nil {
return nil, err
}
return db, nil
}