-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
140 lines (124 loc) · 3.12 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* @Author: Vincent Young
* @Date: 2023-02-07 03:35:39
* @LastEditors: Vincent Yang
* @LastEditTime: 2024-09-08 21:56:09
* @FilePath: /WeiboSearcher/main.go
* @Telegram: https://t.me/missuo
*
* Copyright © 2023 by Vincent, All Rights Reserved.
*/
package main
import (
"fmt"
"io/ioutil"
"net/http"
"regexp"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"gopkg.in/yaml.v2"
"gorm.io/driver/clickhouse"
"gorm.io/gorm"
)
type User struct {
Uid string
Mobile string
}
type Set struct {
ClickhouseConf ClickhouseConf `yaml:"clickhouse"`
WeiboSearcherConf WeiboSearcherConf `yaml:"weiboSearcher"`
}
type ClickhouseConf struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
Username string `yaml:"username"`
Password string `yaml:"password"`
Dbname string `yaml:"dbname"`
}
type WeiboSearcherConf struct {
ListenAddress string `yaml:"listenAddress"`
ListenPort string `yaml:"listenPort"`
}
func (conf *Set) getConf() *Set {
yamlFile, err := ioutil.ReadFile("./config.yml")
if err != nil {
fmt.Println(err.Error())
}
err = yaml.Unmarshal(yamlFile, conf)
if err != nil {
fmt.Println(err.Error())
}
return conf
}
func main() {
// Get Configuration
var set Set
set.getConf()
var clickhouseConf = set.ClickhouseConf
var appConf = set.WeiboSearcherConf
dbHost := clickhouseConf.Host
dbPort := clickhouseConf.Port
dbUsername := clickhouseConf.Username
dbPassword := clickhouseConf.Password
dbName := clickhouseConf.Dbname
appAddress := appConf.ListenAddress
appPort := appConf.ListenPort
// Connect Clickhouse
dsn := "clickhouse://" + dbUsername + ":" + dbPassword + "@" + dbHost + ":" + dbPort + "/" + dbName
db, err := gorm.Open(clickhouse.Open(dsn), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
r.Use(cors.Default())
r.GET("/", func(c *gin.Context) {
// Index Page
c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"message": "This is Weibo SGK. Made by Vincent.",
"usage": "GET/POST to /wb with parameter u",
})
})
r.Any("/wb", func(c *gin.Context) {
re := regexp.MustCompile(`\d+`)
u := c.Query("u")
key := re.FindString(u)
var result User
if len(key) == 10 {
// Input Weibo Uid
db.Raw("SELECT * FROM wb WHERE uid = ? LIMIT 1", key).Scan(&result)
} else if len(key) == 11 {
// Input User Mobile Number
db.Raw("SELECT * FROM wbm WHERE mobile = ? LIMIT 1", key).Scan(&result)
} else {
// Bad Parameters
c.JSON(http.StatusBadRequest, gin.H{
"code": http.StatusBadRequest,
"message": "Bad Parameters",
})
return
}
// No Results
if result.Uid == "" {
c.JSON(http.StatusNotFound, gin.H{
"code": http.StatusNotFound,
"message": "Data Not Found",
})
} else {
c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"uid": result.Uid,
"mobile": result.Mobile,
})
}
})
// Catch-all route to handle undefined paths
r.NoRoute(func(c *gin.Context) {
c.JSON(http.StatusNotFound, gin.H{
"code": http.StatusNotFound,
"message": "Path not found",
})
})
r.Run(appAddress + ":" + appPort)
}