Skip to content
Snippets Groups Projects
Commit 9f1d8fd4 authored by Abdulhadi Al-Sayed's avatar Abdulhadi Al-Sayed
Browse files

updated procfile

parent 8cb64192
No related branches found
No related tags found
No related merge requests found
......@@ -6,9 +6,10 @@ import (
"github.com/go-chi/chi"
"io"
"net/http"
"strings"
)
func replyWithData(w io.Writer, response map[string]interface{}) {
func resWithData(w io.Writer, response map[string]interface{}) {
// handle JSON objects
err := json.NewEncoder(w).Encode(response)
if err != nil {
......@@ -19,32 +20,31 @@ func replyWithData(w io.Writer, response map[string]interface{}) {
// handleHistoryGet utility function, package level, to handle GET request to history route
func HandleHistoryGet(w http.ResponseWriter, r *http.Request) {
println("four")
// Set response to be of JSON type
http.Header.Add(w.Header(), "content-type", "application/json")
/*parts := strings.Split(r.URL.Path, "/")
parts := strings.Split(r.URL.Path, "/")
// error handling
if len(parts) != 8 || parts[5] != "exchangehistory" {
if len(parts) != 6 || parts[3] != "exchangehistory" {
http.Error(w, "Malformed URL", http.StatusBadRequest)
return
}
*/
// shortened function for extracting URL parameters
p := func(r *http.Request, key string) string {
return chi.URLParam(r, key)
}
// extract URL parameters
countryName := chi.URLParam(r, "country_name")
beginDate := chi.URLParam(r, "begin_date")
endDate := chi.URLParam(r, "end_date")
countryName := p(r, "country_name")
beginDate := p(r, "b_year") + "-" + p(r, "b_month") + "-" + p(r, "b_day")
endDate := p(r, "e_year") + "-" + p(r, "e_month") + "-" + p(r, "e_day")
// Request currency code for country
println("five")
currencyCode, err := GetCurrency(countryName)
println("six")
if err != nil { // Error handling bad request parameter for countryName
// In case of no server response, reply with 404
http.Error(w, err.Error(), http.StatusNotFound)
// Error could also be a 400, but we print that only internally
fmt.Println("HTTP status: " + err.Error())
}
/*
// Request currency history based on date period and currency code
result, err := GetHistory(beginDate, endDate, currencyCode, "") // last parameter empty because not part of request
if err != nil { // Error handling bad history request and json decoding
......@@ -53,21 +53,9 @@ func HandleHistoryGet(w http.ResponseWriter, r *http.Request) {
// Error could also be a 400 or failure in decoding, but we print that only internally
fmt.Println("HTTP/JSON status: " + err.Error())
}
*/
_, err = fmt.Fprintf(w, "%s %s / %s", currencyCode, beginDate, endDate)
if err != nil {
// TODO must handle the error!
}
/*
// handle JSON objects
err = json.NewEncoder(w).Encode(result)
if err != nil {
fmt.Println("ERROR encoding JSON", err)
}
*/
// Send result for processing
//replyWithData(w, result)
resWithData(w, result)
}
......@@ -75,10 +63,8 @@ func HandleHistoryGet(w http.ResponseWriter, r *http.Request) {
func HandlerHistory() func (http.ResponseWriter, *http.Request) {
httpHandler := func(w http.ResponseWriter, r *http.Request) {
println("two")
switch r.Method {
case http.MethodGet:
println("three")
HandleHistoryGet(w, r)
case http.MethodPost:
http.Error(w, "Not implemented", http.StatusNotImplemented)
......
web: exchangeserve
\ No newline at end of file
web: exchange
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment