diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..0bdc6b9 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +Makefile +*.md +.github +.vscode \ No newline at end of file diff --git a/elasticsearch.txt b/elasticsearch.txt deleted file mode 100644 index e69de29..0000000 diff --git a/go.mod b/go.mod index 1a7b5c4..1072a36 100644 --- a/go.mod +++ b/go.mod @@ -25,6 +25,7 @@ require ( github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.20.0 // indirect github.com/goccy/go-json v0.10.2 // indirect + github.com/google/uuid v1.6.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.2.7 // indirect github.com/kr/pretty v0.1.0 // indirect diff --git a/go.sum b/go.sum index b1b12e3..889e795 100644 --- a/go.sum +++ b/go.sum @@ -40,6 +40,8 @@ github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5x github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= diff --git a/internal/api/handlers/health.go b/internal/api/handlers/health.go index 7f0d466..01a0b15 100644 --- a/internal/api/handlers/health.go +++ b/internal/api/handlers/health.go @@ -1,6 +1,9 @@ package handlers import ( + "bytes" + "encoding/json" + "fmt" "net/http" "github.com/gin-gonic/gin" @@ -19,3 +22,64 @@ func HealthCheck(c *gin.Context) { "buildTime": BuildTime, }) } + +type TelegramMessagePayload struct { + UpdateID int64 `json:"update_id"` + Message Message `json:"message"` +} + +type Message struct { + MessageID int64 `json:"message_id"` + From User `json:"from"` + Chat Chat `json:"chat"` + Date int64 `json:"date"` + Text string `json:"text"` +} + +type User struct { + ID int64 `json:"id"` + IsBot bool `json:"is_bot"` + FirstName string `json:"first_name"` + Username string `json:"username"` + LanguageCode string `json:"language_code"` +} + +type Chat struct { + ID int64 `json:"id"` + FirstName string `json:"first_name"` + Username string `json:"username"` + Type string `json:"type"` +} +type RequestMessage struct { + ChatID int64 `json:"chat_id"` + Text string `json:"text"` +} + +func HandleTelegramBotMessage(c *gin.Context) { + var payload TelegramMessagePayload + if err := c.ShouldBind(&payload); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + triggerUserId := payload.Message.From.Username + message := payload.Message.Text + + data := RequestMessage{ + ChatID: payload.Message.Chat.ID, + Text: message + ` build is triggered by ` + triggerUserId + `.`, + } + + jsonData, err := json.Marshal(data) + if err != nil { + fmt.Println("Error marshaling JSON:", err) + return + } + response, err := http.Post("https://api.telegram.org/bot6654034396:AAEc3hoa3r11NRfMb9ALhXmjWjNzOvozEds/sendMessage", "application/json", bytes.NewBuffer(jsonData)) + if err != nil { + fmt.Println("Error:", err) + return + } + defer response.Body.Close() + c.JSON(http.StatusOK, gin.H{}) +} diff --git a/internal/api/routes/routes.go b/internal/api/routes/routes.go index 6b1de2a..946282d 100644 --- a/internal/api/routes/routes.go +++ b/internal/api/routes/routes.go @@ -7,6 +7,7 @@ import ( ) func SetupRoutes(router *gin.Engine) { - router.GET("/health", handlers.HealthCheck) + router.POST("/", handlers.HandleTelegramBotMessage) + router.POST("/health", handlers.HealthCheck) AddSearchRoutes(router) } diff --git a/internal/middleware/middleware.go b/internal/middleware/middleware.go index 692393e..a01ad7c 100644 --- a/internal/middleware/middleware.go +++ b/internal/middleware/middleware.go @@ -4,6 +4,7 @@ import ( "time" "github.com/gin-gonic/gin" + "github.com/google/uuid" "github.com/rs/zerolog" "github.com/rs/zerolog/log" ) @@ -12,11 +13,18 @@ func Logger() gin.HandlerFunc { return func(c *gin.Context) { start := time.Now() raw := c.Request.URL.RawQuery + id := uuid.NewString() + + log.Info(). + Str("id", id). + Str("method", c.Request.Method). + Str("path", c.Request.URL.Path). + Msg(raw) c.Next() - latency := time.Since(start).Milliseconds() var zeroLog *zerolog.Event + latency := time.Since(start).Milliseconds() status := c.Writer.Status() switch { case status < 400: @@ -27,12 +35,9 @@ func Logger() gin.HandlerFunc { zeroLog = log.Error() } zeroLog. + Str("id", id). Int("status", c.Writer.Status()). Int64("latency", latency). - Str("method", c.Request.Method). - Str("path", c.Request.URL.Path). - Str("method", c.Request.Method). - Str("query", raw). Msg("HTTP request completed") } } @@ -40,7 +45,6 @@ func Logger() gin.HandlerFunc { func ErrorHandler() gin.HandlerFunc { return func(c *gin.Context) { c.Next() - status := c.Writer.Status() if status < 500 { return diff --git a/readme.md b/readme.md index a5964bf..e69de29 100644 --- a/readme.md +++ b/readme.md @@ -1,3 +0,0 @@ -choco install gh -docker run -p 9200:9200 -d --name elasticsearch --network elastic-net -e ELASTIC_PASSWORD=malgus123 -e "discovery.type=single-node" -e "xpack.security.http.ssl.enabled=false" -e "xpack.license.self_generated.type=trial" docker.elastic.co/elasticsearch/elasticsearch:8.15.0 -go get github.com/elastic/go-elasticsearch/v8 \ No newline at end of file