diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..99924b7 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "ApolloNIA"] + path = ApolloNIA + url = https://github.com/dborowiec10/ApolloNIA.git diff --git a/ApolloNIA b/ApolloNIA new file mode 160000 index 0000000..6fd6d1c --- /dev/null +++ b/ApolloNIA @@ -0,0 +1 @@ +Subproject commit 6fd6d1cba1e6d1046173e294ae2c990b847eabd0 diff --git a/pkg/apollo/server.go b/pkg/apollo/server.go index 40fd57b..6eed09e 100644 --- a/pkg/apollo/server.go +++ b/pkg/apollo/server.go @@ -31,18 +31,35 @@ const localRecordsPath = "./data/local.json" var data []schema.Record +func enableCors(w *http.ResponseWriter) { + (*w).Header().Set("Access-Control-Allow-Origin", "*") +} + +func setupResponse(w *http.ResponseWriter, req *http.Request) { + (*w).Header().Set("Access-Control-Allow-Origin", "*") + (*w).Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE") + (*w).Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization") +} + func index(w http.ResponseWriter, r *http.Request) { + setupResponse(&w, r) + if (*r).Method == "OPTIONS" { + return + } indexFile, err := os.Open("./static/index.html") if err != nil { io.WriteString(w, "error reading index") return } defer indexFile.Close() - io.Copy(w, indexFile) } func scrape(w http.ResponseWriter, r *http.Request) { + setupResponse(&w, r) + if (*r).Method == "OPTIONS" { + return + } linkToScraoe := r.FormValue("q") w.Header().Set("Content-Type", "application/json") result, err := schema.Scrape(linkToScraoe) @@ -55,6 +72,10 @@ func scrape(w http.ResponseWriter, r *http.Request) { } func addData(w http.ResponseWriter, r *http.Request) { + setupResponse(&w, r) + if (*r).Method == "OPTIONS" { + return + } var newData schema.Data err := jsoniter.NewDecoder(r.Body).Decode(&newData) if err != nil { @@ -74,6 +95,10 @@ func addData(w http.ResponseWriter, r *http.Request) { } func search(w http.ResponseWriter, r *http.Request) { + setupResponse(&w, r) + if (*r).Method == "OPTIONS" { + return + } searchQuery := r.FormValue("q") w.Header().Set("Content-Type", "application/json") fmt.Println(searchQuery) @@ -87,6 +112,10 @@ func search(w http.ResponseWriter, r *http.Request) { } func authenticatePassword(w http.ResponseWriter, r *http.Request) { + setupResponse(&w, r) + if (*r).Method == "OPTIONS" { + return + } type Request struct { Password string `json:"password"` }