This repository has been archived by the owner on Apr 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
88 lines (68 loc) · 1.51 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//This is a task for Go programmer vacancy at Geeks.Team
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
var router *gin.Engine
type Request struct { // Request from client
Site []string
SearchText string
}
type Response struct { // Response to client
FoundAtSite string
}
var req Request
var resp Response
func main() {
// Set the router as the default one provided by Gin
router = gin.Default()
router.POST("/checktext", checkText)
// Start serving the application
router.Run()
}
// Handler to POST /checktext
func checkText(c *gin.Context) {
// Decoding json into Request struct:
decoder := json.NewDecoder(c.Request.Body)
err := decoder.Decode(&req)
if err != nil {
panic(err)
}
defer c.Request.Body.Close()
resp.FoundAtSite = ""
// Searching the requsted text on the websites
for i := 0; i < len(req.Site); i++ {
res, err := http.Get(req.Site[i])
if err != nil {
log.Fatalln("Get error: ", err)
}
body, err1 := ioutil.ReadAll(res.Body)
if err1 != nil {
log.Fatalln("Body error: ", err)
}
str := string(body)
if strings.Contains(str, req.SearchText) {
resp.FoundAtSite = req.Site[i]
break
}
}
// Analysis of search process:
status := http.StatusOK
if resp.FoundAtSite == "" {
status = http.StatusNoContent
}
// Sending response:
Render(c, status, gin.H{
"response": resp})
}
// Response function
func Render(c *gin.Context, status int, data gin.H) {
c.JSON(
status,
data["response"])
}