Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server: allow cors http request #7939

Merged
merged 5 commits into from
Oct 21, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Config struct {
Host string `toml:"host" json:"host"`
AdvertiseAddress string `toml:"advertise-address" json:"advertise-address"`
Port uint `toml:"port" json:"port"`
Cors bool `toml:"cors" json:"cors"`
Store string `toml:"store" json:"store"`
Path string `toml:"path" json:"path"`
Socket string `toml:"socket" json:"socket"`
Expand Down Expand Up @@ -252,6 +253,7 @@ var defaultConf = Config{
Host: "0.0.0.0",
AdvertiseAddress: "",
Port: 4000,
Cors: false,
Store: "mocktikv",
Path: "/tmp/tidb",
RunDDL: true,
Expand Down
2 changes: 1 addition & 1 deletion server/http_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (s *Server) startHTTPServer() {
})

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

if len(s.cfg.Security.ClusterSSLCA) != 0 {
err = s.statusServer.ListenAndServeTLS(s.cfg.Security.ClusterSSLCert, s.cfg.Security.ClusterSSLKey)
Expand Down
16 changes: 16 additions & 0 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ package server
import (
"bytes"
"encoding/binary"
"github.com/pingcap/tidb/config"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put this import below the native library, and leave an empty line between them.

"io"
"math"
"net/http"
"strconv"
"time"

Expand Down Expand Up @@ -354,3 +356,17 @@ func appendFormatFloat(in []byte, fVal float64, prec, bitSize int) []byte {
}
return out
}

// CorsHandler add Cors Header if `cors` config is setted
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/ setted/ set
s/ add/ adds
add a . at the end of this comment

type CorsHandler struct {
handler http.Handler
cfg *config.Config
}

func (h CorsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if h.cfg.Cors {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET")
}
h.handler.ServeHTTP(w, req)
}
5 changes: 5 additions & 0 deletions tidb-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const (
nmHost = "host"
nmAdvertiseAddress = "advertise-address"
nmPort = "P"
nmCors = "cors"
nmSocket = "socket"
nmBinlogSocket = "binlog-socket"
nmRunDDL = "run-ddl"
Expand Down Expand Up @@ -91,6 +92,7 @@ var (
host = flag.String(nmHost, "0.0.0.0", "tidb server host")
advertiseAddress = flag.String(nmAdvertiseAddress, "", "tidb server advertise IP")
port = flag.String(nmPort, "4000", "tidb server port")
cors = flagBoolean(nmCors, false, "tidb server allow cors request")
socket = flag.String(nmSocket, "", "The socket file to use for connection.")
binlogSocket = flag.String(nmBinlogSocket, "", "socket file to write binlog")
runDDL = flagBoolean(nmRunDDL, true, "run ddl worker on this tidb-server")
Expand Down Expand Up @@ -278,6 +280,9 @@ func overrideConfig() {
terror.MustNil(err)
cfg.Port = uint(p)
}
if actualFlags[nmCors] {
cfg.Cors = *cors
}
if actualFlags[nmStore] {
cfg.Store = *store
}
Expand Down