-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
129 lines (105 loc) · 4.47 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) 2020, Marcelo Jorge Vieira (https://github.com/mfinancecombr)
// Licensed under the BSD 3-Clause License
package api
import (
"fmt"
"net/http"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/mfinancecombr/finance-wallet-api/db"
_ "github.com/mfinancecombr/finance-wallet-api/docs" // docs is generated by Swag CLI
"github.com/spf13/viper"
echoSwagger "github.com/swaggo/echo-swagger"
"gopkg.in/go-playground/validator.v9"
)
const Version = "0.1.0"
type Server interface {
http.Handler
Start()
}
type server struct {
*echo.Echo
db db.DB
}
func (s *server) Start() {
addr := fmt.Sprintf(":%d", viper.GetInt("port"))
s.Echo.Logger.Fatal(s.Echo.Start(addr))
}
type CustomValidator struct {
validator *validator.Validate
}
func (cv *CustomValidator) Validate(i interface{}) error {
return cv.validator.Struct(i)
}
func NewServerFromDB() (Server, error) {
echoInstance := echo.New()
echoInstance.HideBanner = true
dbInstance, err := db.NewMongoSession()
if err != nil {
return nil, err
}
server := &server{
Echo: echoInstance,
db: dbInstance,
}
echoInstance.Use(
middleware.LoggerWithConfig(
middleware.LoggerConfig{
Format: "timestamp=${time_rfc3339} " +
"method=${method} " +
"request_uri=${uri} " +
"status=${status} " +
"request_id=${id} " +
"latency=${latency_human}\n",
},
),
)
echoInstance.Use(middleware.Recover())
// FIXME
echoInstance.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowMethods: []string{
echo.GET, echo.OPTIONS, echo.POST, echo.DELETE, echo.PUT,
},
}))
echoInstance.Pre(middleware.RemoveTrailingSlash())
echoInstance.Validator = &CustomValidator{validator: validator.New()}
echoInstance.File("/favicon.ico", "images/favicon.ico")
echoInstance.GET("/", server.index)
echoInstance.GET("/healthcheck", server.healthcheck)
echoInstance.GET("/swagger/*", echoSwagger.WrapHandler)
echoInstance.Static("/static/icons", "images/icons")
echoInstance.GET("/api/v1/operations", server.getAllOperations)
echoInstance.DELETE("/api/v1/operations/:id", server.deleteOperationByID)
echoInstance.GET("/api/v1/purchases", server.getAllPurchases)
echoInstance.GET("/api/v1/sales", server.getAllSales)
echoInstance.DELETE("/api/v1/brokers/:id", server.brokersDelete)
echoInstance.GET("/api/v1/brokers", server.brokers)
echoInstance.GET("/api/v1/brokers/:id", server.broker)
echoInstance.POST("/api/v1/brokers", server.brokersAdd)
echoInstance.PUT("/api/v1/brokers/:id", server.brokersUpdate)
echoInstance.DELETE("/api/v1/portfolios/:id", server.portfoliosDelete)
echoInstance.GET("/api/v1/portfolios", server.portfolios)
echoInstance.GET("/api/v1/portfolios/:id", server.portfolio)
echoInstance.POST("/api/v1/portfolios", server.portfoliosAdd)
echoInstance.PUT("/api/v1/portfolios/:id", server.portfoliosUpdate)
echoInstance.GET("/api/v1/stocks/operations/:id", server.getStockOperationByID)
echoInstance.POST("/api/v1/stocks/operations", server.insertStockOperation)
echoInstance.PUT("/api/v1/stocks/operations/:id", server.updateStockOperationByID)
echoInstance.GET("/api/v1/fiis/operations/:id", server.getFIIOperationByID)
echoInstance.POST("/api/v1/fiis/operations", server.insertFIIOperation)
echoInstance.PUT("/api/v1/fiis/operations/:id", server.updateFIIOperationByID)
echoInstance.GET("/api/v1/treasuries-direct/operations/:id", server.getTreasuryDirectOperationByID)
echoInstance.POST("/api/v1/treasuries-direct/operations", server.insertTreasuryDirectOperation)
echoInstance.PUT("/api/v1/treasuries-direct/operations/:id", server.updateTreasuryDirectOperationByID)
echoInstance.GET("/api/v1/certificates-of-deposit/operations/:id", server.getCertificateOfDepositOperationByID)
echoInstance.POST("/api/v1/certificates-of-deposit/operations", server.insertCertificateOfDepositOperation)
echoInstance.PUT("/api/v1/certificates-of-deposit/operations/:id", server.updateCertificateOfDepositOperationByID)
echoInstance.GET("/api/v1/stocks-funds/operations/:id", server.getStockFundOperationByID)
echoInstance.POST("/api/v1/stocks-funds/operations", server.insertStockFundOperation)
echoInstance.PUT("/api/v1/stocks-funds/operations/:id", server.updateStockFundOperationByID)
echoInstance.GET("/api/v1/ficfi/operations/:id", server.getFICFIOperationByID)
echoInstance.POST("/api/v1/ficfi/operations", server.insertFICFIOperation)
echoInstance.PUT("/api/v1/ficfi/operations/:id", server.updateFICFIOperationByID)
return server, nil
}