Skip to content

Commit 6c198a2

Browse files
committed
Rename the API key
Underscores need special treatment in Nginx, not worth the effort to use.
1 parent 5394ad5 commit 6c198a2

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

README.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Start the system and wait for it to update *(or create a [service](#install-as-a
6161

6262
```Shell
6363
cd /var/www/ip-location-api
64-
./ip-location-api
64+
sudo ./ip-location-api
6565
```
6666

6767
## Configuration
@@ -83,7 +83,7 @@ UPDATE_TIME=01:30
8383

8484
If you wish to expose the system without a reverse proxy, you may wish to update `SERVER_HOST` to `0.0.0.0`.
8585

86-
`API_KEY` allows a very basic protection of the system to be applied, a header named `API_KEY` with a matching value must be passed if this variable is populated. If left blank, the API is open.
86+
`API_KEY` allows a very basic protection of the system to be applied, a header named `API-KEY` *(hyphen not underscore!)* with a matching value must be passed if this variable is populated. If left blank, the API is open.
8787

8888
`COUNTRY`, `CITY` and `ASN` are the databases that will be loaded. **If you don't need cities or ASNs, just leave them blank.** The values / names used should mirror the directory values found in the [ip-location-db](https://github.com/sapics/ip-location-db) project:
8989

@@ -119,6 +119,8 @@ The MMDB adaption doesn't need any initialisation, it just needs to be told to u
119119
DB_TYPE=mmdb
120120
```
121121

122+
**N.B. If you choose MMDB, you will need enough RAM to read the entirety of each file into memory *(briefly)* before it is saved to disk. MaxMind don't have a partial method to write their files.** For city files, this can be quite a lot *(more than my favourite AWS `t4g.small` have spare if there's other programs running!)*.
123+
122124
### PostgreSQL
123125

124126
The PostgreSQL adaption will create 3 tables *(`ip_country`, `ip_asn` and `ip_city`)* in a database of your choice:
@@ -214,15 +216,16 @@ server {
214216
include ssl.d/yoursite.com.conf;
215217
216218
location / {
217-
proxy_http_version 1.1;
218-
proxy_cache_bypass $http_upgrade;
219-
proxy_set_header Upgrade $http_upgrade;
220-
proxy_set_header Connection 'upgrade';
221-
proxy_set_header Host $host;
222-
proxy_set_header X-Real-IP $remote_addr;
223-
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
224-
proxy_set_header X-Forwarded-Proto $scheme;
225-
proxy_pass http://127.0.0.1:8081/;
219+
proxy_http_version 1.1;
220+
proxy_cache_bypass $http_upgrade;
221+
proxy_set_header Upgrade $http_upgrade;
222+
proxy_set_header Connection 'upgrade';
223+
proxy_set_header Host $host;
224+
proxy_set_header X-Real-IP $remote_addr;
225+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
226+
proxy_set_header X-Forwarded-Proto $scheme;
227+
proxy_pass_request_headers on;
228+
proxy_pass http://127.0.0.1:8081/;
226229
}
227230
}
228231
```

helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ func randomNumber(min, max int) int {
318318

319319
func validApiKey(request *http.Request, enforceKey bool) bool {
320320
if len(os.Getenv("API_KEY")) > 0 || enforceKey {
321-
if len(os.Getenv("API_KEY")) == 0 || request.Header.Get("API_KEY") != os.Getenv("API_KEY") {
321+
if len(os.Getenv("API_KEY")) == 0 || request.Header.Get("API-KEY") != os.Getenv("API_KEY") {
322322
return false
323323
}
324324
}

load.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@ func loadCheckInitialised() (bool, []string) {
1212
var missing []string
1313

1414
if hasCountryDatabase() {
15-
initialised = dbInitialised("COUNTRY")
16-
if !initialised {
17-
missing = append(missing, "COUNTRY")
15+
if !dbInitialised("COUNTRY") {
16+
missing = append(missing, "COUNTRY")
17+
initialised = false
1818
}
1919
}
2020

21-
if initialised && hasASNDatabase() {
22-
initialised = dbInitialised("ASN")
23-
if !initialised {
24-
missing = append(missing, "ASN")
21+
if hasASNDatabase() {
22+
if !dbInitialised("ASN") {
23+
missing = append(missing, "ASN")
24+
initialised = false
2525
}
2626
}
2727

28-
if initialised && hasCityDatabase() {
29-
initialised = dbInitialised("CITY")
30-
if !initialised {
31-
missing = append(missing, "CITY")
28+
if hasCityDatabase() {
29+
if !dbInitialised("CITY") {
30+
missing = append(missing, "CITY")
31+
initialised = false
3232
}
3333
}
3434

0 commit comments

Comments
 (0)