-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (51 loc) · 1.31 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
package main
import (
"log"
"net/http"
elastic "sokemotor/controller"
"sokemotor/models"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
func main() {
_ = elastic.Client()
app := gin.Default()
corsConfig := cors.DefaultConfig()
corsConfig.AllowOrigins = []string{"*"}
corsConfig.AllowCredentials = true
app.Use(cors.New(corsConfig))
app.GET("/search/:query", searchHandler)
app.POST("/document", documentHandler)
app.POST("/indexHTML", processHTML)
app.Run("0.0.0.0:8080")
}
func searchHandler(c *gin.Context) {
queryString := c.Param("query")
log.Printf("searching for: %v\n", queryString)
val, _ := elastic.SearchIndex(queryString)
c.JSON(http.StatusAccepted, val)
}
func documentHandler(c *gin.Context) {
c.String(http.StatusNotImplemented, "custom docs not yet implemented.")
}
func processHTML(c *gin.Context) {
var v models.HtmlDocument
err := c.Bind(&v)
if err != nil {
log.Printf("indexHTML Error: %v", err)
c.String(http.StatusBadRequest, "%v\n", err)
return
}
// Index into es node.
err = elastic.InsertIntoIndex(v)
if err != nil {
log.Printf("indexHTML Error: %v", err)
c.String(http.StatusInternalServerError, "%v", err)
return
}
// Acknowledge that document is created.
c.JSON(http.StatusCreated, gin.H{
"url": v.Url,
"Created": "true",
})
}