From 21354a50bbc58fcd5450ec03bd31af7de53cbd21 Mon Sep 17 00:00:00 2001 From: fifsky Date: Wed, 18 Mar 2020 22:17:00 +0800 Subject: [PATCH] add web --- api/main.go | 11 +++++++++-- go.mod | 1 + go.sum | 2 ++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/api/main.go b/api/main.go index c006c50..3cf7a2d 100644 --- a/api/main.go +++ b/api/main.go @@ -10,6 +10,7 @@ import ( _ "github.com/go-sql-driver/mysql" "github.com/goapt/dotenv" "github.com/ilibs/gosql/v2" + "github.com/rs/cors" "github.com/fifsky/genstruct/generator" ) @@ -38,7 +39,11 @@ func main() { db := gosql.Use("default") gen := generator.NewGenerator(db) - http.HandleFunc("/api/struct/gen", func(w http.ResponseWriter, r *http.Request) { + c := cors.New(cors.Options{ + AllowedOrigins: []string{"http://api.fifsky.com","https://api.fifsky.com"}, + }) + + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { body, err := ioutil.ReadAll(r.Body) if err != nil { w.Write([]byte(fmt.Sprintf("request body read error \n%s", err))) @@ -90,7 +95,9 @@ func main() { w.Write(st) }) - err = http.ListenAndServe(":8989", nil) + http.Handle("/api/struct/gen", c.Handler(handler)) + + err = http.ListenAndServe(":8989",nil) if err != nil { log.Fatal("ListenAndServe", err) diff --git a/go.mod b/go.mod index 0adcb27..2978bb5 100644 --- a/go.mod +++ b/go.mod @@ -8,4 +8,5 @@ require ( github.com/ilibs/gosql/v2 v2.0.2 github.com/mattn/go-runewidth v0.0.8 // indirect github.com/olekukonko/tablewriter v0.0.4 + github.com/rs/cors v1.7.0 // indirect ) diff --git a/go.sum b/go.sum index 1bffea0..ee49610 100644 --- a/go.sum +++ b/go.sum @@ -18,6 +18,8 @@ github.com/mattn/go-sqlite3 v1.9.0 h1:pDRiWfl+++eC2FEFRy6jXmQlvp4Yh3z1MJKg4UeYM/ github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/olekukonko/tablewriter v0.0.4 h1:vHD/YYe1Wolo78koG299f7V/VAS08c6IpCLn+Ejf/w8= github.com/olekukonko/tablewriter v0.0.4/go.mod h1:zq6QwlOf5SlnkVbMSr5EoBv3636FWnp+qbPhuoO21uA= +github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik= +github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=