From 78110715d2bc39b40d3bc715fa96e171cabe5549 Mon Sep 17 00:00:00 2001 From: Aleksandr Soloshenko Date: Wed, 7 Aug 2024 18:01:38 +0700 Subject: [PATCH] [logs] add empty `/logs` endpoint --- api/local.http | 5 +- api/requests.http | 4 + api/swagger.json | 116 +++++++++++++++++- api/swagger.yaml | 83 ++++++++++++- go.mod | 4 +- go.sum | 6 +- internal/sms-gateway/handlers/3rdparty.go | 5 + .../sms-gateway/handlers/logs/3rdparty.go | 53 ++++++++ internal/sms-gateway/handlers/module.go | 2 + 9 files changed, 269 insertions(+), 9 deletions(-) create mode 100644 internal/sms-gateway/handlers/logs/3rdparty.go diff --git a/api/local.http b/api/local.http index 55b313d..2006322 100644 --- a/api/local.http +++ b/api/local.http @@ -69,7 +69,7 @@ Content-Type: application/json { "id": "LreFUt-Z3sSq0JufY9uWB", - "url": "https://webhook.site/280a6655-eb68-40b9-b857-af5be37c5303", + "url": "https://webhook.site/7598bc37-8765-4e7d-89e0-01e7f5ea5346", "event": "sms:received" } @@ -77,3 +77,6 @@ Content-Type: application/json DELETE {{localUrl}}/webhooks/LreFUt-Z3sSq0JufY9uWB HTTP/1.1 Authorization: Basic {{localCredentials}} +### +GET {{localUrl}}/logs?from=2024-08-01T13:19:02.093%2B07:00 HTTP/1.1 +Authorization: Basic {{localCredentials}} diff --git a/api/requests.http b/api/requests.http index 4d2256b..711b4a1 100644 --- a/api/requests.http +++ b/api/requests.http @@ -77,6 +77,10 @@ Content-Type: application/json DELETE {{baseUrl}}/api/3rdparty/v1/webhooks/MYofX8bTd5Bov0wWFZLRP HTTP/1.1 Authorization: Basic {{credentials}} +### +GET {{baseUrl}}/api/3rdparty/v1/logs HTTP/1.1 +Authorization: Basic {{credentials}} + ### GET http://localhost:3000/metrics HTTP/1.1 diff --git a/api/swagger.json b/api/swagger.json index c93bb8e..293131c 100644 --- a/api/swagger.json +++ b/api/swagger.json @@ -15,7 +15,7 @@ "host": "sms.capcom.me", "basePath": "/api", "paths": { - "/3rdparty/v1/device": { + "/3rdparty/v1/devices": { "get": { "security": [ { @@ -87,6 +87,69 @@ } } }, + "/3rdparty/v1/logs": { + "get": { + "security": [ + { + "ApiAuth": [] + } + ], + "description": "Retrieve a list of log entries within a specified time range.", + "produces": [ + "application/json" + ], + "tags": [ + "System", + "Logs" + ], + "summary": "Get logs", + "parameters": [ + { + "type": "string", + "format": "date-time", + "description": "The start of the time range for the logs to retrieve. Logs created after this timestamp will be included.", + "name": "from", + "in": "query" + }, + { + "type": "string", + "format": "date-time", + "description": "The end of the time range for the logs to retrieve. Logs created before this timestamp will be included.", + "name": "to", + "in": "query" + } + ], + "responses": { + "200": { + "description": "Log entries", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/smsgateway.LogEntry" + } + } + }, + "401": { + "description": "Unauthorized", + "schema": { + "$ref": "#/definitions/smsgateway.ErrorResponse" + } + }, + "500": { + "description": "Internal server error", + "schema": { + "$ref": "#/definitions/smsgateway.ErrorResponse" + } + }, + "501": { + "description": "Not implemented", + "schema": { + "$ref": "#/definitions/smsgateway.ErrorResponse" + } + } + } + } + }, "/3rdparty/v1/message": { "get": { "security": [ @@ -773,6 +836,57 @@ "HealthStatusFail" ] }, + "smsgateway.LogEntry": { + "type": "object", + "properties": { + "context": { + "description": "Additional context information related to the log entry, typically including data relevant to the log event.", + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "createdAt": { + "description": "The timestamp when this log entry was created.", + "type": "string" + }, + "id": { + "description": "A unique identifier for the log entry.", + "type": "integer" + }, + "message": { + "description": "A message describing the log event.", + "type": "string" + }, + "module": { + "description": "The module or component of the system that generated the log entry.", + "type": "string" + }, + "priority": { + "description": "The priority level of the log entry.", + "allOf": [ + { + "$ref": "#/definitions/smsgateway.LogEntryPriority" + } + ] + } + } + }, + "smsgateway.LogEntryPriority": { + "type": "string", + "enum": [ + "DEBUG", + "INFO", + "WARN", + "ERROR" + ], + "x-enum-varnames": [ + "LogEntryPriorityDebug", + "LogEntryPriorityInfo", + "LogEntryPriorityWarn", + "LogEntryPriorityError" + ] + }, "smsgateway.Message": { "type": "object", "required": [ diff --git a/api/swagger.yaml b/api/swagger.yaml index ddc7ab4..ec5f3d9 100644 --- a/api/swagger.yaml +++ b/api/swagger.yaml @@ -92,6 +92,44 @@ definitions: - HealthStatusPass - HealthStatusWarn - HealthStatusFail + smsgateway.LogEntry: + properties: + context: + additionalProperties: + type: string + description: Additional context information related to the log entry, typically + including data relevant to the log event. + type: object + createdAt: + description: The timestamp when this log entry was created. + type: string + id: + description: A unique identifier for the log entry. + type: integer + message: + description: A message describing the log event. + type: string + module: + description: The module or component of the system that generated the log + entry. + type: string + priority: + allOf: + - $ref: '#/definitions/smsgateway.LogEntryPriority' + description: The priority level of the log entry. + type: object + smsgateway.LogEntryPriority: + enum: + - DEBUG + - INFO + - WARN + - ERROR + type: string + x-enum-varnames: + - LogEntryPriorityDebug + - LogEntryPriorityInfo + - LogEntryPriorityWarn + - LogEntryPriorityError smsgateway.Message: properties: id: @@ -329,7 +367,7 @@ info: title: SMS Gateway for Androidâ„¢ API version: '{APP_VERSION}' paths: - /3rdparty/v1/device: + /3rdparty/v1/devices: get: description: Returns list of registered devices produces: @@ -375,6 +413,49 @@ paths: summary: Health check tags: - System + /3rdparty/v1/logs: + get: + description: Retrieve a list of log entries within a specified time range. + parameters: + - description: The start of the time range for the logs to retrieve. Logs created + after this timestamp will be included. + format: date-time + in: query + name: from + type: string + - description: The end of the time range for the logs to retrieve. Logs created + before this timestamp will be included. + format: date-time + in: query + name: to + type: string + produces: + - application/json + responses: + "200": + description: Log entries + schema: + items: + $ref: '#/definitions/smsgateway.LogEntry' + type: array + "401": + description: Unauthorized + schema: + $ref: '#/definitions/smsgateway.ErrorResponse' + "500": + description: Internal server error + schema: + $ref: '#/definitions/smsgateway.ErrorResponse' + "501": + description: Not implemented + schema: + $ref: '#/definitions/smsgateway.ErrorResponse' + security: + - ApiAuth: [] + summary: Get logs + tags: + - System + - Logs /3rdparty/v1/message: get: description: Returns message state by ID diff --git a/go.mod b/go.mod index a17f528..db04e93 100644 --- a/go.mod +++ b/go.mod @@ -4,11 +4,12 @@ go 1.22.0 require ( firebase.google.com/go/v4 v4.12.1 - github.com/android-sms-gateway/client-go v1.0.1 + github.com/android-sms-gateway/client-go v1.0.2 github.com/ansrivas/fiberprometheus/v2 v2.6.1 github.com/capcom6/go-helpers v0.0.0-20240521035631-865ee2879fa3 github.com/capcom6/go-infra-fx v0.0.2 github.com/go-playground/validator/v10 v10.16.0 + github.com/go-sql-driver/mysql v1.7.1 github.com/gofiber/fiber/v2 v2.52.5 github.com/jaevor/go-nanoid v1.3.0 github.com/nyaruka/phonenumbers v1.3.0 @@ -35,7 +36,6 @@ require ( github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect - github.com/go-sql-driver/mysql v1.7.1 // indirect github.com/gofiber/adaptor/v2 v2.2.1 // indirect github.com/gofiber/contrib/fiberzap/v2 v2.1.2 // indirect github.com/golang-jwt/jwt/v4 v4.5.0 // indirect diff --git a/go.sum b/go.sum index a9f6c4a..745825a 100644 --- a/go.sum +++ b/go.sum @@ -26,10 +26,8 @@ github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migc github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEVMRuU21PR1EtLVZJmdB18Gu3Rw= github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8D7ML55dXQrVaamCz2vxCfdQBasLZfHKk= -github.com/android-sms-gateway/client-go v1.0.1-0.20240610222412-894fc9370287 h1:4Q6TuWQcTrKb+nyMrdBTBIV0b4R/xQgmOJhOygHWIkg= -github.com/android-sms-gateway/client-go v1.0.1-0.20240610222412-894fc9370287/go.mod h1:DQsReciU1xcaVW3T5Z2bqslNdsAwCFCtghawmA6g6L4= -github.com/android-sms-gateway/client-go v1.0.1 h1:ZqLMJ0MlpYPafU1Vxc2MoEvggzJFtH8wqmk+wpwRmyE= -github.com/android-sms-gateway/client-go v1.0.1/go.mod h1:DQsReciU1xcaVW3T5Z2bqslNdsAwCFCtghawmA6g6L4= +github.com/android-sms-gateway/client-go v1.0.2 h1:kXWzVeSgBu2bM1yN4ac8tTEm0fX2Tqsn+yr9mMNjNfI= +github.com/android-sms-gateway/client-go v1.0.2/go.mod h1:DQsReciU1xcaVW3T5Z2bqslNdsAwCFCtghawmA6g6L4= github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= github.com/ansrivas/fiberprometheus/v2 v2.6.1 h1:wac3pXaE6BYYTF04AC6K0ktk6vCD+MnDOJZ3SK66kXM= diff --git a/internal/sms-gateway/handlers/3rdparty.go b/internal/sms-gateway/handlers/3rdparty.go index eee2238..8c4dfa5 100644 --- a/internal/sms-gateway/handlers/3rdparty.go +++ b/internal/sms-gateway/handlers/3rdparty.go @@ -7,6 +7,7 @@ import ( "github.com/android-sms-gateway/client-go/smsgateway" "github.com/capcom6/sms-gateway/internal/sms-gateway/handlers/base" devicesCtrl "github.com/capcom6/sms-gateway/internal/sms-gateway/handlers/devices" + "github.com/capcom6/sms-gateway/internal/sms-gateway/handlers/logs" "github.com/capcom6/sms-gateway/internal/sms-gateway/handlers/webhooks" "github.com/capcom6/sms-gateway/internal/sms-gateway/models" "github.com/capcom6/sms-gateway/internal/sms-gateway/modules/auth" @@ -30,6 +31,7 @@ type ThirdPartyHandlerParams struct { HealthHandler *healthHandler WebhooksHandler *webhooks.ThirdPartyController DevicesHandler *devicesCtrl.ThirdPartyController + LogsHandler *logs.ThirdPartyController AuthSvc *auth.Service MessagesSvc *messages.Service @@ -45,6 +47,7 @@ type thirdPartyHandler struct { healthHandler *healthHandler webhooksHandler *webhooks.ThirdPartyController devicesHandler *devicesCtrl.ThirdPartyController + logsHandler *logs.ThirdPartyController authSvc *auth.Service messagesSvc *messages.Service @@ -170,6 +173,7 @@ func (h *thirdPartyHandler) Register(router fiber.Router) { h.devicesHandler.Register(router.Group("/device")) // TODO: remove after 2025-07-11 h.devicesHandler.Register(router.Group("/devices")) h.webhooksHandler.Register(router.Group("/webhooks")) + h.logsHandler.Register(router.Group("/logs")) } func newThirdPartyHandler(params ThirdPartyHandlerParams) *thirdPartyHandler { @@ -178,6 +182,7 @@ func newThirdPartyHandler(params ThirdPartyHandlerParams) *thirdPartyHandler { healthHandler: params.HealthHandler, webhooksHandler: params.WebhooksHandler, devicesHandler: params.DevicesHandler, + logsHandler: params.LogsHandler, authSvc: params.AuthSvc, messagesSvc: params.MessagesSvc, devicesSvc: params.DevicesSvc, diff --git a/internal/sms-gateway/handlers/logs/3rdparty.go b/internal/sms-gateway/handlers/logs/3rdparty.go new file mode 100644 index 0000000..e663b0c --- /dev/null +++ b/internal/sms-gateway/handlers/logs/3rdparty.go @@ -0,0 +1,53 @@ +package logs + +import ( + "github.com/capcom6/sms-gateway/internal/sms-gateway/handlers/base" + "github.com/capcom6/sms-gateway/internal/sms-gateway/models" + "github.com/capcom6/sms-gateway/internal/sms-gateway/modules/auth" + "github.com/go-playground/validator/v10" + "github.com/gofiber/fiber/v2" + "go.uber.org/fx" + "go.uber.org/zap" +) + +type thirdPartyControllerParams struct { + fx.In + + Validator *validator.Validate + Logger *zap.Logger +} + +type ThirdPartyController struct { + base.Handler +} + +// @Summary Get logs +// @Description Retrieve a list of log entries within a specified time range. +// @Security ApiAuth +// @Tags System, Logs +// @Produce json +// @Param from query string false "The start of the time range for the logs to retrieve. Logs created after this timestamp will be included." Format(date-time) +// @Param to query string false "The end of the time range for the logs to retrieve. Logs created before this timestamp will be included." Format(date-time) +// @Success 200 {object} smsgateway.GetLogsResponse "Log entries" +// @Failure 401 {object} smsgateway.ErrorResponse "Unauthorized" +// @Failure 500 {object} smsgateway.ErrorResponse "Internal server error" +// @Failure 501 {object} smsgateway.ErrorResponse "Not implemented" +// @Router /3rdparty/v1/logs [get] +// +// List webhooks +func (h *ThirdPartyController) get(user models.User, c *fiber.Ctx) error { + return fiber.NewError(fiber.StatusNotImplemented, "For privacy reasons, device's logs are not accessible through Cloud server") +} + +func (h *ThirdPartyController) Register(router fiber.Router) { + router.Get("", auth.WithUser(h.get)) +} + +func NewThirdPartyController(params thirdPartyControllerParams) *ThirdPartyController { + return &ThirdPartyController{ + Handler: base.Handler{ + Logger: params.Logger.Named("logs"), + Validator: params.Validator, + }, + } +} diff --git a/internal/sms-gateway/handlers/module.go b/internal/sms-gateway/handlers/module.go index 209e3d8..cfacb76 100644 --- a/internal/sms-gateway/handlers/module.go +++ b/internal/sms-gateway/handlers/module.go @@ -3,6 +3,7 @@ package handlers import ( "github.com/capcom6/go-infra-fx/http" "github.com/capcom6/sms-gateway/internal/sms-gateway/handlers/devices" + "github.com/capcom6/sms-gateway/internal/sms-gateway/handlers/logs" "github.com/capcom6/sms-gateway/internal/sms-gateway/handlers/webhooks" "go.uber.org/fx" "go.uber.org/zap" @@ -24,6 +25,7 @@ var Module = fx.Module( webhooks.NewThirdPartyController, webhooks.NewMobileController, devices.NewThirdPartyController, + logs.NewThirdPartyController, fx.Private, ), )