Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
cloud-assignment-02
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
Nils Petter Skålerud
cloud-assignment-02
Commits
e6570cc7
Commit
e6570cc7
authored
Apr 21, 2023
by
Marius Nersveen
Browse files
Options
Downloads
Patches
Plain Diff
Converting CSV to JSON and then printing it to the web page
parent
932307d5
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
cmd/main.go
+1
-1
1 addition, 1 deletion
cmd/main.go
handlers/currentPercentageHandler.go
+46
-2
46 additions, 2 deletions
handlers/currentPercentageHandler.go
utils/structs.go
+7
-0
7 additions, 0 deletions
utils/structs.go
with
54 additions
and
3 deletions
cmd/main.go
+
1
−
1
View file @
e6570cc7
...
...
@@ -12,7 +12,7 @@ func main() {
serverState
:=
utils
.
ServerState
{}
// Extract PORT variable from the environment variables
// Extract PORT variable from the environment variables
0
port
:=
os
.
Getenv
(
"PORT"
)
// Override port with default port if not provided (e.g. local deployment)
...
...
This diff is collapsed.
Click to expand it.
handlers/currentPercentageHandler.go
+
46
−
2
View file @
e6570cc7
...
...
@@ -3,12 +3,42 @@ package handlers
import
(
"Assignment02/utils"
"encoding/csv"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strconv"
//"strings"
)
func
createRenewableEnergyDataset
(
data
[][]
string
)
[]
utils
.
StructTest
{
// convert csv lines to array of structs
var
RenewableEnergyDataset
[]
utils
.
StructTest
for
i
,
line
:=
range
data
{
if
i
>
0
{
// omit header line
var
rec
utils
.
StructTest
var
err
error
for
j
,
field
:=
range
line
{
if
j
==
0
{
rec
.
Entity
=
field
}
else
if
j
==
1
{
rec
.
Code
=
field
}
else
if
j
==
2
{
rec
.
Year
,
err
=
strconv
.
Atoi
(
field
)
}
else
if
j
==
3
{
rec
.
Renewables
,
err
=
strconv
.
ParseFloat
(
field
,
64
)
if
err
!=
nil
{
continue
}
}
}
RenewableEnergyDataset
=
append
(
RenewableEnergyDataset
,
rec
)
}
}
return
RenewableEnergyDataset
}
// Dedicated handler for GET requests
func
HandleGetRequestForCurrentPercentage
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
...
...
@@ -29,10 +59,24 @@ func HandleGetRequestForCurrentPercentage(w http.ResponseWriter, r *http.Request
// Read the CSV file
fileReader
:=
csv
.
NewReader
(
fd
)
records
,
err
:=
fileReader
.
ReadAll
()
data
,
err
:=
fileReader
.
ReadAll
()
if
err
!=
nil
{
fmt
.
Println
(
"Error reading CSV file."
)
fmt
.
Println
(
err
)
}
fmt
.
Println
(
records
)
//fmt.Println(data) // Print the CSV data to the console
//fmt.Fprintf(w, "%v", data) // Print the CSV data to the browser
// Assign successive lines of raw CSV data to fields of the created structs
RenewableEnergyDataset
:=
createRenewableEnergyDataset
(
data
)
// Convert an array of structs to JSON using marshaling functions from the encoding/json package
jsonData
,
err
:=
json
.
MarshalIndent
(
RenewableEnergyDataset
,
""
,
" "
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
//fmt.Println(string(jsonData)) // Print the JSON data to the console
fmt
.
Fprintf
(
w
,
"%v"
,
string
(
jsonData
))
// Print the JSON data to the browser
}
This diff is collapsed.
Click to expand it.
utils/structs.go
+
7
−
0
View file @
e6570cc7
...
...
@@ -48,3 +48,10 @@ type WebhookRegistration struct {
Url
string
`json:"url"`
Event
string
`json:"event"`
}
type
StructTest
struct
{
Entity
string
`json:"Entity"`
Code
string
`json:"Code"`
Year
int
`json:"Year"`
Renewables
float64
`json:"Renewables (% equivalent primary energy)"`
}
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