-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
96 lines (83 loc) · 3.24 KB
/
api.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2019 Nguyên Cát Phạm. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
// Go Effective Study
//
// API Docs for Go Effective Study v1
//
// Terms Of Service: N/A
// Schemes: http
// Version: 2.0.0
// License: MIT http://opensource.org/licenses/MIT
// Contact: Phạm Nguyên Cát <dtpham258@gmail.com> https://github.com/nguyencatpham
// Host: localhost:8080
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Security:
// - bearer: []
//
// SecurityDefinitions:
// bearer:
// type: apiKey
// name: Authorization
// in: header
//
// swagger:meta
package api
import (
"crypto/sha1"
"github.com/nguyencatpham/go-effective-study/pkg/utl/zlog"
"github.com/nguyencatpham/go-effective-study/pkg/api/auth"
al "github.com/nguyencatpham/go-effective-study/pkg/api/auth/logging"
at "github.com/nguyencatpham/go-effective-study/pkg/api/auth/transport"
"github.com/nguyencatpham/go-effective-study/pkg/api/password"
pl "github.com/nguyencatpham/go-effective-study/pkg/api/password/logging"
pt "github.com/nguyencatpham/go-effective-study/pkg/api/password/transport"
"github.com/nguyencatpham/go-effective-study/pkg/api/product"
prl "github.com/nguyencatpham/go-effective-study/pkg/api/product/logging"
prt "github.com/nguyencatpham/go-effective-study/pkg/api/product/transport"
"github.com/nguyencatpham/go-effective-study/pkg/api/topic"
tl "github.com/nguyencatpham/go-effective-study/pkg/api/topic/logging"
tt "github.com/nguyencatpham/go-effective-study/pkg/api/topic/transport"
"github.com/nguyencatpham/go-effective-study/pkg/api/user"
ul "github.com/nguyencatpham/go-effective-study/pkg/api/user/logging"
ut "github.com/nguyencatpham/go-effective-study/pkg/api/user/transport"
"github.com/nguyencatpham/go-effective-study/pkg/utl/config"
"github.com/nguyencatpham/go-effective-study/pkg/utl/middleware/jwt"
"github.com/nguyencatpham/go-effective-study/pkg/utl/postgres"
"github.com/nguyencatpham/go-effective-study/pkg/utl/rbac"
"github.com/nguyencatpham/go-effective-study/pkg/utl/secure"
"github.com/nguyencatpham/go-effective-study/pkg/utl/server"
)
// Start starts the API service
func Start(cfg *config.Configuration) error {
db, err := postgres.New(cfg.DB.PSN, cfg.DB.Timeout, cfg.DB.LogQueries)
if err != nil {
return err
}
sec := secure.New(cfg.App.MinPasswordStr, sha1.New())
rbac := rbac.New()
jwt := jwt.New(cfg.JWT.Secret, cfg.JWT.SigningAlgorithm, cfg.JWT.Duration)
log := zlog.New()
e := server.New()
e.Static("/api-docs", cfg.App.SwaggerUIPath)
at.NewHTTP(al.New(auth.Initialize(db, jwt, sec, rbac), log), e, jwt.MWFunc())
v1 := e.Group("/v1")
// v1.Use(jwt.MWFunc())
ut.NewHTTP(ul.New(user.Initialize(db, rbac, sec), log), v1)
pt.NewHTTP(pl.New(password.Initialize(db, rbac, sec), log), v1)
tt.NewHTTP(tl.New(topic.Initialize(db, rbac, sec), log), v1)
prt.NewHTTP(prl.New(product.Initialize(db, rbac, sec), nil), v1)
server.Start(e, &server.Config{
Port: cfg.Server.Port,
ReadTimeoutSeconds: cfg.Server.ReadTimeout,
WriteTimeoutSeconds: cfg.Server.WriteTimeout,
Debug: cfg.Server.Debug,
})
return nil
}