Move SnApiToken check into sn.go

This commit is contained in:
ekzyis 2023-04-16 19:43:48 +02:00
parent e69128b822
commit 2f7214e9a0
2 changed files with 19 additions and 23 deletions

23
main.go
View File

@ -1,28 +1,5 @@
package main
import (
"log"
"github.com/joho/godotenv"
"github.com/namsral/flag"
)
var (
SnApiToken string
)
func init() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
flag.StringVar(&SnApiToken, "NEXT_AUTH_CSRF_TOKEN", "", "Token required for authorizing requests to stacker.news/api/graphql")
flag.Parse()
if SnApiToken == "" {
log.Fatal("NEXT_AUTH_CSRF_TOKEN not set")
}
}
func main() {
stories := fetchTopStoriesFromHN()
filtered := filterByRelevanceForSN(&stories)

19
sn.go
View File

@ -6,6 +6,9 @@ import (
"fmt"
"log"
"net/http"
"github.com/joho/godotenv"
"github.com/namsral/flag"
)
type GraphQLPayload struct {
@ -25,6 +28,22 @@ type DupesResponse struct {
} `json:"data"`
}
var (
SnApiToken string
)
func init() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
flag.StringVar(&SnApiToken, "NEXT_AUTH_CSRF_TOKEN", "", "Token required for authorizing requests to stacker.news/api/graphql")
flag.Parse()
if SnApiToken == "" {
log.Fatal("NEXT_AUTH_CSRF_TOKEN not set")
}
}
func makeGraphQLRequest(body GraphQLPayload) *http.Response {
bodyJSON, err := json.Marshal(body)
if err != nil {