diff --git a/biz/handler/weather_and_earthquake/weather_and_earthquake_service.go b/biz/handler/weather_and_earthquake/weather_and_earthquake_service.go index b9c2c57..963bb6e 100644 --- a/biz/handler/weather_and_earthquake/weather_and_earthquake_service.go +++ b/biz/handler/weather_and_earthquake/weather_and_earthquake_service.go @@ -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) } diff --git a/biz/router/weather_and_earthquake/WeatherAndEarthquake.go b/biz/router/weather_and_earthquake/WeatherAndEarthquake.go index 0d4b1ca..61e936c 100644 --- a/biz/router/weather_and_earthquake/WeatherAndEarthquake.go +++ b/biz/router/weather_and_earthquake/WeatherAndEarthquake.go @@ -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)...) } diff --git a/biz/router/weather_and_earthquake/middleware.go b/biz/router/weather_and_earthquake/middleware.go index d4cfc92..bac1cb6 100644 --- a/biz/router/weather_and_earthquake/middleware.go +++ b/biz/router/weather_and_earthquake/middleware.go @@ -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 } diff --git a/configs/apps.yaml b/configs/apps.yaml new file mode 100644 index 0000000..3f58aad --- /dev/null +++ b/configs/apps.yaml @@ -0,0 +1,7 @@ +app_secrets: + - id: abcdefghijklmn + secret: secret1 + - id: cdefgghijklmnl + secret: secret2 + - id: gdfagfdgfggggg + secret: secret3 \ No newline at end of file diff --git a/configs/config.yaml b/configs/config.yaml new file mode 100644 index 0000000..be2beab --- /dev/null +++ b/configs/config.yaml @@ -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 \ No newline at end of file diff --git a/idl/WeatherAndEarthquake.thrift b/idl/WeatherAndEarthquake.thrift index c62899b..8508a76 100644 --- a/idl/WeatherAndEarthquake.thrift +++ b/idl/WeatherAndEarthquake.thrift @@ -30,5 +30,5 @@ struct QueryResp { } service WeatherAndEarthquakeService { - QueryResp QueryMethod(1: QueryReq request) (api.get="/"); + QueryResp QueryMethod(1: QueryReq request) (api.get="/query"); } \ No newline at end of file diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..28e56a3 --- /dev/null +++ b/internal/config/config.go @@ -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"` +} diff --git a/internal/middleware/middleware.go b/internal/middleware/middleware.go new file mode 100644 index 0000000..3495c77 --- /dev/null +++ b/internal/middleware/middleware.go @@ -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) + } +} diff --git a/router.go b/router.go index d95463c..7d1feac 100644 --- a/router.go +++ b/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...) }