|
|
|
@ -3,20 +3,58 @@ package main |
|
|
|
|
import ( |
|
|
|
|
"github.com/go-chi/chi/v5" |
|
|
|
|
"github.com/go-chi/chi/v5/middleware" |
|
|
|
|
"github.com/go-chi/cors" |
|
|
|
|
"github.com/go-chi/httprate" |
|
|
|
|
"hupeh.vip/pricing/app" |
|
|
|
|
"log" |
|
|
|
|
"net/http" |
|
|
|
|
"time" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func main() { |
|
|
|
|
app.ConfigLogger("debug.log", app.LogWhenMinute) |
|
|
|
|
app.ConfigGormDB() |
|
|
|
|
|
|
|
|
|
r := chi.NewRouter() |
|
|
|
|
|
|
|
|
|
r.Use(middleware.Logger) |
|
|
|
|
r.Get("/goods", app.Handler(app.GetGoodsList)) |
|
|
|
|
r.Post("/goods", app.Handler(app.CreateGoods)) |
|
|
|
|
r.Get("/goods/:id/prices", app.Handler(app.GetGoodsPrices)) |
|
|
|
|
r.Post("/goods/:id", app.Handler(app.UpdateGoods)) |
|
|
|
|
r.Use(middleware.Recoverer) |
|
|
|
|
r.Use(middleware.NoCache) |
|
|
|
|
r.Use(middleware.Throttle(15)) |
|
|
|
|
r.Use(middleware.Heartbeat("/")) |
|
|
|
|
|
|
|
|
|
//r.Use(middleware.RouteHeaders().
|
|
|
|
|
// Route("Host", "example.com", middleware.New(r)).
|
|
|
|
|
// Route("Host", "*.example.com", middleware.New(rSubdomain)).
|
|
|
|
|
// Handler)
|
|
|
|
|
|
|
|
|
|
// Enable httprate request limiter of 100 requests per minute.
|
|
|
|
|
//
|
|
|
|
|
// In the code example below, rate-limiting is bound to the request IP address
|
|
|
|
|
// via the LimitByIP middleware handler.
|
|
|
|
|
//
|
|
|
|
|
// To have a single rate-limiter for all requests, use httprate.LimitAll(..).
|
|
|
|
|
//
|
|
|
|
|
// Please see _example/main.go for other more, or read the library code.
|
|
|
|
|
r.Use(httprate.LimitByIP(100, 1*time.Minute)) |
|
|
|
|
|
|
|
|
|
// mounting net/http/pprof
|
|
|
|
|
r.Mount("/debug", middleware.Profiler()) |
|
|
|
|
|
|
|
|
|
// 允许跨域
|
|
|
|
|
// see: https://developer.github.com/v3/#cross-origin-resource-sharing
|
|
|
|
|
r.Use(cors.Handler(cors.Options{ |
|
|
|
|
//AllowedOrigins: []string{"https://foo.com"}, // Use this to allow specific origin hosts
|
|
|
|
|
//AllowedOrigins: []string{"https://*", "http://*"},
|
|
|
|
|
AllowOriginFunc: func(r *http.Request, origin string) bool { return true }, |
|
|
|
|
AllowedMethods: []string{"GET", "HEAD", "PATCH", "POST", "PUT", "DELETE", "OPTIONS"}, |
|
|
|
|
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"}, |
|
|
|
|
ExposedHeaders: []string{"Link"}, |
|
|
|
|
AllowCredentials: false, |
|
|
|
|
MaxAge: 300, // Maximum value not ignored by any of major browsers
|
|
|
|
|
})) |
|
|
|
|
|
|
|
|
|
app.RegisterRoutes(r) |
|
|
|
|
|
|
|
|
|
if err := http.ListenAndServe(":3000", r); err != nil { |
|
|
|
|
log.Fatalln(err) |
|
|
|
|