mirror of
https://github.com/trafficlunar/api.git
synced 2026-06-27 22:24:08 +00:00
feat: github projects route
This commit is contained in:
parent
a1f22391e9
commit
a3eb6c6e32
7 changed files with 115 additions and 0 deletions
62
internal/service/github.go
Normal file
62
internal/service/github.go
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"api/internal/model"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetGitHubData() []model.GitHubData {
|
||||
data := []model.GitHubData{}
|
||||
|
||||
projects := strings.Split(os.Getenv("GITHUB_PROJECTS"), ",")
|
||||
client := &http.Client{}
|
||||
|
||||
// Go through every project specified in GITHUB_PROJECTS
|
||||
for _, project := range projects {
|
||||
url := fmt.Sprintf("https://api.github.com/repos/%s", project)
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
slog.Error("Error creating request", slog.Any("error", err))
|
||||
continue
|
||||
}
|
||||
|
||||
// Add authorization header
|
||||
req.Header.Set("Authorization", "token "+os.Getenv("GITHUB_TOKEN"))
|
||||
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
slog.Error("Error requesting GitHub API", slog.Any("error", err))
|
||||
continue
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
slog.Error("Error reading body", slog.Any("error", err))
|
||||
continue
|
||||
}
|
||||
|
||||
var apiResponse model.GitHubAPI
|
||||
err = json.Unmarshal(body, &apiResponse)
|
||||
if err != nil {
|
||||
slog.Error("Error unmarshalling JSON", slog.Any("error", err))
|
||||
continue
|
||||
}
|
||||
|
||||
data = append(data, model.GitHubData{
|
||||
Name: project,
|
||||
Description: apiResponse.Description,
|
||||
Stars: fmt.Sprint(apiResponse.Stars),
|
||||
Language: apiResponse.Language,
|
||||
Url: apiResponse.Url,
|
||||
})
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue