Skip to content

Commit

Permalink
[logs] add empty /logs endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
capcom6 committed Aug 8, 2024
1 parent 9ada820 commit 7811071
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 9 deletions.
5 changes: 4 additions & 1 deletion api/local.http
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,14 @@ 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"
}

###
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}}
4 changes: 4 additions & 0 deletions api/requests.http
Original file line number Diff line number Diff line change
Expand Up @@ -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

116 changes: 115 additions & 1 deletion api/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"host": "sms.capcom.me",
"basePath": "/api",
"paths": {
"/3rdparty/v1/device": {
"/3rdparty/v1/devices": {
"get": {
"security": [
{
Expand Down Expand Up @@ -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": [
Expand Down Expand Up @@ -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": [
Expand Down
83 changes: 82 additions & 1 deletion api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
5 changes: 5 additions & 0 deletions internal/sms-gateway/handlers/3rdparty.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -30,6 +31,7 @@ type ThirdPartyHandlerParams struct {
HealthHandler *healthHandler
WebhooksHandler *webhooks.ThirdPartyController
DevicesHandler *devicesCtrl.ThirdPartyController
LogsHandler *logs.ThirdPartyController

AuthSvc *auth.Service
MessagesSvc *messages.Service
Expand All @@ -45,6 +47,7 @@ type thirdPartyHandler struct {
healthHandler *healthHandler
webhooksHandler *webhooks.ThirdPartyController
devicesHandler *devicesCtrl.ThirdPartyController
logsHandler *logs.ThirdPartyController

authSvc *auth.Service
messagesSvc *messages.Service
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
53 changes: 53 additions & 0 deletions internal/sms-gateway/handlers/logs/3rdparty.go
Original file line number Diff line number Diff line change
@@ -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,
},
}
}
Loading

0 comments on commit 7811071

Please sign in to comment.