Skip to content

Commit 486b22b

Browse files
committed
chore(api): add option to disable CORS to aid development
1 parent 482603d commit 486b22b

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

cmd/serve.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,19 @@ func executeServeCommand(appContext cli.AppContext) runEFunc {
161161

162162
// Create the router
163163
r := mux.NewRouter()
164+
165+
if appContext.Config.DisableCORS {
166+
log.Ctx(ctx).Info().Msg("Disabling CORS")
167+
r.Use(func(next http.Handler) http.Handler {
168+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
169+
w.Header().Set("Access-Control-Allow-Origin", "*")
170+
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
171+
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
172+
next.ServeHTTP(w, r)
173+
})
174+
})
175+
}
176+
164177
apiRouter := r.PathPrefix(baseURL + "/").Subrouter()
165178

166179
// Use our validation middleware to check all requests against the

pkg/api/api.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ func (a *amplifyAPI) GetV0(w http.ResponseWriter, r *http.Request) {
7373
fallthrough
7474
case "application/vnd.api+json":
7575
w.Header().Set("Content-Type", "application/vnd.api+json")
76-
w.Header().Set("Access-Control-Allow-Origin", "*")
7776
err := json.NewEncoder(w).Encode(home)
7877
if err != nil {
7978
sendError(r.Context(), w, http.StatusInternalServerError, "Could not render JSON", err.Error())
@@ -102,7 +101,6 @@ func (a *amplifyAPI) GetV0Jobs(w http.ResponseWriter, r *http.Request) {
102101
fallthrough
103102
case "application/vnd.api+json":
104103
w.Header().Set("Content-Type", "application/vnd.api+json")
105-
w.Header().Set("Access-Control-Allow-Origin", "*")
106104
err := json.NewEncoder(w).Encode(jobs)
107105
if err != nil {
108106
sendError(r.Context(), w, http.StatusInternalServerError, "Could not render JSON", err.Error())
@@ -135,7 +133,6 @@ func (a *amplifyAPI) GetV0JobsId(w http.ResponseWriter, r *http.Request, id stri
135133
fallthrough
136134
case "application/vnd.api+json":
137135
w.Header().Set("Content-Type", "application/vnd.api+json")
138-
w.Header().Set("Access-Control-Allow-Origin", "*")
139136
err := json.NewEncoder(w).Encode(j)
140137
if err != nil {
141138
sendError(r.Context(), w, http.StatusInternalServerError, "Could not render JSON", err.Error())
@@ -178,7 +175,6 @@ func (a *amplifyAPI) GetV0Queue(w http.ResponseWriter, r *http.Request, params G
178175
fallthrough
179176
case "application/vnd.api+json":
180177
w.Header().Set("Content-Type", "application/vnd.api+json")
181-
w.Header().Set("Access-Control-Allow-Origin", "*")
182178
err := json.NewEncoder(w).Encode(executions)
183179
if err != nil {
184180
sendError(r.Context(), w, http.StatusInternalServerError, "Could not render JSON", err.Error())
@@ -211,7 +207,6 @@ func (a *amplifyAPI) GetV0QueueId(w http.ResponseWriter, r *http.Request, id ope
211207
fallthrough
212208
case "application/vnd.api+json":
213209
w.Header().Set("Content-Type", "application/vnd.api+json")
214-
w.Header().Set("Access-Control-Allow-Origin", "*")
215210
err := json.NewEncoder(w).Encode(e)
216211
if err != nil {
217212
sendError(r.Context(), w, http.StatusInternalServerError, "Could not render JSON", err.Error())
@@ -230,7 +225,6 @@ func (a *amplifyAPI) GetV0QueueId(w http.ResponseWriter, r *http.Request, id ope
230225
// (PUT /v0/queue/{id})
231226
func (a *amplifyAPI) PutV0QueueId(w http.ResponseWriter, r *http.Request, id openapi_types.UUID) {
232227
log.Ctx(r.Context()).Trace().Str("id", id.String()).Msg("PutV0QueueId")
233-
w.Header().Set("Access-Control-Allow-Origin", "*")
234228
var body ExecutionRequest
235229
switch r.Header.Get("Content-Type") {
236230
case "application/json":
@@ -255,7 +249,6 @@ func (a *amplifyAPI) PutV0QueueId(w http.ResponseWriter, r *http.Request, id ope
255249
func (a *amplifyAPI) PostV0Queue(w http.ResponseWriter, r *http.Request) {
256250
log.Ctx(r.Context()).Trace().Msg("PostV0Queue")
257251
var body ExecutionRequest
258-
w.Header().Set("Access-Control-Allow-Origin", "*")
259252
switch r.Header.Get("Content-Type") {
260253
case "application/x-www-form-urlencoded":
261254
err := r.ParseForm()
@@ -340,7 +333,6 @@ func (a *amplifyAPI) GetV0Graph(w http.ResponseWriter, r *http.Request) {
340333
fallthrough
341334
case "application/vnd.api+json":
342335
w.Header().Set("Content-Type", "application/vnd.api+json")
343-
w.Header().Set("Access-Control-Allow-Origin", "*")
344336
err := json.NewEncoder(w).Encode(outputGraph)
345337
if err != nil {
346338
sendError(r.Context(), w, http.StatusInternalServerError, "Could not render JSON", err.Error())
@@ -604,7 +596,6 @@ func sendError(ctx context.Context, w http.ResponseWriter, statusCode int, userE
604596
Detail: &devErr,
605597
}
606598
w.Header().Set("Content-Type", "application/vnd.api+json")
607-
w.Header().Set("Access-Control-Allow-Origin", "*")
608599
w.WriteHeader(statusCode)
609600
err := json.NewEncoder(w).Encode(&Errors{e})
610601
if err != nil {

pkg/config/appConfig.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const (
2828
NumConcurrentNodesFlag = "num-concurrent-nodes"
2929
NumConcurrentWorkflowsFlag = "num-concurrent-workflows"
3030
MaxWaitingWorkflowsFlag = "max-waiting-workflows"
31+
DisableCORSFlag = "disable-cors"
3132
)
3233

3334
type AppConfig struct {
@@ -39,6 +40,7 @@ type AppConfig struct {
3940
NodeConcurrency int `yaml:"concurrency"`
4041
WorkflowConcurrency int `yaml:"workflow-concurrency"`
4142
MaxWaitingWorkflows int `yaml:"max-waiting-workflows"`
43+
DisableCORS bool `yaml:"disable-cors"`
4244
}
4345

4446
type Trigger struct {
@@ -80,13 +82,21 @@ func ParseAppConfig(cmd *cobra.Command) *AppConfig {
8082
log.Ctx(ctx).Warn().Msgf("Max waiting workflows (%d) is less than workflow concurrency (%d), reducing %s to match", maxWaitingWorkflowsInt, workflowConcurrencyInt, NumConcurrentWorkflowsFlag)
8183
workflowConcurrencyInt = maxWaitingWorkflowsInt
8284
}
85+
disableCORS, err := cmd.Flags().GetBool(DisableCORSFlag)
86+
if err != nil {
87+
log.Ctx(ctx).Fatal().Err(err).Str("flag", DisableCORSFlag).Msg("Failed to parse")
88+
}
89+
ipfsSearchEnabled, err := cmd.Flags().GetBool(IPFSSearchEnabledFlag)
90+
if err != nil {
91+
log.Ctx(ctx).Fatal().Err(err).Str("flag", IPFSSearchEnabledFlag).Msg("Failed to parse")
92+
}
8393
return &AppConfig{
8494
LogLevel: logLevel,
8595
ConfigPath: cmd.Flag(ConfigPathFlag).Value.String(),
8696
Port: port,
8797
Trigger: Trigger{
8898
IPFSSearch: IPFSSearch{
89-
Enabled: cmd.Flag(IPFSSearchEnabledFlag).Value.String() == "true",
99+
Enabled: ipfsSearchEnabled,
90100
QueryURL: cmd.Flag(IPFSSearchQueryURLFlag).Value.String(),
91101
},
92102
},
@@ -96,6 +106,7 @@ func ParseAppConfig(cmd *cobra.Command) *AppConfig {
96106
NodeConcurrency: nodeConcurrencyInt,
97107
WorkflowConcurrency: workflowConcurrencyInt,
98108
MaxWaitingWorkflows: maxWaitingWorkflowsInt,
109+
DisableCORS: disableCORS,
99110
}
100111
}
101112

@@ -110,6 +121,7 @@ func AddGlobalFlags(cmd *cobra.Command) {
110121
cmd.PersistentFlags().Int(NumConcurrentNodesFlag, 10, "Number of concurrent nodes to run at one time")
111122
cmd.PersistentFlags().Int(NumConcurrentWorkflowsFlag, 10, "Number of concurrent workflows to run at one time")
112123
cmd.PersistentFlags().Int(MaxWaitingWorkflowsFlag, 100, "Maximum number of workflows to queue up before rejecting new workflows")
124+
cmd.PersistentFlags().Bool(DisableCORSFlag, false, "Disable CORS")
113125
}
114126

115127
func InitViper(cmd *cobra.Command) (*viper.Viper, error) {

0 commit comments

Comments
 (0)