Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
O
oblig1
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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Steffen Sæther
oblig1
Commits
63ffd98a
Commit
63ffd98a
authored
1 year ago
by
Steffen Sæther
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
74d98a9f
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
oblig1/endpoints/status.go
+55
-0
55 additions, 0 deletions
oblig1/endpoints/status.go
with
55 additions
and
0 deletions
oblig1/endpoints/status.go
0 → 100644
+
55
−
0
View file @
63ffd98a
package
endpoints
import
(
"encoding/json"
"net/http"
"time"
)
// Global variable to store the service start time.
var
startTime
=
time
.
Now
()
// DiagnosticResponse represents the structure of diagnostics response.
type
DiagnosticResponse
struct
{
GutendexAPIStatus
int
`json:"gutendexapi"`
LanguageAPIStatus
int
`json:"languageapi"`
CountriesAPIStatus
int
`json:"countriesapi"`
Version
string
`json:"version"`
Uptime
float64
`json:"uptime"`
}
// checkAPIStatus checks the status of an API by sending a GET request and returning the HTTP status code.
func
checkAPIStatus
(
apiURL
string
)
int
{
response
,
err
:=
http
.
Get
(
apiURL
)
if
err
!=
nil
{
return
http
.
StatusInternalServerError
}
defer
response
.
Body
.
Close
()
return
response
.
StatusCode
}
// StatusHandler handles requests to the /librarystats/v1/status/ endpoint.
func
StatusHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
// Check the status of Gutendex, Language2Countries, and REST Countries APIs.
gutendexStatus
:=
checkAPIStatus
(
"http://129.241.150.113:8000/"
)
language2CountriesStatus
:=
checkAPIStatus
(
"http://129.241.150.113:3000/"
)
restCountriesStatus
:=
checkAPIStatus
(
"http://129.241.150.113:8080/v3.1/alpha/"
)
// Example response for testing purposes.
response
:=
DiagnosticResponse
{
GutendexAPIStatus
:
gutendexStatus
,
LanguageAPIStatus
:
language2CountriesStatus
,
CountriesAPIStatus
:
restCountriesStatus
,
Version
:
"v1"
,
Uptime
:
time
.
Since
(
startTime
)
.
Seconds
(),
}
sendJSONResponse
(
w
,
response
)
}
// sendJSONResponse sends a JSON response with the provided data.
func
sendJSONResponse
(
w
http
.
ResponseWriter
,
data
interface
{})
{
w
.
WriteHeader
(
http
.
StatusOK
)
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
json
.
NewEncoder
(
w
)
.
Encode
(
data
)
}
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