-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (66 loc) · 1.44 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
// Package happy_translate @Description TODO
// @Author jiangyang
// @Created 2023/10/13 18:14
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"happy_translate/baidutranslate"
"log"
"net/http"
)
func main() {
e := gin.Default()
g := e.Group("").Use(Logger(), Auth())
g.GET("/t", Translate)
if err := e.Run(":8080"); err != nil {
log.Fatal("Exit err:", err)
}
}
func Logger() func(*gin.Context) {
return func(ctx *gin.Context) {
log.Println(ctx.RemoteIP(), ctx.Request.RequestURI, ctx.Request.Body)
ctx.Next()
}
}
func Auth() func(*gin.Context) {
return func(ctx *gin.Context) {
token := ctx.GetHeader("TOKEN")
if token != "JIANGYANG" && token != "TEST" {
ctx.JSON(http.StatusUnauthorized, gin.H{
"error_code": 10004,
"error_msg": "未授权访问",
})
ctx.Abort()
}
ctx.Next()
}
}
func Translate(ctx *gin.Context) {
q := ctx.Query("q")
if len(q) == 0 {
ctx.JSON(http.StatusOK, gin.H{
"error_code": 10001,
"error_msg": "非法空值",
})
return
}
result, err := baidutranslate.Translate(q)
if err != nil {
log.Println(err)
ctx.JSON(http.StatusOK, gin.H{
"error_code": 10002,
"error_msg": "查询失败",
})
return
}
if len(result.TransResult) == 0 {
ctx.JSON(http.StatusOK, gin.H{
"error_code": 10003,
"error_msg": "结果为空",
})
return
}
log.Println(result.TransResult)
_, _ = ctx.Writer.Write([]byte(fmt.Sprintf(`%s`, result.TransResult[0].Dst)))
}