|
| 1 | +package applications |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/MrAndreID/goapigateway/configs" |
| 11 | + "github.com/MrAndreID/goapigateway/internal/handlers" |
| 12 | + |
| 13 | + "github.com/MrAndreID/gomiddleware" |
| 14 | + "github.com/MrAndreID/gopackage" |
| 15 | + "github.com/fatih/color" |
| 16 | + "github.com/labstack/echo/v4" |
| 17 | + "github.com/labstack/echo/v4/middleware" |
| 18 | + "github.com/labstack/gommon/log" |
| 19 | + "github.com/sirupsen/logrus" |
| 20 | + "github.com/spf13/cast" |
| 21 | + "github.com/unrolled/secure" |
| 22 | + "go.elastic.co/apm/module/apmechov4" |
| 23 | +) |
| 24 | + |
| 25 | +type Application struct { |
| 26 | + Config *configs.Config |
| 27 | + TimeLocation *time.Location |
| 28 | +} |
| 29 | + |
| 30 | +func Start(toggle bool) any { |
| 31 | + var tag string = "Applications.Main.New." |
| 32 | + |
| 33 | + cfg, err := configs.New(toggle) |
| 34 | + |
| 35 | + if err != nil { |
| 36 | + logrus.WithFields(logrus.Fields{ |
| 37 | + "tag": tag + "01", |
| 38 | + "error": err.Error(), |
| 39 | + }).Error("failed to initiate configuration") |
| 40 | + |
| 41 | + return nil |
| 42 | + } |
| 43 | + |
| 44 | + timeLocation, err := time.LoadLocation(cfg.AppLocation) |
| 45 | + |
| 46 | + if err != nil { |
| 47 | + logrus.WithFields(logrus.Fields{ |
| 48 | + "tag": tag + "02", |
| 49 | + "error": err.Error(), |
| 50 | + }).Error("failed to load location for time") |
| 51 | + |
| 52 | + return nil |
| 53 | + } |
| 54 | + |
| 55 | + app := Application{ |
| 56 | + Config: cfg, |
| 57 | + TimeLocation: timeLocation, |
| 58 | + } |
| 59 | + |
| 60 | + echo.NotFoundHandler = func(c echo.Context) error { |
| 61 | + logrus.WithFields(logrus.Fields{ |
| 62 | + "tag": tag + "03", |
| 63 | + }).Error("route not found") |
| 64 | + |
| 65 | + return c.JSON(http.StatusNotFound, map[string]string{ |
| 66 | + "code": fmt.Sprintf("%04d", http.StatusNotFound), |
| 67 | + "description": strings.ToUpper(strings.ReplaceAll(http.StatusText(http.StatusNotFound), " ", "_")), |
| 68 | + }) |
| 69 | + } |
| 70 | + |
| 71 | + echo.MethodNotAllowedHandler = func(c echo.Context) error { |
| 72 | + logrus.WithFields(logrus.Fields{ |
| 73 | + "tag": tag + "04", |
| 74 | + }).Error("method not allowed") |
| 75 | + |
| 76 | + return c.JSON(http.StatusMethodNotAllowed, map[string]string{ |
| 77 | + "code": fmt.Sprintf("%04d", http.StatusMethodNotAllowed), |
| 78 | + "description": strings.ToUpper(strings.ReplaceAll(http.StatusText(http.StatusMethodNotAllowed), " ", "_")), |
| 79 | + }) |
| 80 | + } |
| 81 | + |
| 82 | + e := echo.New() |
| 83 | + |
| 84 | + e.Validator = gopackage.CustomValidator() |
| 85 | + |
| 86 | + e.HTTPErrorHandler = gopackage.EchoCustomHTTPErrorHandler |
| 87 | + |
| 88 | + e.JSONSerializer = gopackage.CustomJSON() |
| 89 | + |
| 90 | + e.Pre(middleware.RemoveTrailingSlash()) |
| 91 | + |
| 92 | + e.Pre(gomiddleware.EchoSetRequestID) |
| 93 | + |
| 94 | + e.Use(apmechov4.Middleware()) |
| 95 | + |
| 96 | + e.Use(middleware.Recover()) |
| 97 | + |
| 98 | + e.Use(middleware.SecureWithConfig(middleware.SecureConfig{ |
| 99 | + XSSProtection: "1; mode=block", |
| 100 | + ContentTypeNosniff: "nosniff", |
| 101 | + XFrameOptions: "SAMEORIGIN", |
| 102 | + HSTSMaxAge: 3600, |
| 103 | + ContentSecurityPolicy: "default-src 'self'", |
| 104 | + })) |
| 105 | + |
| 106 | + secureMiddleware := secure.Options{ |
| 107 | + SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"}, |
| 108 | + STSSeconds: 63072000, |
| 109 | + STSIncludeSubdomains: true, |
| 110 | + STSPreload: true, |
| 111 | + ForceSTSHeader: true, |
| 112 | + IsDevelopment: true, |
| 113 | + } |
| 114 | + |
| 115 | + e.Use(echo.WrapMiddleware(secure.New(secureMiddleware).Handler)) |
| 116 | + |
| 117 | + e.Use(middleware.CORSWithConfig(middleware.CORSConfig{ |
| 118 | + AllowOrigins: cfg.AllowedOrigins, |
| 119 | + AllowHeaders: []string{"*"}, |
| 120 | + AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE}, |
| 121 | + })) |
| 122 | + |
| 123 | + e.Use(gomiddleware.EchoSetNoCache) |
| 124 | + |
| 125 | + e.Use(gomiddleware.EchoSetMaintenanceMode("storages/maintenance.flag")) |
| 126 | + |
| 127 | + e.Use(func(next echo.HandlerFunc) echo.HandlerFunc { |
| 128 | + return func(c echo.Context) error { |
| 129 | + start := time.Now().In(timeLocation) |
| 130 | + |
| 131 | + err := next(c) |
| 132 | + |
| 133 | + stop := time.Now().In(timeLocation) |
| 134 | + |
| 135 | + dateTime := stop.Format(time.DateTime) |
| 136 | + |
| 137 | + redBackground := color.New(color.FgWhite).Add(color.BgRed).SprintFunc() |
| 138 | + greenBackground := color.New(color.FgWhite).Add(color.BgGreen).SprintFunc() |
| 139 | + |
| 140 | + redForeground := color.New(color.FgRed).SprintFunc() |
| 141 | + greenForeground := color.New(color.FgGreen).SprintFunc() |
| 142 | + |
| 143 | + var status, statusType string |
| 144 | + |
| 145 | + if c.Response().Status >= 400 { |
| 146 | + status = redBackground(" " + strconv.Itoa(c.Response().Status) + " ") |
| 147 | + |
| 148 | + statusType = redForeground("[ ERROR ]") |
| 149 | + } else { |
| 150 | + status = greenBackground(" " + strconv.Itoa(c.Response().Status) + " ") |
| 151 | + |
| 152 | + statusType = greenForeground("[ SUCCESS ]") |
| 153 | + } |
| 154 | + |
| 155 | + fmt.Printf( |
| 156 | + "%s %s %s %s %s | %s | %s\n", |
| 157 | + dateTime, |
| 158 | + status, |
| 159 | + statusType, |
| 160 | + c.Request().Method, |
| 161 | + c.Request().URL.String(), |
| 162 | + stop.Sub(start).String(), |
| 163 | + cast.ToString(c.Get("RequestID")), |
| 164 | + ) |
| 165 | + |
| 166 | + return err |
| 167 | + } |
| 168 | + }) |
| 169 | + |
| 170 | + if cfg.AppDebug { |
| 171 | + e.Logger.SetLevel(log.DEBUG) |
| 172 | + |
| 173 | + e.Debug = true |
| 174 | + } |
| 175 | + |
| 176 | + initService(&app) |
| 177 | + |
| 178 | + if toggle { |
| 179 | + handlers.NewGatewayHandler(e, GatewayService) |
| 180 | + |
| 181 | + return e.Start(":" + cfg.AppPort) |
| 182 | + } |
| 183 | + |
| 184 | + return e |
| 185 | +} |
0 commit comments