fix cors
This commit is contained in:
6
main.go
6
main.go
@@ -34,6 +34,12 @@ func main() {
|
||||
|
||||
router := router.New()
|
||||
|
||||
router.Handle("OPTIONS /", func(w http.ResponseWriter, r *http.Request) error {
|
||||
w.WriteHeader(200)
|
||||
fmt.Fprintf(w, "ok")
|
||||
return nil
|
||||
})
|
||||
|
||||
router.Handle("POST /auth/register", func(w http.ResponseWriter, r *http.Request) error {
|
||||
var body struct {
|
||||
Email string `json:"email"`
|
||||
|
||||
@@ -32,6 +32,11 @@ func (router *router) SendJSON(w http.ResponseWriter, data any) error {
|
||||
|
||||
func (router *router) Handle(pattern string, handler func(w http.ResponseWriter, r *http.Request) error) {
|
||||
router.mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:3000")
|
||||
w.Header().Set("Access-Control-Allow-Methods", "GET,POST,PATCH,DELETE,OPTIONS")
|
||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type,Content-Length")
|
||||
|
||||
if err := handler(w, r); err != nil {
|
||||
router.SendJSON(w, struct {
|
||||
Error string `json:"error"`
|
||||
|
||||
Reference in New Issue
Block a user