Skip to content

Commit 65b0589

Browse files
committed
(feat): add comments
1 parent aa132be commit 65b0589

File tree

4 files changed

+15
-0
lines changed

4 files changed

+15
-0
lines changed

cmd/server/convert.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"time"
1414
)
1515

16+
// Validate ... Validation function for convertReqObj
1617
func (c *convertReqObj) Validate() error {
1718

1819
if ex.CurrencyList[c.To] == "" || ex.CurrencyList[c.From] == "" {
@@ -23,6 +24,7 @@ func (c *convertReqObj) Validate() error {
2324
return nil
2425
}
2526

27+
// Hash ... return md5 string hash of the convertReqObj with 1 Unit Amount to cache the rate only for 1 Unit Amount
2628
func (c convertReqObj) Hash() string {
2729
// hash exchange key only with 1 Unit value
2830
c.Amount = 1
@@ -31,6 +33,7 @@ func (c convertReqObj) Hash() string {
3133
return fmt.Sprintf("%x", md5Sum[:])
3234
}
3335

36+
// Convert ... Main convert function attached to the router handler
3437
var Convert = func(w http.ResponseWriter, r *http.Request) {
3538
if r.Method == "POST" {
3639
ConvertPost(w, r)
@@ -40,6 +43,7 @@ var Convert = func(w http.ResponseWriter, r *http.Request) {
4043
}
4144
}
4245

46+
// ConvertGet ... handle GET request and simulate payload from get query params to ConvertPost function
4347
var ConvertGet = func(w http.ResponseWriter, r *http.Request) {
4448

4549
query := r.URL.Query()
@@ -70,6 +74,7 @@ var ConvertGet = func(w http.ResponseWriter, r *http.Request) {
7074
ConvertPost(w, r)
7175
}
7276

77+
// ConvertPost ... handle POST request, build Swap object, get and cache the currency exchange rate and amount
7378
var ConvertPost = func(w http.ResponseWriter, r *http.Request) {
7479

7580
convertReq := &convertReqObj{}

cmd/server/helpers.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import (
77
"path/filepath"
88
)
99

10+
// GetEnv ... get value from env variables or return the fallback value
1011
func GetEnv(key, fallback string) string {
1112
if value, ok := os.LookupEnv(key); ok {
1213
return value
1314
}
1415
return fallback
1516
}
1617

18+
// LsFiles ... list file in directory
1719
func LsFiles(pattern string) {
1820
err := filepath.Walk(pattern,
1921
func(path string, info os.FileInfo, err error) error {

cmd/server/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ var (
3636
staticPath = defaultStaticPath
3737
)
3838

39+
// init ... init function of the server
3940
func init() {
4041
// Logging
4142
backendStderr := logging.NewLogBackend(os.Stderr, "", 0)
@@ -69,6 +70,7 @@ func init() {
6970

7071
}
7172

73+
// main ... main function start the server
7274
func main() {
7375

7476
Logger.Infof("host %s", host)
@@ -86,6 +88,7 @@ func main() {
8688
select {}
8789
}
8890

91+
// serveHTTP ... initiate the HTTP Server
8992
func serveHTTP(host string, port int) {
9093

9194
mux := http.NewServeMux()

cmd/server/types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import (
44
"github.com/op/go-logging"
55
)
66

7+
// exchangerReqObj ... Exchanger object that form array of Exchangers in the Convert Request Data Object
78
type exchangerReqObj struct {
89
Name string `json:"name"`
910
UserAgent string `json:"userAgent,omitempty"`
1011
ApiKey string `json:"apiKey,omitempty"`
1112
ApiVersion string `json:"apiVersion,omitempty"`
1213
}
1314

15+
// convertReqObj ... Convert Request Data Object
1416
type convertReqObj struct {
1517
Amount float64 `json:"amount"`
1618
Exchanger []exchangerReqObj
@@ -20,6 +22,7 @@ type convertReqObj struct {
2022
DecimalPoints int `json:"decimalPoints"`
2123
}
2224

25+
// convertResObj ... Convert Response Data Object
2326
type convertResObj struct {
2427
From string `json:"from"`
2528
To string `json:"to"`
@@ -34,8 +37,10 @@ type convertResObj struct {
3437

3538
// Password is just an example type implementing the Redactor interface. Any
3639
// time this is logged, the Redacted() function will be called.
40+
// Secret ... Secret Type for logging in the Logger
3741
type Secret string
3842

43+
// Redacted ... Secret Redacted function
3944
func (p Secret) Redacted() interface{} {
4045
return logging.Redact(string(p))
4146
}

0 commit comments

Comments
 (0)