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

basic structure of REST app implemented

parents
No related branches found
No related tags found
No related merge requests found
package exchangeserve
import (
"net/http"
)
// HandlerHistory main handler for route related to `/student` requests
func HandlerHistory() func (http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
handleHistoryGet(w, r)
case http.MethodPost:
http.Error(w, "Not implemented", http.StatusNotImplemented)
case http.MethodPut:
http.Error(w, "Not implemented", http.StatusNotImplemented)
case http.MethodDelete:
http.Error(w, "Not implemented", http.StatusNotImplemented)
}
}
}
// handleHistoryGet utility function, package level, to handle GET request to history route
func handleHistoryGet(w http.ResponseWriter, r *http.Request) {
http.Header.Add(w.Header(), "content-type", "application/json")
/*
parts := strings.Split(r.URL.Path, "/")
// error handling
if len(parts) != 3 || parts[1] != "student" {
http.Error(w, "Malformed URL", http.StatusBadRequest)
return
}
// handle the request /student/ which will return ALL students as array of JSON objects
if parts[2] == "" {
replyWithAllStudents(w, db)
} else {
replyWithStudent(w, db, parts[2])
}
*/
}
\ No newline at end of file
package main
import (
"fmt"
"html"
"log"
"net/http"
"os"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
)
func main() {
port := os.Getenv("PORT")
if port == "" {
log.Fatal("$PORT must be set")
}
// Define new router
r := chi.NewRouter()
// Logs the start and end of each request with the elapsed processing time
r.Use(middleware.Logger)
r.Get("/exchange/v1/exchangehistory/")
r.Get("/exchange/v1/exchangeborder/")
r.Get("/exchange/v1/diag/")
log.Fatal(http.ListenAndServe(":"+port, r))
}
\ No newline at end of file
package exchangeserve
import (
"github.com/alediaferia/gocountries"
)
/*
GetCurrency returns a string of specified Country's currency code e.g.(NOK, USD, EUR...)
*/
func GetCurrency(countryName string) (string, error) {
// Query for structs of possible countries
countries, err := gocountries.CountriesByName(countryName)
// Extract first country
country := (countries)[0]
// Extract currency code
currencyCode := country.Currencies[0]
return currencyCode, err
}
package exchangeserve
/*
GetHistory returns a map of a decoded json object with
specified country's history of exchange rates based on date specified.
*/
func GetCurrenc(countryName, datePeriod string) ()
resp, err := http.Get("http://example.com/")
\ No newline at end of file
go.mod 0 → 100644
module exchangeserve
// +heroku goVersion go1.13
go 1.13
require (
github.com/alediaferia/gocountries v0.0.0-20150710192521-aaf4e81715ec
github.com/go-chi/chi v1.5.3
)
web: exchangeserve
\ 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