Browse Source

Add CORS Header

pull/9/head
garritfra 5 years ago
parent
commit
5a44644d17
  1. 12
      server/webserver.go

12
server/webserver.go

@ -57,16 +57,23 @@ func registerRouteHandlers() {
http.HandleFunc("/add_peers", handleAddPeers) http.HandleFunc("/add_peers", handleAddPeers)
} }
func enableCors(w *http.ResponseWriter) {
(*w).Header().Set("Access-Control-Allow-Origin", "*")
}
func handleError(err error, w http.ResponseWriter, r *http.Request) { func handleError(err error, w http.ResponseWriter, r *http.Request) {
enableCors(&w)
w.Write([]byte("Error: " + err.Error())) w.Write([]byte("Error: " + err.Error()))
log.Print(err.Error()) log.Print(err.Error())
} }
func handleListBlocks(w http.ResponseWriter, r *http.Request) { func handleListBlocks(w http.ResponseWriter, r *http.Request) {
enableCors(&w)
json.NewEncoder(w).Encode(blockchain.AsJSON()) json.NewEncoder(w).Encode(blockchain.AsJSON())
} }
func handleAddTransaction(w http.ResponseWriter, r *http.Request) { func handleAddTransaction(w http.ResponseWriter, r *http.Request) {
enableCors(&w)
decoder := json.NewDecoder(r.Body) decoder := json.NewDecoder(r.Body)
var receivedTransaction core.Transaction var receivedTransaction core.Transaction
err := decoder.Decode(&receivedTransaction) err := decoder.Decode(&receivedTransaction)
@ -80,21 +87,25 @@ func handleAddTransaction(w http.ResponseWriter, r *http.Request) {
} }
func handleMineBlock(w http.ResponseWriter, r *http.Request) { func handleMineBlock(w http.ResponseWriter, r *http.Request) {
enableCors(&w)
block := blockchain.MineBlock() block := blockchain.MineBlock()
json.NewEncoder(w).Encode(block) json.NewEncoder(w).Encode(block)
} }
func handleListPendingTransactions(w http.ResponseWriter, r *http.Request) { func handleListPendingTransactions(w http.ResponseWriter, r *http.Request) {
enableCors(&w)
json.NewEncoder(w).Encode(blockchain.PendingTransactions) json.NewEncoder(w).Encode(blockchain.PendingTransactions)
} }
func handleIsValid(w http.ResponseWriter, r *http.Request) { func handleIsValid(w http.ResponseWriter, r *http.Request) {
enableCors(&w)
valid := blockchain.IsValid() valid := blockchain.IsValid()
json.NewEncoder(w).Encode(valid) json.NewEncoder(w).Encode(valid)
} }
// Takes an a string-slice, and adds it to the known peers // Takes an a string-slice, and adds it to the known peers
func handleAddPeers(w http.ResponseWriter, r *http.Request) { func handleAddPeers(w http.ResponseWriter, r *http.Request) {
enableCors(&w)
decoder := json.NewDecoder(r.Body) decoder := json.NewDecoder(r.Body)
var receivedPeers []string var receivedPeers []string
err := decoder.Decode(&receivedPeers) err := decoder.Decode(&receivedPeers)
@ -114,5 +125,6 @@ func handleAddPeers(w http.ResponseWriter, r *http.Request) {
} }
func handleUpdate(w http.ResponseWriter, r *http.Request) { func handleUpdate(w http.ResponseWriter, r *http.Request) {
enableCors(&w)
json.NewEncoder(w).Encode(blockchain.Update()) json.NewEncoder(w).Encode(blockchain.Update())
} }

Loading…
Cancel
Save