feat: 加入中间件和NoRoute设定

todo: 校验签名的中间件逻辑
This commit is contained in:
pengyinjie 2025-10-31 17:23:44 +08:00
parent fc1b759aaa
commit 70d3731302
9 changed files with 111 additions and 5 deletions

View File

@ -22,6 +22,6 @@ func QueryMethod(ctx context.Context, c *app.RequestContext) {
} }
resp := new(weather_and_earthquake.QueryResp) resp := new(weather_and_earthquake.QueryResp)
resp.Code = req.Op
c.JSON(consts.StatusOK, resp) c.JSON(consts.StatusOK, resp)
} }

View File

@ -17,5 +17,5 @@ import (
func Register(r *server.Hertz) { func Register(r *server.Hertz) {
root := r.Group("/", rootMw()...) root := r.Group("/", rootMw()...)
root.GET("/", append(_querymethodMw(), weather_and_earthquake.QueryMethod)...) root.GET("/query", append(_querymethodMw(), weather_and_earthquake.QueryMethod)...)
} }

View File

@ -4,6 +4,7 @@ package weather_and_earthquake
import ( import (
"github.com/cloudwego/hertz/pkg/app" "github.com/cloudwego/hertz/pkg/app"
"weather_and_earthquake/internal/middleware"
) )
func rootMw() []app.HandlerFunc { func rootMw() []app.HandlerFunc {
@ -12,6 +13,14 @@ func rootMw() []app.HandlerFunc {
} }
func _querymethodMw() []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... // your code...
return nil return nil
} }

7
configs/apps.yaml Normal file
View File

@ -0,0 +1,7 @@
app_secrets:
- id: abcdefghijklmn
secret: secret1
- id: cdefgghijklmnl
secret: secret2
- id: gdfagfdgfggggg
secret: secret3

18
configs/config.yaml Normal file
View File

@ -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

View File

@ -30,5 +30,5 @@ struct QueryResp {
} }
service WeatherAndEarthquakeService { service WeatherAndEarthquakeService {
QueryResp QueryMethod(1: QueryReq request) (api.get="/"); QueryResp QueryMethod(1: QueryReq request) (api.get="/query");
} }

43
internal/config/config.go Normal file
View File

@ -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"`
}

View File

@ -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)
}
}

View File

@ -3,13 +3,20 @@
package main package main
import ( import (
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server" "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. // customizeRegister registers customize routers.
func customizedRegister(r *server.Hertz) { func customizedRegister(r *server.Hertz) {
r.GET("/ping", handler.Ping) //r.GET("/ping", handler.Ping)
// your code ... // 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...)
} }