Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
assignment1
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
Mathilde Hertaas
assignment1
Commits
b237e7e1
Commit
b237e7e1
authored
4 months ago
by
Mathilde Hertaas
Browse files
Options
Downloads
Patches
Plain Diff
working on status endpoint, statuscode and uptime are now working
parent
68690d25
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
main.go
+61
-1
61 additions, 1 deletion
main.go
with
61 additions
and
1 deletion
main.go
+
61
−
1
View file @
b237e7e1
...
@@ -4,9 +4,59 @@ import (
...
@@ -4,9 +4,59 @@ import (
"encoding/json"
"encoding/json"
"fmt"
"fmt"
"net/http"
"net/http"
"time"
"strings"
"strings"
)
)
var
serviceStartTime
time
.
Time
// Funksjon for å hente statuskode fra et API
func
getAPIStatus
(
url
string
)
(
int
,
error
)
{
resp
,
err
:=
http
.
Get
(
url
)
if
err
!=
nil
{
return
0
,
err
}
defer
resp
.
Body
.
Close
()
// Returner HTTP-statuskoden
return
resp
.
StatusCode
,
nil
}
// Funksjon for å beregne oppetid
func
getUptime
()
int64
{
return
int64
(
time
.
Since
(
serviceStartTime
)
.
Seconds
())
}
// Handler for Diagnostics endpoint
func
diagnosticsHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
// Hent statuskoder fra eksterne API-er
countriesNowStatus
,
err
:=
getAPIStatus
(
"http://129.241.150.113:3500/api/v0.1/countries"
)
if
err
!=
nil
{
http
.
Error
(
w
,
"Error fetching CountriesNow status"
,
http
.
StatusInternalServerError
)
return
}
restCountriesStatus
,
err
:=
getAPIStatus
(
"http://129.241.150.113:8080/v3.1/all"
)
//funker med all
if
err
!=
nil
{
http
.
Error
(
w
,
"Error fetching RestCountries status"
,
http
.
StatusInternalServerError
)
return
}
// Bygg responsen med statusinformasjon
response
:=
map
[
string
]
interface
{}{
"countriesnowapi"
:
fmt
.
Sprintf
(
"%d"
,
countriesNowStatus
),
"restcountriesapi"
:
fmt
.
Sprintf
(
"%d"
,
restCountriesStatus
),
"version"
:
"v1"
,
// Versjon av API
"uptime"
:
getUptime
(),
// Oppetid i sekunder
}
// Sett Content-Type til application/json
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
// Returner JSON-responsen
json
.
NewEncoder
(
w
)
.
Encode
(
response
)
}
type
Flags
struct
{
type
Flags
struct
{
PNG
string
`json:"png"`
PNG
string
`json:"png"`
}
}
...
@@ -110,6 +160,12 @@ func countryInfoHandler(w http.ResponseWriter, r *http.Request) {
...
@@ -110,6 +160,12 @@ func countryInfoHandler(w http.ResponseWriter, r *http.Request) {
}
}
func
main
()
{
func
main
()
{
//starttidspunkt for tjenesten
serviceStartTime
=
time
.
Now
()
http
.
HandleFunc
(
"/status/"
,
diagnosticsHandler
)
http
.
HandleFunc
(
"/"
,
homeHandler
)
http
.
HandleFunc
(
"/"
,
homeHandler
)
http
.
HandleFunc
(
"/countryinfo/v1/info/"
,
countryInfoHandler
)
http
.
HandleFunc
(
"/countryinfo/v1/info/"
,
countryInfoHandler
)
...
@@ -126,9 +182,13 @@ func homeHandler(w http.ResponseWriter, r *http.Request) {
...
@@ -126,9 +182,13 @@ func homeHandler(w http.ResponseWriter, r *http.Request) {
"endpoints"
:
map
[
string
]
interface
{}{
"endpoints"
:
map
[
string
]
interface
{}{
"Get Country Info"
:
map
[
string
]
string
{
"Get Country Info"
:
map
[
string
]
string
{
"Description"
:
"Get country details including cities, population, and more."
,
"Description"
:
"Get country details including cities, population, and more."
,
"URL"
:
"http://localhost:8080/countryinfo/v1/info/{country_code}"
,
"URL
Format
"
:
"http://localhost:8080/countryinfo/v1/info/{country_code}"
,
"Example"
:
"http://localhost:8080/countryinfo/v1/info/NO"
,
"Example"
:
"http://localhost:8080/countryinfo/v1/info/NO"
,
},
},
"Get status info"
:
map
[
string
]
string
{
"Description"
:
"Get information about the API statuses"
,
"URL"
:
"http://localhost:8080/status/"
,
},
},
},
}
}
...
...
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