Skip to content

Commit f4d4127

Browse files
Kingwlzz-jason
authored andcommitted
server: allow cors http request (#7939)
1 parent 25118db commit f4d4127

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

config/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type Config struct {
4747
Host string `toml:"host" json:"host"`
4848
AdvertiseAddress string `toml:"advertise-address" json:"advertise-address"`
4949
Port uint `toml:"port" json:"port"`
50+
Cors string `toml:"cors" json:"cors"`
5051
Store string `toml:"store" json:"store"`
5152
Path string `toml:"path" json:"path"`
5253
Socket string `toml:"socket" json:"socket"`
@@ -252,6 +253,7 @@ var defaultConf = Config{
252253
Host: "0.0.0.0",
253254
AdvertiseAddress: "",
254255
Port: 4000,
256+
Cors: "",
255257
Store: "mocktikv",
256258
Path: "/tmp/tidb",
257259
RunDDL: true,

server/http_status.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (s *Server) startHTTPServer() {
117117
})
118118

119119
log.Infof("Listening on %v for status and metrics report.", addr)
120-
s.statusServer = &http.Server{Addr: addr, Handler: serverMux}
120+
s.statusServer = &http.Server{Addr: addr, Handler: CorsHandler{handler: serverMux, cfg: s.cfg}}
121121

122122
if len(s.cfg.Security.ClusterSSLCA) != 0 {
123123
err = s.statusServer.ListenAndServeTLS(s.cfg.Security.ClusterSSLCert, s.cfg.Security.ClusterSSLKey)

server/util.go

+16
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ import (
3939
"encoding/binary"
4040
"io"
4141
"math"
42+
"net/http"
4243
"strconv"
4344
"time"
4445

46+
"github.com/pingcap/tidb/config"
4547
"github.com/pingcap/tidb/mysql"
4648
"github.com/pingcap/tidb/types"
4749
"github.com/pingcap/tidb/util/chunk"
@@ -354,3 +356,17 @@ func appendFormatFloat(in []byte, fVal float64, prec, bitSize int) []byte {
354356
}
355357
return out
356358
}
359+
360+
// CorsHandler adds Cors Header if `cors` config is set.
361+
type CorsHandler struct {
362+
handler http.Handler
363+
cfg *config.Config
364+
}
365+
366+
func (h CorsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
367+
if h.cfg.Cors != "" {
368+
w.Header().Set("Access-Control-Allow-Origin", h.cfg.Cors)
369+
w.Header().Set("Access-Control-Allow-Methods", "GET")
370+
}
371+
h.handler.ServeHTTP(w, req)
372+
}

tidb-server/main.go

+6
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ const (
6464
nmHost = "host"
6565
nmAdvertiseAddress = "advertise-address"
6666
nmPort = "P"
67+
nmCors = "cors"
6768
nmSocket = "socket"
6869
nmBinlogSocket = "binlog-socket"
6970
nmRunDDL = "run-ddl"
@@ -91,6 +92,7 @@ var (
9192
host = flag.String(nmHost, "0.0.0.0", "tidb server host")
9293
advertiseAddress = flag.String(nmAdvertiseAddress, "", "tidb server advertise IP")
9394
port = flag.String(nmPort, "4000", "tidb server port")
95+
cors = flag.String(nmCors, "", "tidb server allow cors origin")
9496
socket = flag.String(nmSocket, "", "The socket file to use for connection.")
9597
binlogSocket = flag.String(nmBinlogSocket, "", "socket file to write binlog")
9698
runDDL = flagBoolean(nmRunDDL, true, "run ddl worker on this tidb-server")
@@ -278,6 +280,10 @@ func overrideConfig() {
278280
terror.MustNil(err)
279281
cfg.Port = uint(p)
280282
}
283+
if actualFlags[nmCors] {
284+
fmt.Println(cors)
285+
cfg.Cors = *cors
286+
}
281287
if actualFlags[nmStore] {
282288
cfg.Store = *store
283289
}

0 commit comments

Comments
 (0)