Skip to content

Commit

Permalink
feat: Use rs/cors for handling CORS (allow all)
Browse files Browse the repository at this point in the history
Signed-off-by: Ashish Kumar <ashishkr23438@gmail.com>
  • Loading branch information
krashish8 committed Jan 27, 2022
1 parent ecfa81c commit b313ca2
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/jackc/pgconn v1.10.1
github.com/jackc/pgx/v4 v4.14.1
github.com/mitchellh/mapstructure v1.4.3
github.com/rs/cors v1.8.2
github.com/sirupsen/logrus v1.8.1
github.com/spf13/viper v1.10.1
github.com/stretchr/testify v1.7.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ github.com/googleapis/gax-go/v2 v2.1.1/go.mod h1:hddJymUZASv3XPyGkUpKj8pPO47Rmb0
github.com/googleapis/gnostic v0.4.1/go.mod h1:LRhVm6pbyptWbWbuZ38d1eyptfvIytN3ir6b65WBswg=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/handlers v0.0.0-20150720190736-60c7bfde3e33/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ=
github.com/gorilla/handlers v1.4.2 h1:0QniY0USkHQ1RGCLfKxeNHK9bkDHGRYGNDFBCS+YARg=
github.com/gorilla/handlers v1.4.2/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ=
github.com/gorilla/mux v1.7.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
Expand Down Expand Up @@ -1057,6 +1058,8 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/rs/cors v1.8.2 h1:KCooALfAYGs415Cwu5ABvv9n9509fSiG5SQJn/AQo4U=
github.com/rs/cors v1.8.2/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU=
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
Expand Down
5 changes: 4 additions & 1 deletion internal/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/go-playground/validator/v10"
"github.com/gorilla/mux"
"github.com/jackc/pgx/v4"
"github.com/rs/cors"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -65,7 +66,9 @@ func NewServer(conn *pgx.Conn) *Server {

func (server *Server) Start(port string) error {
logrus.Info("Serving requests on port", port)
return http.ListenAndServe(port, util.Logger(server.Router))

// handle CORS
return http.ListenAndServe(port, util.Logger(cors.AllowAll().Handler(server.Router)))
}

func (server *Server) handleRoutes(router *mux.Router) {
Expand Down
1 change: 0 additions & 1 deletion internal/util/format_ical.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ func SerializeICal(calendar []ICal) string {
func (r *Formatter) FormatICAL(w http.ResponseWriter, respCode int, calendar []ICal, filename string) {
// Set the content-type, content-disposition, and response code in the header
w.Header().Set("Content-Type", "text/calendar")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))
w.WriteHeader(respCode)

Expand Down
1 change: 0 additions & 1 deletion internal/util/format_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func (r *Formatter) FormatJSON(w http.ResponseWriter, respCode int, data interfa

// Set the content-type and response code in the header
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.WriteHeader(respCode)

if data == nil {
Expand Down
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ along with pg_scheduleserv. If not, see <https://www.gnu.org/licenses/>.
******************************************************************GRP-GNU-AGPL*/


package main

import (
Expand Down

0 comments on commit b313ca2

Please sign in to comment.