feat: 天气查询逻辑完成
This commit is contained in:
parent
754c4899ad
commit
98e9410548
|
|
@ -4,6 +4,9 @@ package weather_and_earthquake
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"weather_and_earthquake/internal/biz"
|
||||
"weather_and_earthquake/internal/data"
|
||||
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"github.com/cloudwego/hertz/pkg/protocol/consts"
|
||||
|
|
@ -25,6 +28,144 @@ func QueryMethod(ctx context.Context, c *app.RequestContext) {
|
|||
|
||||
logs.Info("QueryMethod req: %v", req)
|
||||
resp := new(weather_and_earthquake.QueryResp)
|
||||
resp.Code = req.Op
|
||||
switch req.Op {
|
||||
case 0:
|
||||
// 获取地震信息
|
||||
resp.Code = 0
|
||||
resp.EarthquakeInformations = biz.GetEarthquakeInfo()
|
||||
case 1:
|
||||
// 通过ip地址查天气,不需要其他参数
|
||||
ip := c.ClientIP()
|
||||
GetWeatherConditionByIPAddr(ctx, resp, ip)
|
||||
case 2:
|
||||
// 通过 location key 查询天气,需要提供 location key
|
||||
if len(req.LocationKey) == 0 {
|
||||
resp.Code = -1
|
||||
} else {
|
||||
resp.LocationKey = req.LocationKey
|
||||
GetWeatherConditionByLocationKey(ctx, resp, req.LocationKey)
|
||||
}
|
||||
case 3:
|
||||
// 通过地名查询天气
|
||||
if len(req.Addr) == 0 {
|
||||
resp.Code = -1
|
||||
} else {
|
||||
GetWeatherConditionByAddress(ctx, resp, req.Addr)
|
||||
}
|
||||
default:
|
||||
resp.Code = -1
|
||||
}
|
||||
|
||||
c.JSON(consts.StatusOK, resp)
|
||||
}
|
||||
|
||||
func GetWeatherConditionByLocationKey(ctx context.Context, resp *weather_and_earthquake.QueryResp, key string) {
|
||||
resp.Code = 0
|
||||
condition, err := data.GetWeatherCondition(ctx, key)
|
||||
if err == nil && len(condition) > 0 {
|
||||
resp.CurrentConditions = condition
|
||||
} else {
|
||||
condition, err = biz.GetCurrentConditions(key)
|
||||
if err != nil {
|
||||
resp.Code = 1
|
||||
resp.CurrentConditions = ""
|
||||
logs.Errorf("GetWeatherConditionByLocationKey err: %v\n", err)
|
||||
} else {
|
||||
resp.CurrentConditions = condition
|
||||
err = data.SetWeatherCondition(ctx, key, condition)
|
||||
if err != nil {
|
||||
logs.Errorf("Set Weather Condition err: %v\n", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
forecasts, err := data.GetDailyForecasts(ctx, key)
|
||||
if err == nil && len(forecasts) > 0 {
|
||||
resp.DailyForecasts = forecasts
|
||||
} else {
|
||||
forecasts, err = biz.GetDailyForecasts(key)
|
||||
if err != nil {
|
||||
resp.Code = 1
|
||||
resp.DailyForecasts = ""
|
||||
logs.Errorf("Get daily forecasts failed: %v\n", err)
|
||||
} else {
|
||||
resp.DailyForecasts = forecasts
|
||||
err = data.SetDailyForecasts(ctx, key, forecasts)
|
||||
if err != nil {
|
||||
logs.Errorf("Set daily forecasts failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func GetWeatherConditionByIPAddr(ctx context.Context, resp *weather_and_earthquake.QueryResp, ip string) {
|
||||
key, err := biz.GetLocationByIPAddr(ip)
|
||||
if err != nil {
|
||||
resp.Code = 2
|
||||
logs.Errorf("Query location for ip[%s] failed: %v\n", ip, err)
|
||||
return
|
||||
}
|
||||
|
||||
resp.Code = 0
|
||||
resp.LocationKey = key
|
||||
condition, err := data.GetWeatherCondition(ctx, key)
|
||||
if err == nil && len(condition) > 0 {
|
||||
resp.CurrentConditions = condition
|
||||
} else {
|
||||
condition, err = biz.GetCurrentConditions(key)
|
||||
if err != nil {
|
||||
resp.Code = 1
|
||||
resp.CurrentConditions = ""
|
||||
logs.Errorf("Get current conditions failed: %v\n", err)
|
||||
} else {
|
||||
resp.CurrentConditions = condition
|
||||
err = data.SetWeatherCondition(ctx, key, condition)
|
||||
if err != nil {
|
||||
logs.Errorf("Set weather condition failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
forecasts, err := data.GetDailyForecasts(ctx, key)
|
||||
if err == nil && len(forecasts) > 0 {
|
||||
resp.DailyForecasts = forecasts
|
||||
} else {
|
||||
forecasts, err = biz.GetDailyForecasts(key)
|
||||
if err != nil {
|
||||
resp.Code = 1
|
||||
resp.DailyForecasts = ""
|
||||
logs.Errorf("Get daily forecasts failed: %v\n", err)
|
||||
} else {
|
||||
resp.DailyForecasts = forecasts
|
||||
err = data.SetDailyForecasts(ctx, key, forecasts)
|
||||
if err != nil {
|
||||
logs.Errorf("Set daily forecasts failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func GetWeatherConditionByAddress(ctx context.Context, resp *weather_and_earthquake.QueryResp, address string) {
|
||||
content, err := biz.GetLocationsByName(address)
|
||||
if err != nil || len(content) == 0 {
|
||||
resp.Code = 2
|
||||
return
|
||||
}
|
||||
|
||||
cities := make([]biz.CityInfo, 0)
|
||||
err = json.Unmarshal([]byte(content), &cities)
|
||||
if err != nil {
|
||||
resp.Code = 2
|
||||
return
|
||||
}
|
||||
|
||||
if len(cities) == 1 {
|
||||
// 只找到一个对应的地点,就返回这个地点的结果
|
||||
city := cities[0]
|
||||
resp.LocationKey = city.Key
|
||||
GetWeatherConditionByLocationKey(ctx, resp, city.Key)
|
||||
} else {
|
||||
resp.Code = 3
|
||||
resp.Locations = content
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
app_secrets:
|
||||
- id: abcdefghijklmn
|
||||
- id: weatherQuery
|
||||
secret: secret1
|
||||
- id: cdefgghijklmnl
|
||||
- id: weatherQuery2
|
||||
secret: secret2
|
||||
- id: gdfagfdgfggggg
|
||||
- id: weatherQuery3
|
||||
secret: secret3
|
||||
|
|
@ -12,7 +12,7 @@ data:
|
|||
db: 0
|
||||
pool_size: 16
|
||||
min_idle_conns: 4
|
||||
expire: 3600
|
||||
expire: 86400s
|
||||
|
||||
accu_weather:
|
||||
api_key: zpka_0e750f6b405446f389a3a7d954c780b6_63fd410f
|
||||
|
|
|
|||
4
go.mod
4
go.mod
|
|
@ -7,6 +7,8 @@ replace github.com/apache/thrift => github.com/apache/thrift v0.13.0
|
|||
require (
|
||||
github.com/apache/thrift v0.0.0-00010101000000-000000000000
|
||||
github.com/cloudwego/hertz v0.10.3
|
||||
github.com/go-redis/redis/v8 v8.11.5
|
||||
github.com/hertz-contrib/keyauth v0.0.0-20221011055545-d02ab20cb1ca
|
||||
github.com/zxysilent/logs v0.8.4
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
|
@ -21,10 +23,10 @@ require (
|
|||
github.com/cloudwego/netpoll v0.7.0 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/fsnotify/fsnotify v1.5.4 // indirect
|
||||
github.com/go-redis/redis/v8 v8.11.5 // indirect
|
||||
github.com/golang/protobuf v1.5.0 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
|
||||
github.com/nyaruka/phonenumbers v1.0.55 // indirect
|
||||
github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d // indirect
|
||||
github.com/tidwall/gjson v1.14.4 // indirect
|
||||
github.com/tidwall/match v1.1.1 // indirect
|
||||
github.com/tidwall/pretty v1.2.0 // indirect
|
||||
|
|
|
|||
44
go.sum
44
go.sum
|
|
@ -1,7 +1,10 @@
|
|||
github.com/apache/thrift v0.13.0 h1:5hryIiq9gtn+MiLVn0wP37kb/uTeRZgN08WoCsAhIhI=
|
||||
github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
|
||||
github.com/bytedance/go-tagexpr/v2 v2.9.2/go.mod h1:5qsx05dYOiUXOUgnQ7w3Oz8BYs2qtM/bJokdLb79wRM=
|
||||
github.com/bytedance/gopkg v0.0.0-20220413063733-65bf48ffb3a7/go.mod h1:2ZlV9BaUH4+NXIBF0aMdKKAnHTzqH+iMU4KUjAbL23Q=
|
||||
github.com/bytedance/gopkg v0.1.1 h1:3azzgSkiaw79u24a+w9arfH8OfnQQ4MHUt9lJFREEaE=
|
||||
github.com/bytedance/gopkg v0.1.1/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM=
|
||||
github.com/bytedance/sonic v1.3.5/go.mod h1:V973WhNhGmvHxW6nQmsHEfHaoU9F3zTF+93rH03hcUQ=
|
||||
github.com/bytedance/sonic v1.14.0 h1:/OfKt8HFw0kh2rj8N0F6C/qPGRESq0BbaNZgcNXXzQQ=
|
||||
github.com/bytedance/sonic v1.14.0/go.mod h1:WoEbx8WTcFJfzCe0hbmyTGrfjt8PzNEBdxlNUO24NhA=
|
||||
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||
|
|
@ -9,13 +12,16 @@ github.com/bytedance/sonic/loader v0.3.0 h1:dskwH8edlzNMctoruo8FPTJDF3vLtDT0sXZw
|
|||
github.com/bytedance/sonic/loader v0.3.0/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
|
||||
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
|
||||
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
|
||||
github.com/cloudwego/base64x v0.1.5 h1:XPciSp1xaq2VCSt6lF0phncD4koWyULpl5bUxbfCyP4=
|
||||
github.com/cloudwego/base64x v0.1.5/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
||||
github.com/cloudwego/gopkg v0.1.4 h1:EoQiCG4sTonTPHxOGE0VlQs+sQR+Hsi2uN0qqwu8O50=
|
||||
github.com/cloudwego/gopkg v0.1.4/go.mod h1:FQuXsRWRsSqJLsMVd5SYzp8/Z1y5gXKnVvRrWUOsCMI=
|
||||
github.com/cloudwego/hertz v0.3.1/go.mod h1:hnv3B7eZ6kMv7CKFHT2OC4LU0mA4s5XPyu/SbixLcrU=
|
||||
github.com/cloudwego/hertz v0.10.3 h1:NFcQAjouVJsod79XPLC/PaFfHgjMTYbiErmW+vGBi8A=
|
||||
github.com/cloudwego/hertz v0.10.3/go.mod h1:W5dUFXZPZkyfjMMo3EQrMQbofuvTsctM9IxmhbkuT18=
|
||||
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
|
||||
github.com/cloudwego/netpoll v0.2.6/go.mod h1:1T2WVuQ+MQw6h6DpE45MohSvDTKdy2DlzCx2KsnPI4E=
|
||||
github.com/cloudwego/netpoll v0.7.0 h1:bDrxQaNfijRI1zyGgXHQoE/nYegL0nr+ijO1Norelc4=
|
||||
github.com/cloudwego/netpoll v0.7.0/go.mod h1:PI+YrmyS7cIr0+SD4seJz3Eo3ckkXdu2ZVKBLhURLNU=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
|
|
@ -28,11 +34,20 @@ github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwV
|
|||
github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU=
|
||||
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
|
||||
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
|
||||
github.com/goccy/go-json v0.9.4/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4=
|
||||
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/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/henrylee2cn/ameda v1.4.8/go.mod h1:liZulR8DgHxdK+MEwvZIylGnmcjzQ6N6f2PlWe7nEO4=
|
||||
github.com/henrylee2cn/ameda v1.4.10/go.mod h1:liZulR8DgHxdK+MEwvZIylGnmcjzQ6N6f2PlWe7nEO4=
|
||||
github.com/henrylee2cn/goutil v0.0.0-20210127050712-89660552f6f8/go.mod h1:Nhe/DM3671a5udlv2AdV2ni/MZzgfv2qrPL5nIi3EGQ=
|
||||
github.com/hertz-contrib/keyauth v0.0.0-20221011055545-d02ab20cb1ca h1:phnBlulxXwFhYvDF9GEj/rsBlWY7gppXM2ts04eqQqg=
|
||||
github.com/hertz-contrib/keyauth v0.0.0-20221011055545-d02ab20cb1ca/go.mod h1:5tkyAnVuT3WGZQQZAXjis2BDGdnV9Y1ClfGsqlecfe4=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
|
||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
||||
|
|
@ -41,15 +56,28 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
|
|||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
|
||||
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
|
||||
github.com/nyaruka/phonenumbers v1.0.55 h1:bj0nTO88Y68KeUQ/n3Lo2KgK7lM1hF7L9NFuwcCl3yg=
|
||||
github.com/nyaruka/phonenumbers v1.0.55/go.mod h1:sDaTZ/KPX5f8qyV9qN+hIm+4ZBARJrupC6LuhshJq1U=
|
||||
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
|
||||
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
|
||||
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
|
||||
github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs=
|
||||
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/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d h1:Q+gqLBOPkFGHyCJxXMRqtUgUbTjI8/Ze8vu8GGyNFwo=
|
||||
github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d/go.mod h1:Gy+0tqhJvgGlqnTF8CVGP0AaGRjwBtXs/a5PA0Y3+A4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
|
|
@ -57,14 +85,19 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
|
|||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/tidwall/gjson v1.9.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/gjson v1.12.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/gjson v1.13.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
|
||||
github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
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.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
|
||||
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||
github.com/tidwall/sjson v1.2.4/go.mod h1:098SZ494YoMWPmMO6ct4dcFnqxwj9r/gF0Etp19pSNM=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
github.com/zxysilent/logs v0.8.4 h1:z/TFm99ZxSgnrWeKPFxllnPyU7y06mWR0qhrZQ4zJNI=
|
||||
github.com/zxysilent/logs v0.8.4/go.mod h1:cmA0baYm8n2z+8GJwNUvnu7v8GlVNHZNOktZfChaJmI=
|
||||
|
|
@ -82,14 +115,17 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
|
|||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
|
||||
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
|
||||
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
|
||||
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220110181412-a018aaa089fe/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
|
|
@ -110,6 +146,7 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
|||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
|
|
@ -119,12 +156,19 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
|
|||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
|
||||
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
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.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
||||
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
|
||||
|
|
|
|||
|
|
@ -1 +1,7 @@
|
|||
package biz
|
||||
|
||||
var _earthquakeInfo string
|
||||
|
||||
func GetEarthquakeInfo() string {
|
||||
return _earthquakeInfo
|
||||
}
|
||||
|
|
|
|||
|
|
@ -290,7 +290,6 @@ func GetLocationByIPAddr(ip string) (string, error) {
|
|||
return "", err
|
||||
}
|
||||
httpReq.Header.Set("Authorization", "Bearer "+AccuWeather.APIKey)
|
||||
defer httpReq.Body.Close()
|
||||
|
||||
httpResp, err := http.DefaultClient.Do(httpReq)
|
||||
if err != nil {
|
||||
|
|
@ -323,43 +322,36 @@ func GetLocationByIPAddr(ip string) (string, error) {
|
|||
}
|
||||
|
||||
// 通过地名搜索城市。有可能会搜到多个地点。
|
||||
func GetLocationsByName(name string) ([]CityInfo, error) {
|
||||
func GetLocationsByName(name string) (string, error) {
|
||||
url := fmt.Sprintf(AccuWeather.CitiesSearchURL, url2.QueryEscape(name))
|
||||
|
||||
httpReq, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return "", err
|
||||
}
|
||||
httpReq.Header.Set("Authorization", "Bearer "+AccuWeather.APIKey)
|
||||
defer httpReq.Body.Close()
|
||||
|
||||
httpResp, err := http.DefaultClient.Do(httpReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return "", err
|
||||
}
|
||||
defer httpResp.Body.Close()
|
||||
|
||||
respBody, err := io.ReadAll(httpResp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return "", err
|
||||
}
|
||||
|
||||
if httpResp.StatusCode != http.StatusOK {
|
||||
var errResp ErrorResponse
|
||||
err = json.Unmarshal(respBody, &errResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return "", err
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf(errResp.Title)
|
||||
return "", fmt.Errorf(errResp.Title)
|
||||
} else {
|
||||
cities := make([]CityInfo, 0)
|
||||
err = json.Unmarshal(respBody, &cities)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return cities, nil
|
||||
return string(respBody), nil
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -372,7 +364,6 @@ func GetCurrentConditions(key string) (string, error) {
|
|||
return "", err
|
||||
}
|
||||
httpReq.Header.Set("Authorization", "Bearer "+AccuWeather.APIKey)
|
||||
defer httpReq.Body.Close()
|
||||
|
||||
httpResp, err := http.DefaultClient.Do(httpReq)
|
||||
if err != nil {
|
||||
|
|
@ -407,7 +398,6 @@ func GetDailyForecasts(key string) (string, error) {
|
|||
return "", err
|
||||
}
|
||||
httpReq.Header.Set("Authorization", "Bearer "+AccuWeather.APIKey)
|
||||
defer httpReq.Body.Close()
|
||||
|
||||
httpResp, err := http.DefaultClient.Do(httpReq)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package data
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"time"
|
||||
"weather_and_earthquake/internal/config"
|
||||
|
|
@ -27,15 +28,37 @@ func InitWeatherCache(ctx context.Context, data *config.Redis) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// 设置某个地方的天气情况,默认保留1小时数据
|
||||
// 设置某个地方的天气情况
|
||||
func SetWeatherCondition(ctx context.Context, key, condition string) error {
|
||||
_, err := _redis.SetNX(ctx, key, condition, _expire*time.Second).Result()
|
||||
redis_key := fmt.Sprintf("%s-condition", key)
|
||||
_, err := _redis.SetNX(ctx, redis_key, condition, _expire*time.Second).Result()
|
||||
return err
|
||||
}
|
||||
|
||||
// 查找某个地方的天气情况
|
||||
func GetWeatherCondition(ctx context.Context, key string) (string, error) {
|
||||
value, err := _redis.Get(ctx, key).Result()
|
||||
redis_key := fmt.Sprintf("%s-condition", key)
|
||||
value, err := _redis.Get(ctx, redis_key).Result()
|
||||
if err == nil {
|
||||
return value, nil
|
||||
} else if err == redis.Nil {
|
||||
return "", nil
|
||||
} else {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
// 设置某个地方的天气预报
|
||||
func SetDailyForecasts(ctx context.Context, key, forecasts string) error {
|
||||
redis_key := fmt.Sprintf("%s-forecasts", key)
|
||||
_, err := _redis.SetNX(ctx, redis_key, forecasts, _expire*time.Second).Result()
|
||||
return err
|
||||
}
|
||||
|
||||
// 查找某个地方的天气预报
|
||||
func GetDailyForecasts(ctx context.Context, key string) (string, error) {
|
||||
redis_key := fmt.Sprintf("%s-forecasts", key)
|
||||
value, err := _redis.Get(ctx, redis_key).Result()
|
||||
if err == nil {
|
||||
return value, nil
|
||||
} else if err == redis.Nil {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,11 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"github.com/cloudwego/hertz/pkg/protocol/consts"
|
||||
"weather_and_earthquake/internal/config"
|
||||
)
|
||||
import "context"
|
||||
|
|
@ -22,6 +26,37 @@ func CorsMiddleware() app.HandlerFunc {
|
|||
// 校验签名
|
||||
func AuthMiddleware() app.HandlerFunc {
|
||||
return func(ctx context.Context, c *app.RequestContext) {
|
||||
author := c.Request.Header.Get("Authorization")
|
||||
timestamp := c.Request.Header.Get("Timestamp")
|
||||
sign := c.Request.Header.Get("Sign")
|
||||
if author == "" || timestamp == "" || sign == "" {
|
||||
c.AbortWithMsg("Missing Parameter.", consts.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
var secret string = ""
|
||||
for _, v := range Apps.AppSecrets {
|
||||
if v.Id == author {
|
||||
secret = v.Secret
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if len(secret) == 0 {
|
||||
c.AbortWithMsg("Wrong Authorization.", consts.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
temp := fmt.Sprintf("%s-%s-%s", author, timestamp, secret)
|
||||
|
||||
h := md5.New()
|
||||
h.Write([]byte(temp))
|
||||
localSign := hex.EncodeToString(h.Sum(nil))
|
||||
if localSign != sign {
|
||||
c.AbortWithMsg("Wrong Signature.", consts.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
c.Next(ctx)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
8
main.go
8
main.go
|
|
@ -78,10 +78,10 @@ func initialize() {
|
|||
logs.SetLevel(logs.LDEBUG)
|
||||
}
|
||||
logs.SetFile("./logs/logs.log")
|
||||
logs.Warn("Logs warning")
|
||||
logs.Error("Logs error")
|
||||
logs.Info("Logs info")
|
||||
logs.Debug("Logs debug")
|
||||
//logs.Warn("Logs warning")
|
||||
//logs.Error("Logs error")
|
||||
//logs.Info("Logs info")
|
||||
//logs.Debug("Logs debug")
|
||||
|
||||
hostPort := fmt.Sprintf("%s:%d", conf.Server.Addr, conf.Server.Port)
|
||||
_server = server.Default(server.WithHostPorts(hostPort))
|
||||
|
|
|
|||
|
|
@ -14,6 +14,12 @@ func customizedRegister(r *server.Hertz) {
|
|||
//r.GET("/ping", handler.Ping)
|
||||
|
||||
// your code ...
|
||||
//r.Use(keyauth.New(
|
||||
// keyauth.WithFilter(func(ctx context.Context, c *app.RequestContext) bool {
|
||||
// return true
|
||||
// }),
|
||||
//))
|
||||
|
||||
routes := make([]app.HandlerFunc, 0)
|
||||
routes = append(routes, middleware.AuthMiddleware())
|
||||
routes = append(routes, middleware.CorsMiddleware())
|
||||
|
|
|
|||
Loading…
Reference in New Issue