Skip to content

Commit

Permalink
Support setting "Access-Control-Allow-Origin" header for API server
Browse files Browse the repository at this point in the history
Closes #27
  • Loading branch information
devgianlu committed Apr 16, 2024
1 parent dcccf4a commit 6b2fae9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
18 changes: 15 additions & 3 deletions cmd/daemon/api_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
const timeout = 10 * time.Second

type ApiServer struct {
allowOrigin string

close bool
listener net.Listener

Expand Down Expand Up @@ -226,8 +228,8 @@ type ApiEventDataShuffleContext struct {
Value bool `json:"value"`
}

func NewApiServer(address string, port int) (_ *ApiServer, err error) {
s := &ApiServer{}
func NewApiServer(address string, port int, allowOrigin string) (_ *ApiServer, err error) {
s := &ApiServer{allowOrigin: allowOrigin}
s.requests = make(chan ApiRequest)

s.listener, err = net.Listen("tcp", fmt.Sprintf("%s:%d", address, port))
Expand Down Expand Up @@ -262,6 +264,16 @@ func (s *ApiServer) handleRequest(req ApiRequest, w http.ResponseWriter) {
_ = json.NewEncoder(w).Encode(resp.data)
}

func (s *ApiServer) allowOriginMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if len(s.allowOrigin) > 0 {
w.Header().Set("Access-Control-Allow-Origin", s.allowOrigin)
}

next.ServeHTTP(w, req)
})
}

func (s *ApiServer) serve() {
m := http.NewServeMux()
m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -455,7 +467,7 @@ func (s *ApiServer) serve() {
}
})

err := http.Serve(s.listener, m)
err := http.Serve(s.listener, s.allowOriginMiddleware(m))
if s.close {
return
} else if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions cmd/daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,10 @@ type Config struct {
ExternalVolume bool `yaml:"external_volume"`
ZeroconfEnabled bool `yaml:"zeroconf_enabled"`
Server struct {
Enabled bool `yaml:"enabled"`
Address string `yaml:"address"`
Port int `yaml:"port"`
Enabled bool `yaml:"enabled"`
Address string `yaml:"address"`
Port int `yaml:"port"`
AllowOrigin string `yaml:"allow_origin"`
} `yaml:"server"`
Credentials struct {
Type string `yaml:"type"`
Expand Down Expand Up @@ -407,7 +408,7 @@ func main() {

// create api server if needed
if cfg.Server.Enabled {
app.server, err = NewApiServer(cfg.Server.Address, cfg.Server.Port)
app.server, err = NewApiServer(cfg.Server.Address, cfg.Server.Port, cfg.Server.AllowOrigin)
if err != nil {
log.WithError(err).Fatal("failed creating api server")
}
Expand Down
5 changes: 5 additions & 0 deletions config_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
"type": "integer",
"description": "Which port to bind for the API server",
"default": 0
},
"allow_origin": {
"type": "string",
"description": "The value for the Access-Control-Allow-Origin header",
"default": ""
}
}
},
Expand Down

0 comments on commit 6b2fae9

Please sign in to comment.