-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (62 loc) · 1.89 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
package main
import (
"context"
"database/sql"
"fmt"
"os"
"github.com/aria3ppp/url-shortener-openapi/internal/core/usecase"
"github.com/aria3ppp/url-shortener-openapi/internal/generator"
"github.com/aria3ppp/url-shortener-openapi/internal/oapi"
"github.com/aria3ppp/url-shortener-openapi/internal/repository"
"github.com/aria3ppp/url-shortener-openapi/internal/server"
"github.com/deepmap/oapi-codegen/pkg/middleware"
"github.com/getkin/kin-openapi/openapi3filter"
"github.com/labstack/echo/v4"
_ "github.com/lib/pq"
)
func main() {
dsn := fmt.Sprintf(
"postgres://%s:%s@%s:%s/%s?sslmode=disable",
os.Getenv("POSTGRES_USER"),
os.Getenv("POSTGRES_PASSWORD"),
os.Getenv("POSTGRES_HOST"),
os.Getenv("POSTGRES_PORT"),
os.Getenv("POSTGRES_DB"),
)
db, err := sql.Open("postgres", dsn)
if err != nil {
panic(err)
}
if err := db.Ping(); err != nil {
panic(err)
}
repo := repository.NewRepository(db)
generator := generator.NewRandomStringGenerator(6)
serviceUseCases := usecase.NewService(repo, generator)
//--------------------------------------------------------------------------
// handler := handler.NewHandler(serviceUseCases)
// router := echo.New()
// helper.HandleRoutes(router, handler)
// if err := router.Start(":" + os.Getenv("SERVER_PORT")); err != nil {
// panic(err)
// }
//--------------------------------------------------------------------------
swagger, err := oapi.GetSwagger()
if err != nil {
panic(err)
}
// swagger.Servers = nil
serverImpl := server.New(serviceUseCases)
e := echo.New()
e.Use(middleware.OapiRequestValidatorWithOptions(swagger, &middleware.Options{
Options: openapi3filter.Options{
AuthenticationFunc: func(ctx context.Context, ai *openapi3filter.AuthenticationInput) error {
return nil
},
},
}))
oapi.RegisterHandlers(e, serverImpl)
if err := e.Start(":" + os.Getenv("SERVER_PORT")); err != nil {
panic(err)
}
}