-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
63 lines (53 loc) · 1.01 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/streadway/amqp"
"log"
"net/http"
"os"
)
type Request struct {
URL string `json:"url"`
}
var channelAmqp *amqp.Channel
func init() {
amqpConnection, err := amqp.Dial(os.Getenv("RABBITMQ_URI"))
if err != nil {
log.Fatal(err)
}
channelAmqp, _ = amqpConnection.Channel()
}
func main() {
router := gin.Default()
router.POST("/parse", ParserHandler)
log.Fatal(router.Run(":6000"))
}
func ParserHandler(c *gin.Context) {
var request Request
if err := c.ShouldBindJSON(&request); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
return
}
data, _ := json.Marshal(request)
err := channelAmqp.Publish(
"",
os.Getenv("RABBITMQ_QUEUE"),
false,
false,
amqp.Publishing{
ContentType: "application/json",
Body: data,
})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"message": "success",
})
}