-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
81 lines (71 loc) · 1.66 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
package main
import (
"embed"
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"time"
"github.com/gin-gonic/gin"
"gopkg.in/yaml.v2"
"hsott.cn/UniAnalytics/util"
)
//go:embed html
var html embed.FS
type Config struct {
Title string `yaml:"title"`
Card_title string `yaml:"card_title"`
Card_text string `yaml:"card_text"`
}
func init() {
fmt.Println("===== UniAnalytics v0.2.2 =====")
log.Println("初始化中...")
t := time.Now()
util.InitConfig()
log.Println("===========================")
util.InitSql()
log.Println("初始化完成,耗时", time.Since(t))
log.Println("===========================")
}
func main() {
log.Println("启动服务中...")
t := time.Now()
//读取配置文件
conf := &Config{}
data, err := ioutil.ReadFile("./config.yaml")
if err != nil {
return
}
if err := yaml.Unmarshal([]byte(data), &conf); err != nil {
return
}
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
// r.LoadHTMLGlob("html/*")
template, _ := template.ParseFS(html, "html/*.html")
r.SetHTMLTemplate(template)
r.GET("/", func(ctx *gin.Context) {
j := ctx.Query("j")
jumpType := ctx.Query("type")
if j == "" {
ctx.String(http.StatusOK, "参数错误")
return
}
if jumpType == "" {
ctx.HTML(http.StatusOK, "jump.html", gin.H{
"url": j,
"title": conf.Title,
"card_title": conf.Card_title,
"card_text": conf.Card_text,
})
return
}
util.AddSql(j, ctx.ClientIP())
ctx.Redirect(http.StatusMovedPermanently, j)
})
log.Println("启动服务完成,耗时", time.Since(t))
log.Println("启动成功: http://127.0.0.1:8080 ")
log.Println("===========================")
r.Run(":8080")
}