Skip to content

Commit 0533988

Browse files
authored
Merge pull request #3 from dense-analysis/codex/extend-sea-go-generator-nginx-map-for-backend-routing
Enhance nginx map routing
2 parents 965ca32 + 022a7b7 commit 0533988

File tree

8 files changed

+116
-15
lines changed

8 files changed

+116
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*.dll
88
*.so
99
*.dylib
10+
/sea
1011

1112
# Test binary, built with `go test -c`
1213
*.test

AGENTS.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# AGENTS.md
2+
3+
The purpose of this project is to produce an nginx configuration for redirecting
4+
to search providers based on the phrases used.
5+
6+
You can run the main executable like so:
7+
8+
```
9+
go run ./cmd/sea/main.go
10+
```
11+
12+
`./cmd/sea/nginx.conf.tmpl` is the nginx template, and you should keep
13+
`nginx.conf` committed to the repository so people don't have to run the program
14+
to get it. Commit `config.toml` as a default configuration people can override
15+
if they so choose.
16+
17+
You may validate that the nginx configuration works with the docker compose file
18+
like so:
19+
20+
```
21+
# Leave an nginx docker image running on port 57321
22+
docker compose up
23+
# Check a redirect. The hostname comes from `server_name` in `config.toml`
24+
curl -s -D - http://search.localhost:57321/?q=how+can+i -o /dev/null | grep -i '^Location:'
25+
```

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ applied to `nginx`.
1515
- **Simple Configuration**: Ships as a ready-to-use NGINX `.conf` file.
1616
- **Lightweight & Fast**: Minimal dependencies for high throughput. Very simple
1717
security profile, as there is no web app to exploit.
18+
- **Optimized Matching**: Regex rules are evaluated once per request using
19+
NGINX's `map` directive. Enable `pcre_jit` for the best performance.
1820

1921
## Development Requirements
2022

cmd/sea/main.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ type Config struct {
1919
CustomKeywords []KeywordRule `toml:"custom_keywords"`
2020
}
2121

