Skip to content

Commit

Permalink
Feat: automatically obtain datasets from data sources at regular inte…
Browse files Browse the repository at this point in the history
…rvals (#6)

* Feat: auto update
* Chore: rename import package name
* Feat: log display on data update
  • Loading branch information
AH-dark authored Jul 20, 2022
1 parent 6820f25 commit a612eef
Show file tree
Hide file tree
Showing 14 changed files with 242 additions and 17 deletions.
9 changes: 8 additions & 1 deletion bootstrap/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@ import (
"github.com/lezi-wiki/lezi-api/pkg/conf"
"github.com/lezi-wiki/lezi-api/pkg/text"
"github.com/lezi-wiki/lezi-api/pkg/util"
"github.com/lezi-wiki/lezi-api/services/remote"
)

func Init(dataPath string, data string, confPath string) {
func Init(dataPath string, data string, confPath string, updateEndpoint string) {
util.Log()

// 初始化配置文件
conf.Init(confPath)

// 初始化数据
text.Init(dataPath, data)

// 设置更新
remote.Endpoint = updateEndpoint

// Debug 关闭时,切换为生产模式
if !conf.SystemConfig.Debug {
gin.SetMode(gin.ReleaseMode)
Expand Down
4 changes: 2 additions & 2 deletions controller/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/lezi-wiki/lezi-api/model"
"github.com/lezi-wiki/lezi-api/pkg/response"
"github.com/lezi-wiki/lezi-api/pkg/text"
"github.com/lezi-wiki/lezi-api/services"
textService "github.com/lezi-wiki/lezi-api/services/text"
"math/rand"
)

Expand All @@ -18,7 +18,7 @@ func GlobalHandler(c *gin.Context) {
var arr = text.Data

if ns != "" {
arr, err = services.GetTextByNamespace(ns)
arr, err = textService.GetTextByNamespace(ns)
if err != nil {
response.NotFoundError(c)
return
Expand Down
6 changes: 3 additions & 3 deletions controller/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package controller
import (
"github.com/gin-gonic/gin"
"github.com/lezi-wiki/lezi-api/pkg/response"
"github.com/lezi-wiki/lezi-api/services"
"github.com/lezi-wiki/lezi-api/services/text"
"math/rand"
"time"
)

func NamespaceJsonHandler(c *gin.Context) {
ns := c.Param("namespace")

arr, err := services.GetTextByNamespace(ns)
arr, err := text.GetTextByNamespace(ns)
if err != nil {
response.NotFoundError(c)
return
Expand All @@ -27,7 +27,7 @@ func NamespaceJsonHandler(c *gin.Context) {
func NamespaceTextHandler(c *gin.Context) {
ns := c.Param("namespace")

arr, err := services.GetTextByNamespace(ns)
arr, err := text.GetTextByNamespace(ns)
if err != nil {
response.NotFoundError(c)
return
Expand Down
6 changes: 3 additions & 3 deletions controller/speaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package controller
import (
"github.com/gin-gonic/gin"
"github.com/lezi-wiki/lezi-api/pkg/response"
"github.com/lezi-wiki/lezi-api/services"
"github.com/lezi-wiki/lezi-api/services/text"
"math/rand"
"time"
)

func SpeakerJsonHandler(c *gin.Context) {
speaker := c.Param("speaker")

arr, err := services.GetTextBySpeaker(speaker)
arr, err := text.GetTextBySpeaker(speaker)
if err != nil {
response.NotFoundError(c)
return
Expand All @@ -27,7 +27,7 @@ func SpeakerJsonHandler(c *gin.Context) {
func SpeakerTextHandler(c *gin.Context) {
speaker := c.Param("speaker")

arr, err := services.GetTextBySpeaker(speaker)
arr, err := text.GetTextBySpeaker(speaker)
if err != nil {
response.NotFoundError(c)
return
Expand Down
18 changes: 15 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,38 @@ go 1.18
require github.com/gin-gonic/gin v1.8.1

require (
github.com/basgys/goxml2json v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/gin-contrib/cors v1.4.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-ini/ini v1.66.6 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.11.0 // indirect
github.com/goccy/go-json v0.9.8 // indirect
github.com/goccy/go-json v0.9.10 // indirect
github.com/idoubi/goutils v1.0.5 // indirect
github.com/idoubi/goz v1.2.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/robfig/cron v1.2.0 // indirect
github.com/stretchr/testify v1.8.0 // indirect
github.com/tidwall/gjson v1.14.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
github.com/yoda-of-soda/map2xml v1.0.2 // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/net v0.0.0-20220706163947-c90051bbdb60 // indirect
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e // indirect
golang.org/x/net v0.0.0-20220708220712-1185a9018129 // indirect
golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
35 changes: 35 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
github.com/basgys/goxml2json v1.1.0 h1:4ln5i4rseYfXNd86lGEB+Vi652IsIXIvggKM/BhUKVw=
github.com/basgys/goxml2json v1.1.0/go.mod h1:wH7a5Np/Q4QoECFIU8zTQlZwZkrilY0itPfecMw41Dw=
github.com/bitly/go-simplejson v0.5.0/go.mod h1:cXHtHw4XUPsvGaxgjIAn8PhEWG9NfngEKAMDJEczWVA=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down Expand Up @@ -26,10 +30,17 @@ github.com/goccy/go-json v0.9.7 h1:IcB+Aqpx/iMHu5Yooh7jEzJk1JZ7Pjtmys2ukPr7EeM=
github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/goccy/go-json v0.9.8 h1:DxXB6MLd6yyel7CLph8EwNIonUtVZd3Ue5iRcL4DQCE=
github.com/goccy/go-json v0.9.8/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/goccy/go-json v0.9.10 h1:hCeNmprSNLB8B8vQKWl6DpuH0t60oEs+TAk9a7CScKc=
github.com/goccy/go-json v0.9.10/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/idoubi/goutils v1.0.4/go.mod h1:h89DYbCHEpDqEPFz76+jJsG9f9xnKSBrg+QDqwW6jUc=
github.com/idoubi/goutils v1.0.5 h1:s+rWYDwFX0D3GDABXD0y30sSnBA7dcczoU6b8ZEnq5w=
github.com/idoubi/goutils v1.0.5/go.mod h1:h89DYbCHEpDqEPFz76+jJsG9f9xnKSBrg+QDqwW6jUc=
github.com/idoubi/goz v1.2.2 h1:6o/EJ572VZ6iYrqUodPDTXxrCrHqAJw8ouzCU4N9veo=
github.com/idoubi/goz v1.2.2/go.mod h1:w/6bwRG1ZTg6xPiQz1dzoLKj7sLbCTdZCrRUzbddy6E=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand Down Expand Up @@ -61,6 +72,8 @@ github.com/pelletier/go-toml/v2 v2.0.2/go.mod h1:MovirKjgVRESsAvNZlAjtFwV867yGuw
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
Expand All @@ -72,21 +85,37 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/tidwall/gjson v1.6.1/go.mod h1:BaHyNc5bjzYkPqgLq7mdVzeiRtULKULXLgZFKsxEHI0=
github.com/tidwall/gjson v1.14.1 h1:iymTbGkQBhveq21bEvAQ81I0LEBork8BFe1CUZXdyuo=
github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.0.2/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/ugorji/go v1.2.7 h1:qYhyWUUd6WbiM+C6JZAUkIJt/1WrjzNHY9+KCIjVqTo=
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
github.com/yoda-of-soda/map2xml v1.0.2 h1:z3Io7yyf2UFovGcy1b4ct50ci9KbLk8YmyjTpMRgXZ4=
github.com/yoda-of-soda/map2xml v1.0.2/go.mod h1:kEZVcMDvg9pYiosS3G3xWl/C2LspDkrYY294J50dqjc=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220517181318-183a9ca12b87/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220706163947-c90051bbdb60 h1:8NSylCMxLW4JvserAndSgFL7aPli6A68yf0bYFTcWCM=
golang.org/x/net v0.0.0-20220706163947-c90051bbdb60/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.0.0-20220708220712-1185a9018129 h1:vucSRfWwTsoXro7P+3Cjlr6flUMtzCwzlvkxEQtHHB0=
golang.org/x/net v0.0.0-20220708220712-1185a9018129/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -96,10 +125,15 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 h1:siQdpVirKtzPhKl3lZWozZraCFObP8S1v6PRp0bLrtU=
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e h1:CsOuNlbOuf0mzxJIefr6Q4uAUetRUwZE4qt7VfzP+xo=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e h1:NHvCuwuS43lGnYhten69ZWqi2QOj/CiDNcKbVqwVoew=
golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
Expand All @@ -120,4 +154,5 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
17 changes: 13 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"github.com/lezi-wiki/lezi-api/bootstrap"
"github.com/lezi-wiki/lezi-api/pkg/conf"
"github.com/lezi-wiki/lezi-api/pkg/cron"
"github.com/lezi-wiki/lezi-api/pkg/util"
"github.com/lezi-wiki/lezi-api/routers"
)
Expand All @@ -13,18 +14,20 @@ import (
var data string

var (
isEject bool
confPath string
dataPath string
isEject bool
confPath string
dataPath string
updateEndpoint string
)

func init() {
flag.StringVar(&confPath, "c", util.RelativePath("conf.ini"), "配置文件路径")
flag.StringVar(&dataPath, "d", util.RelativePath("data.json"), "数据文件路径")
flag.StringVar(&updateEndpoint, "update", "https://raw.githubusercontent.com/lezi-wiki/lezi-api/master/data.json", "数据更新地址")
flag.BoolVar(&isEject, "eject", false, "导出内置静态资源")
flag.Parse()

bootstrap.Init(dataPath, data, confPath)
bootstrap.Init(dataPath, data, confPath, updateEndpoint)
}

func main() {
Expand All @@ -33,6 +36,12 @@ func main() {
return
}

if updateEndpoint != "false" {
cron.InitJobs()
} else {
util.Log().Warning("自动更新服务未启动,数据将无法从 GitHub 自动获取!")
}

r := routers.InitRouter()

util.Log().Info("应用将监听 %s", conf.SystemConfig.Listen)
Expand Down
21 changes: 21 additions & 0 deletions pkg/cron/data_jobs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cron

import (
"github.com/lezi-wiki/lezi-api/pkg/text"
"github.com/lezi-wiki/lezi-api/pkg/util"
"github.com/lezi-wiki/lezi-api/services/remote"
)

func getNewData() {
util.Log().Info("准备从 GitHub 更新数据集")
data, err := remote.GetDataFromGitHub()
if err != nil {
util.Log().Error("更新数据集失败")
return
}

text.Data = data
util.Log().Debug("数据集已更新:%v", text.Data)

util.Log().Info("数据集更新完成")
}
25 changes: 25 additions & 0 deletions pkg/cron/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cron

import (
"github.com/lezi-wiki/lezi-api/pkg/util"
"github.com/robfig/cron"
)

var task *cron.Cron

func init() {
task = cron.New()
task.Start()
}

func InitJobs() {
// 每30分钟获取远端数据
err := task.AddFunc("0 */30 * * *", getNewData)
if err != nil {
util.Log().Error("Cron jobs 错误, %s", err.Error())
util.Log().Warning("自动更新服务未启动,数据将无法从 GitHub 自动获取!")
return
} else {
util.Log().Info("自动更新服务已启动,数据将从 GitHub 自动获取!")
}
}
9 changes: 9 additions & 0 deletions pkg/http/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package http

import "github.com/idoubi/goz"

var client *goz.Request

func init() {
client = goz.NewClient()
}
52 changes: 52 additions & 0 deletions pkg/http/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package http

import (
"errors"
"github.com/idoubi/goz"
)

const (
ErrHttp = "http error"
)

func Get(url string, opt ...goz.Options) ([]byte, error) {
req, err := client.Get(url, opt...)
if err != nil {
return nil, err
}

code := req.GetStatusCode()
if code != 200 {
return nil, errors.New(ErrHttp)
}

data, err := req.GetBody()
if err != nil {
return nil, err
}

return data, nil
}

func Post(url string, data interface{}, opts ...goz.Options) ([]byte, error) {
newOpt := []goz.Options{
{
JSON: data,
},
}
for _, option := range opts {
newOpt = append(newOpt, option)
}

req, err := client.Post(url, newOpt...)
if err != nil {
return nil, err
}

resp, err := req.GetBody()
if err != nil {
return nil, err
}

return resp, nil
}
Loading

0 comments on commit a612eef

Please sign in to comment.