diff --git a/go.mod b/go.mod index 0a9a87d..74865b3 100644 --- a/go.mod +++ b/go.mod @@ -24,6 +24,7 @@ require ( github.com/mattn/go-runewidth v0.0.15 // indirect github.com/olekukonko/tablewriter v0.0.5 // indirect github.com/rivo/uniseg v0.4.7 // indirect + github.com/rs/cors v1.11.1 go.opentelemetry.io/otel v1.24.0 // indirect go.opentelemetry.io/otel/metric v1.24.0 // indirect go.opentelemetry.io/otel/sdk v1.24.0 // indirect diff --git a/go.sum b/go.sum index 3640d20..c284a80 100644 --- a/go.sum +++ b/go.sum @@ -41,6 +41,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA= +github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo= go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo= diff --git a/main.go b/main.go index d2c9769..8444d45 100644 --- a/main.go +++ b/main.go @@ -1,16 +1,37 @@ package main import ( - _ "practice_Go/internal/logic" - _ "practice_Go/internal/packed" + _ "practice_Go/internal/logic" + _ "practice_Go/internal/packed" - _ "github.com/gogf/gf/contrib/drivers/mysql/v2" + "github.com/gogf/gf/v2/os/gctx" - "github.com/gogf/gf/v2/os/gctx" + _ "github.com/gogf/gf/contrib/drivers/mysql/v2" - "practice_Go/internal/cmd" + "practice_Go/internal/cmd" + + "fmt" + "log" + "net/http" + + "github.com/rs/cors" ) func main() { - cmd.Main.Run(gctx.GetInitCtx()) -} + cmd.Main.Run(gctx.GetInitCtx()) + + mux := http.NewServeMux() + mux.HandleFunc("/mainpage/get-shows", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, "Show data") + }) + + // 创建CORS中间件,配置允许的源等信息 + c := cors.New(cors.Options{ + AllowedOrigins: []string{"http://localhost:5173"}, + AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"}, + AllowedHeaders: []string{"Content-Type"}, + }) + handler := c.Handler(mux) + log.Fatal(http.ListenAndServe(":8080", handler)) + +} \ No newline at end of file