chore: add dev mode

This commit is contained in:
Guanran Wang 2024-10-25 17:05:24 +08:00
parent b543d0c1f7
commit 50ff583672
Signed by: nyancat
GPG key ID: 91F97D9ED12639CF
2 changed files with 17 additions and 4 deletions

7
.envrc
View file

@ -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

14
main.go
View file

@ -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]
}