22+
// TemplateData combines the parsed configuration with the destination
23+
// to URL mappings used to construct the final nginx file.
24+
type TemplateData struct {
25+
Config
26+
Targets map[string]string
27+
}
28+
2229
// KeywordRule maps a phrase to a destination.
2330
type KeywordRule struct {
2431
Phrase string `toml:"phrase"`
@@ -73,8 +80,19 @@ func generateNginx(cfg Config) (string, error) {
7380
return "", err
7481
}
7582

83+
data := TemplateData{
84+
Config: cfg,
85+
Targets: map[string]string{
86+
"google": "https://www.google.com/search?q=$arg_q",
87+
"chatgpt": "https://chatgpt.com/?q=$arg_q",
88+
"wikipedia": "https://en.wikipedia.org/wiki/$arg_q",
89+
"google_images": "https://www.google.com/search?tbm=isch&q=$arg_q",
90+
"google_maps": "https://www.google.com/maps/search/?q=$arg_q",
91+
},
92+
}
93+
7694
var b bytes.Buffer
77-
if err := tmpl.Execute(&b, cfg); err != nil {
95+
if err := tmpl.Execute(&b, data); err != nil {
7896
return "", err
7997
}
8098
return b.String(), nil

cmd/sea/nginx.conf.tmpl

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1+
# Map routes search queries to the appropriate backend.
2+
# Regex is evaluated once per request; enable `pcre_jit` for best speed.
13
map $arg_q $dest {
2-
default google;
3-
~*(?i)^\s*how\s+to\b chatgpt;
4-
~*(?i)^\s*what\s+is\b chatgpt;
5-
~*(?i)^\s*(when|where|why)\b chatgpt;
6-
~*(?i)^\s*who\s+(?:is|was)\b wikipedia;
7-
~*(?i)\bpictures?\s+of\b google_images;
8-
~*(?i)\bvs\b google;
9-
~*(?i)\bdownload\b google;
10-
~*(?i)\bwikipedia\b|\bwiki\b wikipedia;
4+
# direct image search
5+
~*(?i)\bpictures?\s+of\b google_images;
6+
# map and direction queries
7+
~*(?i)\b(near\s+me|directions?\s+to|map\s+of)\b google_maps;
8+
# Wikipedia lookups
9+
~*(?i)\b(?:biography|history|life\s+of)\b wikipedia;
10+
# question detection
11+
~*(?i)\b(who|what|when|where|why|how|can|could|would|should|do|did|is|are|was|were|am|will|whom|whose|which)\b chatgpt;
12+
# instructional queries
13+
~*(?i)\b(explain|describe|compare|define)\b chatgpt;
14+
# explicit wiki keywords
15+
~*(?i)\bwikipedia\b|\bwiki\b wikipedia;
1116
{{- range .CustomKeywords }}
1217
~*(?i)^{{ escape .Phrase }}$ {{ .Dest }};
1318
{{- end }}
19+
default google;
1420
}
1521

22+
# Map engine names to full URL targets. Update this list in Go.
1623
map $dest $target {
17-
google "https://www.google.com/search?q=$arg_q";
18-
google_images "https://www.google.com/search?tbm=isch&q=$arg_q";
19-
chatgpt "https://chat.openai.com/?q=$arg_q";
20-
wikipedia "https://en.wikipedia.org/wiki/$arg_q";
24+
{{- range $name, $url := .Targets }}
25+
{{ $name }} {{ $url }};
26+
{{- end }}
2127
}
2228

2329
server {
@@ -28,3 +34,4 @@ server {
2834
return 302 $target;
2935
}
3036
}
37+

compose.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
services:
3+
nginx:
4+
image: nginx:latest
5+
ports:
6+
- '57321:80'
7+
volumes:
8+
- ./nginx.conf:/etc/nginx/conf.d/default.conf:ro

config.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
listen = 80
2-
server_name = "search.local"
2+
server_name = "search.localhost"
33

44
[[custom_keywords]]
55
phrase = "nginx"

nginx.conf

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Map routes search queries to the appropriate backend.
2+
# Regex is evaluated once per request; enable `pcre_jit` for best speed.
3+
map $arg_q $dest {
4+
# direct image search
5+
~*(?i)\bpictures?\s+of\b google_images;
6+
# map and direction queries
7+
~*(?i)\b(near\s+me|directions?\s+to|map\s+of)\b google_maps;
8+
# Wikipedia lookups
9+
~*(?i)\b(?:biography|history|life\s+of)\b wikipedia;
10+
# question detection
11+
~*(?i)\b(who|what|when|where|why|how|can|could|would|should|do|did|is|are|was|were|am|will|whom|whose|which)\b chatgpt;
12+
# instructional queries
13+
~*(?i)\b(explain|describe|compare|define)\b chatgpt;
14+
# explicit wiki keywords
15+
~*(?i)\bwikipedia\b|\bwiki\b wikipedia;
16+
~*(?i)^nginx$ wikipedia;
17+
~*(?i)^New\ York$ wikipedia;
18+
~*(?i)^openai$ wikipedia;
19+
~*(?i)^cat\ pictures$ google_images;
20+
default google;
21+
}
22+
23+
# Map engine names to full URL targets. Update this list in Go.
24+
map $dest $target {
25+
chatgpt https://chatgpt.com/?q=$arg_q;
26+
google https://www.google.com/search?q=$arg_q;
27+
google_images https://www.google.com/search?tbm=isch&q=$arg_q;
28+
google_maps https://www.google.com/maps/search/?q=$arg_q;
29+
wikipedia https://en.wikipedia.org/wiki/$arg_q;
30+
}
31+
32+
server {
33+
listen 80;
34+
server_name search.localhost;
35+
36+
location / {
37+
return 302 $target;
38+
}
39+
}
40+

0 commit comments

Comments
 (0)