Files
go-opt-pricer/serve.go

44 lines
792 B
Go

package main
import (
"encoding/json"
"fmt"
"net/http"
)
// index is a function that handles all requests to the main page
// Note that there are two separate returns
func index(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
mcOpt := &Option{}
bsOpt := &Option{}
err := json.NewDecoder(r.Body).Decode(&mcOpt)
if err != nil {
fmt.Printf("Error: %s\n", err)
}
mcOpt.PriceMonteCarlo()
options := make(map[string]Option)
options["MonteCarlo"] = *mcOpt
options["BlackScholes"] = *bsOpt
json.NewEncoder(w).Encode(options)
if err != nil {
fmt.Printf("Error: %v\n", err)
}
} else if r.Method == "GET" {
fmt.Fprintf(w, "Hello go!") // To be updated
}
}
func main() {
http.HandleFunc("/", index)
http.ListenAndServe(":8080", nil)
}