-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp_echo.go
94 lines (77 loc) · 2.08 KB
/
http_echo.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
89
90
91
92
93
94
package main
import (
"fmt"
"log"
"net/http"
"git.restream.ru/itv-backend/reindexer/benchmarks/repo"
"github.com/labstack/echo"
)
func EchoQueryGetByIDHandler(ctx echo.Context) error {
r := ctx.Param("repo")
item := repo.Get(r).QueryByID(1, false)
return ctx.JSON(http.StatusOK, item)
}
func EchoQuery1CondHandler(ctx echo.Context) error {
r := ctx.Param("repo")
items := repo.Get(r).Query1Cond(1, false, 10)
return ctx.JSON(http.StatusOK, items)
}
func EchoQuery2CondHandler(ctx echo.Context) error {
r := ctx.Param("repo")
items := repo.Get(r).Query2Cond(1, false, 10)
return ctx.JSON(http.StatusOK, items)
}
func EchoQueryTextHandler(ctx echo.Context) error {
r := ctx.Param("repo")
dsl := ""
switch r {
case "elastic", "mysql", "reindex":
dsl = "%s %s"
case "sqlite":
dsl = "%s OR %s"
case "sphinx":
dsl = "%s | %s"
case "mongo":
dsl = "%s %s"
case "arango":
dsl = "%s,|%s"
}
items := repo.Get(r).QueryFullText(func() string { return fmt.Sprintf(dsl, randStringWord(), randStringWord()) }, 1, 10)
return ctx.JSON(http.StatusOK, items)
}
func EchoQueryTextPrefixHandler(ctx echo.Context) error {
r := ctx.Param("repo")
dsl := ""
switch r {
case "elastic", "mysql", "reindex":
dsl = "%s* %s*"
case "sqlite":
dsl = "%s* OR %s*"
case "sphinx":
dsl = "%s* | %s*"
case "arango":
dsl = "prefix:%s,|prefix:%s"
}
items := repo.Get(r).QueryFullText(func() string { return fmt.Sprintf(dsl, randStringPref(), randStringPref()) }, 1, 10)
return ctx.JSON(http.StatusOK, items)
}
func EchoUpdateHandler(ctx echo.Context) error {
r := ctx.Param("repo")
repo.Get(r).Update(1)
return ctx.String(http.StatusOK, "{}")
}
func StartEchoHTTP() {
e := echo.New()
e.HideBanner = true
e.GET("/byid/:repo", EchoQueryGetByIDHandler)
e.GET("/1cond/:repo", EchoQuery1CondHandler)
e.GET("/2cond/:repo", EchoQuery2CondHandler)
e.GET("/text/:repo", EchoQueryTextHandler)
e.GET("/text_prefix/:repo", EchoQueryTextPrefixHandler)
e.GET("/update/:repo", EchoUpdateHandler)
log.Printf("Starting listen echo on 8082")
err := e.Start(":8082")
if err != nil {
panic(err)
}
}