Skip to content

Commit

Permalink
serve both json and file
Browse files Browse the repository at this point in the history
  • Loading branch information
dulvui committed Mar 27, 2024
1 parent 4b591f7 commit 868a132
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,50 @@ func main() {

r.Use(gin.Recovery())

r.GET("/", artifact)
r.GET("/", artifactFile)
r.GET("/json", artifactJson)
r.GET("/health", health)
r.Run()
}
func health(c *gin.Context) {
c.Status(http.StatusOK)
}
func artifact(c *gin.Context) {
func artifactJson(c *gin.Context) {
req, err := http.NewRequest(http.MethodGet, "https://gitlab.com/api/v4/projects/54944672/jobs/artifacts/main/raw/report.json?job=harvest", nil)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
}

req.Header = http.Header{
"PRIVATE-TOKEN": {token},
"Accept": {"application/json"},
}

res, err := http.DefaultClient.Do(req)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
c.AbortWithError(http.StatusInternalServerError, err)
}

bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
}

var data map[string]interface{}
json.Unmarshal(bodyBytes, &data)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
}

c.JSON(http.StatusOK, data)
}

func artifactFile(c *gin.Context) {
req, err := http.NewRequest(http.MethodGet, "https://gitlab.com/api/v4/projects/54944672/jobs/artifacts/main/raw/report.json?job=harvest", nil)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
Expand Down

0 comments on commit 868a132

Please sign in to comment.