Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
covidcase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Abdulhadi Al-Sayed
covidcase
Commits
67e9e4cd
Commit
67e9e4cd
authored
4 years ago
by
Abdulhadi Al-Sayed
Browse files
Options
Downloads
Patches
Plain Diff
handleCountryGet, setup routing and URL query parameter handling
parent
0be8fbb1
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
.gitignore
+1
-1
1 addition, 1 deletion
.gitignore
api_infoservice.go
+37
-9
37 additions, 9 deletions
api_infoservice.go
cmd/covidcase_server.go
+1
-1
1 addition, 1 deletion
cmd/covidcase_server.go
with
39 additions
and
11 deletions
.gitignore
+
1
−
1
View file @
67e9e4cd
.DS_Store
.idea/*
.vscode/*
cmd/
exchang
e_server
cmd/
covidcas
e_server
This diff is collapsed.
Click to expand it.
api_infoservice.go
+
37
−
9
View file @
67e9e4cd
...
...
@@ -14,6 +14,7 @@ import (
"time"
)
const
DATELEN
=
21
var
appStart
time
.
Time
// Diagnose struct for JSON encoding
...
...
@@ -66,12 +67,12 @@ func HandlerLostUser(w http.ResponseWriter, r *http.Request) {
}
}
// Handler
Histo
ry main handler for route related to `/
exchangehisto
ry` requests
// Handler
Count
ry main handler for route related to `/
count
ry` requests
func
HandlerCountry
()
func
(
http
.
ResponseWriter
,
*
http
.
Request
)
{
return
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
switch
r
.
Method
{
case
http
.
MethodGet
:
handle
Histo
ryGet
(
w
,
r
)
handle
Count
ryGet
(
w
,
r
)
case
http
.
MethodPost
:
http
.
Error
(
w
,
"Not implemented"
,
http
.
StatusNotImplemented
)
case
http
.
MethodPut
:
...
...
@@ -116,20 +117,34 @@ func HandlerDiag(t time.Time) func(http.ResponseWriter, *http.Request) {
}
// handle
Histo
ryGet utility function, package level, to handle GET request to
histo
ry route
func
handle
Histo
ryGet
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
// handle
Count
ryGet utility function, package level, to handle GET request to
count
ry route
func
handle
Count
ryGet
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
// Set response to be of JSON type
http
.
Header
.
Add
(
w
.
Header
(),
"content-type"
,
"application/json"
)
parts
:=
strings
.
Split
(
r
.
URL
.
Path
,
"/"
)
// error handling
if
len
(
parts
)
!=
6
||
parts
[
3
]
!=
"
exchangehisto
ry"
{
if
len
(
parts
)
!=
5
||
parts
[
3
]
!=
"
count
ry"
{
http
.
Error
(
w
,
"Malformed URL"
,
http
.
StatusBadRequest
)
return
}
// extract URL parameters
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"
)
// Handling case sensitivity of country name (lowercase all letters then capitalize first letter)
countryName
=
strings
.
ToLower
(
countryName
)
// All letters lower case
countryName
=
strings
.
Title
(
countryName
)
// First letter capitalized
// Extract optional 'scope' parameter
scope
:=
r
.
URL
.
Query
()
.
Get
(
"scope"
)
// Extract start and end date from scope
sDate
,
eDate
:=
split
(
scope
,
"-"
,
3
)
fmt
.
Println
(
sDate
)
fmt
.
Println
(
eDate
)
fmt
.
Println
(
countryName
)
/*
// Request currency code for country
currencyCode, err := country.GetCurrency(countryName)
if err != nil { // Error handling bad request parameter for countryName
...
...
@@ -147,9 +162,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())
}
*/
// Send result for processing
resWithData
(
w
,
result
)
//
resWithData(w, result)
}
// handleBorderGet utility function, package level, to handle GET request to border route
...
...
@@ -249,6 +264,19 @@ func p(r *http.Request, key string) string {
return
chi
.
URLParam
(
r
,
key
)
}
// split uses strings.Split and strings.Join to separate a string on the Nth occurrence of a character and returns
// both parts of the string
func
split
(
s
,
sep
string
,
pos
int
)
(
string
,
string
)
{
// Check if date entered has a valid length of 21 chars
if
len
(
s
)
!=
DATELEN
{
return
""
,
""
// Empty return
}
else
{
str
:=
strings
.
Split
(
s
,
sep
)
// Join seperated parts using sep character and return both split ends of the string
return
strings
.
Join
(
str
[
:
pos
],
sep
),
strings
.
Join
(
str
[
pos
:
],
sep
)
}
}
// getLimit converts string number into int and returns int, for limiting option
func
getLimit
(
s
string
)
int
{
// convert string number to an int and handle error for non digit characters
...
...
This diff is collapsed.
Click to expand it.
cmd/
exchang
e_server.go
→
cmd/
covidcas
e_server.go
+
1
−
1
View file @
67e9e4cd
...
...
@@ -15,7 +15,7 @@ import (
*/
const
(
// Chi regex parameters
COUNTRY
=
"{country_name:[a-z]+}"
// Country name
COUNTRY
=
"{country_name:[
A-Z
a-z]+}"
// Country name
//BY = "{b_year:\\d\\d\\d\\d}" // Begin year
//BM = "{b_month:\\d\\d}" // Begin month
//BD = "{b_day:\\d\\d}" // Begin day
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment