From 50ff5836725787f53eb6437c5042506bcd11688c Mon Sep 17 00:00:00 2001 From: Guanran Wang Date: Fri, 25 Oct 2024 17:05:24 +0800 Subject: [PATCH] chore: add dev mode --- .envrc | 7 +++++++ main.go | 14 ++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/.envrc b/.envrc index 3550a30..4ef3088 100644 --- a/.envrc +++ b/.envrc @@ -1 +1,8 @@ use flake + +nix build nixpkgs#dbip-city-lite --no-link +nix build nixpkgs#dbip-asn-lite --no-link + +export IP_CHECKER_CITY_DB=$(nix eval nixpkgs#dbip-city-lite.mmdb --raw) +export IP_CHECKER_ASN_DB=$(nix eval nixpkgs#dbip-asn-lite.mmdb --raw) +export IP_CHECKER_MODE=dev diff --git a/main.go b/main.go index efcbc3a..b69a1f0 100644 --- a/main.go +++ b/main.go @@ -22,16 +22,18 @@ var ( ) type ServerConfig struct { - Listen string - CityDB string ASNDB string + CityDB string + Listen string + Mode string } func main() { config = ServerConfig{ - Listen: getEnvOr("IP_CHECKER_LISTEN", ":8080"), - CityDB: os.Getenv("IP_CHECKER_CITY_DB"), ASNDB: os.Getenv("IP_CHECKER_ASN_DB"), + CityDB: os.Getenv("IP_CHECKER_CITY_DB"), + Listen: getEnvOr("IP_CHECKER_LISTEN", ":8080"), + Mode: os.Getenv("IP_CHECKER_MODE"), } if config.CityDB != "" { @@ -135,6 +137,10 @@ func getIPCountry(ip string) string { func handleRequest(w http.ResponseWriter, r *http.Request) { sourceIP := func() string { + if config.Mode == "dev" { + return "223.5.5.5" + } + if ip := r.Header.Get("X-Forwarded-For"); ip != "" { return strings.Split(ip, ",")[0] }