Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7ccbb9b

Browse files
committed
feat(http): add flag/config to allow CORS requests, close #229
1 parent 37bdf46 commit 7ccbb9b

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

commands/local_server_start.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import (
5252

5353
var localWebServerProdWarningMsg = "The local web server is optimized for local development and MUST never be used in a production setup."
5454
var localWebServerTlsKeyLogWarningMsg = "Logging TLS master key is enabled. It means TLS connections between the client and this server will be INSECURE. This is NOT recommended unless you are debugging the connections."
55+
var localWebServerAllowCorsLogWarningMsg = "Cross-origin resource sharing (CORS) is enabled for every requests.\nYou may want to use https://github.com/nelmio/NelmioCorsBundle to have more control on HTTP headers."
5556

5657
var localServerStartCmd = &console.Command{
5758
Category: "local",
@@ -81,6 +82,7 @@ var localServerStartCmd = &console.Command{
8182
EnvVars: []string{"SSLKEYLOGFILE"},
8283
},
8384
&console.BoolFlag{Name: "no-workers", Usage: "Do not start workers"},
85+
&console.BoolFlag{Name: "allow-cors", Usage: "Allow Cross-origin resource sharing (CORS) requests"},
8486
},
8587
Action: func(c *console.Context) error {
8688
ui := terminal.SymfonyStyle(terminal.Stdout, terminal.Stdin)
@@ -186,6 +188,10 @@ var localServerStartCmd = &console.Command{
186188
ui.Warning(localWebServerTlsKeyLogWarningMsg)
187189
}
188190

191+
if config.AllowCORS {
192+
ui.Warning(localWebServerAllowCorsLogWarningMsg)
193+
}
194+
189195
lw, err := pidFile.LogWriter()
190196
if err != nil {
191197
return err

local/http/http.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type Server struct {
5555
Appversion string
5656
UseGzip bool
5757
TlsKeyLogFile string
58+
AllowCORS bool
5859

5960
httpserver *http.Server
6061
httpsserver *http.Server
@@ -212,6 +213,10 @@ func (s *Server) ProxyHandler(w http.ResponseWriter, r *http.Request) {
212213

213214
// Handler handles HTTP requests
214215
func (s *Server) Handler(w http.ResponseWriter, r *http.Request) {
216+
if s.AllowCORS {
217+
w.Header().Add("Access-Control-Allow-Origin", "*")
218+
}
219+
215220
// static file?
216221
if !strings.HasSuffix(strings.ToLower(r.URL.Path), ".php") {
217222
p := r.URL.Path

local/project/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type Config struct {
4848
UseGzip bool `yaml:"use_gzip"`
4949
TlsKeyLogFile string `yaml:"tls_key_log_file"`
5050
NoWorkers bool `yaml:"no_workers"`
51+
AllowCORS bool `yaml:"allow_cors"`
5152
}
5253

5354
type FileConfig struct {
@@ -116,6 +117,9 @@ func NewConfigFromContext(c *console.Context, projectDir string) (*Config, *File
116117
if c.IsSet("no-workers") {
117118
config.NoWorkers = c.Bool("no-workers")
118119
}
120+
if c.IsSet("allow-cors") {
121+
config.AllowCORS = c.Bool("allow-cors")
122+
}
119123

120124
return config, fileConfig, nil
121125
}

local/project/project.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func New(c *Config) (*Project, error) {
6262
UseGzip: c.UseGzip,
6363
Appversion: c.AppVersion,
6464
TlsKeyLogFile: c.TlsKeyLogFile,
65+
AllowCORS: c.AllowCORS,
6566
},
6667
}
6768
if err != nil {

0 commit comments

Comments
 (0)