Use more vars

This commit is contained in:
ekzyis 2023-04-16 19:51:17 +02:00
parent 2f7214e9a0
commit 02a9e4e32f
2 changed files with 15 additions and 4 deletions

12
hn.go
View File

@ -20,10 +20,20 @@ type Story struct {
Url string Url string
} }
var (
HackerNewsUrl string
HackerNewsFirebaseUrl string
)
func init() {
HackerNewsUrl = "https://news.ycombinator.com"
HackerNewsFirebaseUrl = "https://hacker-news.firebaseio.com/v0"
}
func fetchTopStoriesFromHN() []Story { func fetchTopStoriesFromHN() []Story {
// API docs: https://github.com/HackerNews/API // API docs: https://github.com/HackerNews/API
url := "https://hacker-news.firebaseio.com/v0/topstories.json" url := fmt.Sprintf("%s/topstories.json", HackerNewsFirebaseUrl)
resp, err := http.Get(url) resp, err := http.Get(url)
if err != nil { if err != nil {
log.Fatal("Error fetching top stories:", err) log.Fatal("Error fetching top stories:", err)

7
sn.go
View File

@ -29,10 +29,12 @@ type DupesResponse struct {
} }
var ( var (
SnApiUrl string
SnApiToken string SnApiToken string
) )
func init() { func init() {
SnApiUrl = "https://stacker.news/api/graphql"
err := godotenv.Load() err := godotenv.Load()
if err != nil { if err != nil {
log.Fatal("Error loading .env file") log.Fatal("Error loading .env file")
@ -50,8 +52,7 @@ func makeGraphQLRequest(body GraphQLPayload) *http.Response {
log.Fatal("Error during json.Marshal:", err) log.Fatal("Error during json.Marshal:", err)
} }
url := "https://stacker.news/api/graphql" req, err := http.NewRequest("POST", SnApiUrl, bytes.NewBuffer(bodyJSON))
req, err := http.NewRequest("POST", url, bytes.NewBuffer(bodyJSON))
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -64,7 +65,7 @@ func makeGraphQLRequest(body GraphQLPayload) *http.Response {
panic(err) panic(err)
} }
log.Printf("POST %s %d\n", url, resp.StatusCode) log.Printf("POST %s %d\n", SnApiUrl, resp.StatusCode)
return resp return resp
} }