Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
Quang nd committed Sep 23, 2024
1 parent 19ab4b1 commit 11fec99
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 10 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Makefile
*.md
.github
.vscode
Empty file removed elasticsearch.txt
Empty file.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
64 changes: 64 additions & 0 deletions internal/api/handlers/health.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package handlers

import (
"bytes"
"encoding/json"
"fmt"
"net/http"

"github.com/gin-gonic/gin"
Expand All @@ -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{})
}
3 changes: 2 additions & 1 deletion internal/api/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
16 changes: 10 additions & 6 deletions internal/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
Expand All @@ -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:
Expand All @@ -27,20 +35,16 @@ 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")
}
}

func ErrorHandler() gin.HandlerFunc {
return func(c *gin.Context) {
c.Next()

status := c.Writer.Status()
if status < 500 {
return
Expand Down
3 changes: 0 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 11fec99

Please sign in to comment.