parent
fc1b759aaa
commit
70d3731302
|
|
@ -22,6 +22,6 @@ func QueryMethod(ctx context.Context, c *app.RequestContext) {
|
|||
}
|
||||
|
||||
resp := new(weather_and_earthquake.QueryResp)
|
||||
|
||||
resp.Code = req.Op
|
||||
c.JSON(consts.StatusOK, resp)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,5 +17,5 @@ import (
|
|||
func Register(r *server.Hertz) {
|
||||
|
||||
root := r.Group("/", rootMw()...)
|
||||
root.GET("/", append(_querymethodMw(), weather_and_earthquake.QueryMethod)...)
|
||||
root.GET("/query", append(_querymethodMw(), weather_and_earthquake.QueryMethod)...)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ package weather_and_earthquake
|
|||
|
||||
import (
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"weather_and_earthquake/internal/middleware"
|
||||
)
|
||||
|
||||
func rootMw() []app.HandlerFunc {
|
||||
|
|
@ -12,6 +13,14 @@ func rootMw() []app.HandlerFunc {
|
|||
}
|
||||
|
||||
func _querymethodMw() []app.HandlerFunc {
|
||||
// your code...
|
||||
routes := make([]app.HandlerFunc, 0)
|
||||
routes = append(routes, middleware.AuthMiddleware())
|
||||
routes = append(routes, middleware.CorsMiddleware())
|
||||
return nil
|
||||
}
|
||||
|
||||
func __2aMw() []app.HandlerFunc {
|
||||
// your code...
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,7 @@
|
|||
app_secrets:
|
||||
- id: abcdefghijklmn
|
||||
secret: secret1
|
||||
- id: cdefgghijklmnl
|
||||
secret: secret2
|
||||
- id: gdfagfdgfggggg
|
||||
secret: secret3
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
server:
|
||||
Addr: 0.0.0.0
|
||||
port: 8080
|
||||
|
||||
data:
|
||||
redis:
|
||||
addr: 127.0.0.1:6379
|
||||
password: Abc123..
|
||||
read_timeout: 0.2s
|
||||
write_timeout: 0.2s
|
||||
db: 1
|
||||
|
||||
accu_weather:
|
||||
api_key: zpka_0e750f6b405446f389a3a7d954c780b6_63fd410f
|
||||
cities_search_by_ip_url: https://dataservice.accuweather.com/locations/v1/cities/ipaddress?q=%s
|
||||
cities_search_url: https://dataservice.accuweather.com/locations/v1/cities/search?q=%s
|
||||
current_condition_url: https://dataservice.accuweather.com/currentconditions/v1/%s?details=true
|
||||
daily_forecasts_url: https://dataservice.accuweather.com/forecasts/v1/daily/5day/%s?details=true
|
||||
|
|
@ -30,5 +30,5 @@ struct QueryResp {
|
|||
}
|
||||
|
||||
service WeatherAndEarthquakeService {
|
||||
QueryResp QueryMethod(1: QueryReq request) (api.get="/");
|
||||
QueryResp QueryMethod(1: QueryReq request) (api.get="/query");
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
package config
|
||||
|
||||
import "time"
|
||||
|
||||
type Server struct {
|
||||
Addr string `yaml:"addr"`
|
||||
Port uint32 `yaml:"port"`
|
||||
}
|
||||
|
||||
type Redis struct {
|
||||
Addr string `yaml:"addr"`
|
||||
Password string `yaml:"password"`
|
||||
ReadTimeout time.Duration `yaml:"read_timeout"`
|
||||
WriteTimeout time.Duration `yaml:"write_timeout"`
|
||||
DB int `yaml:"db"`
|
||||
}
|
||||
|
||||
type Data struct {
|
||||
Redis *Redis `yaml:"redis"`
|
||||
}
|
||||
|
||||
type AccuWeather struct {
|
||||
APIKey string `yaml:"api_key"`
|
||||
CitiesSearchByIPURL string `yaml:"cities_search_by_ip_url"`
|
||||
CitiesSearchURL string `yaml:"cities_search_url"`
|
||||
CurrentConditionURL string `yaml:"current_condition_url"`
|
||||
DailyForecastsURL string `yaml:"daily_forecasts_url"` // 查7填预报需要更高级的账号。默认先用查5天的。后续可以通过配置来改。
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Server *Server `yaml:"server"`
|
||||
Data *Data `yaml:"data"`
|
||||
AccuWeather *AccuWeather `yaml:"accu_weather"`
|
||||
}
|
||||
|
||||
type AppSecret struct {
|
||||
Id string `yaml:"id" json:"id"`
|
||||
Secret string `yaml:"secret" json:"secret"`
|
||||
}
|
||||
|
||||
type Apps struct {
|
||||
AppSecrets []AppSecret `yaml:"app_secrets" json:"app_secrets"`
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
package middleware
|
||||
|
||||
import "github.com/cloudwego/hertz/pkg/app"
|
||||
import "context"
|
||||
|
||||
// 跨域
|
||||
func CorsMiddleware() app.HandlerFunc {
|
||||
return func(ctx context.Context, c *app.RequestContext) {
|
||||
c.Response.Header.Set("Access-Control-Allow-Origin", "*")
|
||||
c.Response.Header.Set("Access-Control-Allow-Methods", "*")
|
||||
c.Response.Header.Set("Access-Control-Allow-Headers", "*")
|
||||
c.Response.Header.Set("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type")
|
||||
c.Next(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// 校验签名
|
||||
func AuthMiddleware() app.HandlerFunc {
|
||||
return func(ctx context.Context, c *app.RequestContext) {
|
||||
c.Next(ctx)
|
||||
}
|
||||
}
|
||||
11
router.go
11
router.go
|
|
@ -3,13 +3,20 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"github.com/cloudwego/hertz/pkg/app/server"
|
||||
handler "weather_and_earthquake/biz/handler"
|
||||
"weather_and_earthquake/biz/handler/weather_and_earthquake"
|
||||
"weather_and_earthquake/internal/middleware"
|
||||
)
|
||||
|
||||
// customizeRegister registers customize routers.
|
||||
func customizedRegister(r *server.Hertz) {
|
||||
r.GET("/ping", handler.Ping)
|
||||
//r.GET("/ping", handler.Ping)
|
||||
|
||||
// your code ...
|
||||
routes := make([]app.HandlerFunc, 0)
|
||||
routes = append(routes, middleware.AuthMiddleware())
|
||||
routes = append(routes, middleware.CorsMiddleware())
|
||||
routes = append(routes, weather_and_earthquake.QueryMethod)
|
||||
r.NoRoute(routes...)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue