So, I have a golang application and reactJs application. If I run them on the different domains, I get problems with CORS.
To allow CORS on the server side is not a problem at all, but I can't get cookies from the server, because they are ignored by browser as third-party cookies.
So, I see quite a simple solution - to run server and frontend on the same domain, but I can't understand, how to do that. How could I configure everything to run on the same domain without conflicts? What are the best practices to prevent CORS between golang app and ReactJS app?
Use Go's file server to serve the static assets:
package main
import (
"log"
"net/http"
)
func main() {
http.HandleFunc("/api/", apiHandler)
http.Handle("/", http.FileServer(http.Dir("/var/www/my-app")))
log.Fatal(http.ListenAndServe(":3000", nil))
}
func apiHandler(w http.ResponseWriter, r *http.Request) {}
The documentation has more examples for http.FileServer.
Alternatively, you can use something like github.com/jteeuwen/go-bindata (and go-bindata-assetfs) to compile your assets into the Go binary and serve them from memory.