Skip to content

Commit 022a7b7

Browse files
committed
Correct errors from the agent
* Use .localhost TLD for local services instead * Commit the generated `nginx.conf` so it is easy to get * Add AGENTS.md to tell agents how to work on the project * Fix indent style in the Go source code * Use chatgpt.com which has replaced the old domain name * Add a `docker compose` configuration for testing the config
1 parent d9f2e59 commit 022a7b7

File tree

7 files changed

+108
-34
lines changed

7 files changed

+108
-34
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+
```

cmd/sea/main.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ import (
1414

1515
// Config represents the TOML configuration structure.
1616
type Config struct {
17-
Listen int `toml:"listen"`
18-
ServerName string `toml:"server_name"`
19-
CustomKeywords []KeywordRule `toml:"custom_keywords"`
17+
Listen int `toml:"listen"`
18+
ServerName string `toml:"server_name"`
19+
CustomKeywords []KeywordRule `toml:"custom_keywords"`
2020
}
2121

2222
// TemplateData combines the parsed configuration with the destination
2323
// to URL mappings used to construct the final nginx file.
2424
type TemplateData struct {
25-
Config
26-
Targets map[string]string
25+
Config
26+
Targets map[string]string
2727
}
2828

2929
// KeywordRule maps a phrase to a destination.
@@ -73,29 +73,29 @@ func loadConfig(path string) (Config, error) {
7373
// generateNginx assembles the nginx configuration using heuristics
7474
// and any custom keyword rules from the configuration file.
7575
func generateNginx(cfg Config) (string, error) {
76-
tmpl, err := template.New("nginx.conf.tmpl").Funcs(template.FuncMap{
77-
"escape": escapeSpace,
78-
}).ParseFS(templateFS, "nginx.conf.tmpl")
79-
if err != nil {
80-
return "", err
81-
}
76+
tmpl, err := template.New("nginx.conf.tmpl").Funcs(template.FuncMap{
77+
"escape": escapeSpace,
78+
}).ParseFS(templateFS, "nginx.conf.tmpl")
79+
if err != nil {
80+
return "", err
81+
}
8282

83-
data := TemplateData{
84-
Config: cfg,
85-
Targets: map[string]string{
86-
"google": "https://www.google.com/search?q=$arg_q",
87-
"chatgpt": "https://chat.openai.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-
}
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+
}
9393

94-
var b bytes.Buffer
95-
if err := tmpl.Execute(&b, data); err != nil {
96-
return "", err
97-
}
98-
return b.String(), nil
94+
var b bytes.Buffer
95+
if err := tmpl.Execute(&b, data); err != nil {
96+
return "", err
97+
}
98+
return b.String(), nil
9999
}
100100

101101
func escapeSpace(s string) string {

cmd/sea/nginx.conf.tmpl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
# Regex is evaluated once per request; enable `pcre_jit` for best speed.
33
map $arg_q $dest {
44
# direct image search
5-
~*(?i)\bpictures?\s+of\b google_images; # Google Images
5+
~*(?i)\bpictures?\s+of\b google_images;
66
# map and direction queries
7-
~*(?i)\b(near\s+me|directions?\s+to|map\s+of)\b google_maps; # map/directions
7+
~*(?i)\b(near\s+me|directions?\s+to|map\s+of)\b google_maps;
88
# Wikipedia lookups
9-
~*(?i)\b(?:biography|history|life\s+of)\b wikipedia; # Wikipedia lookup
9+
~*(?i)\b(?:biography|history|life\s+of)\b wikipedia;
1010
# 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; # 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;
1212
# instructional queries
13-
~*(?i)\b(explain|describe|compare|define)\b chatgpt; # instructional verbs
13+
~*(?i)\b(explain|describe|compare|define)\b chatgpt;
1414
# explicit wiki keywords
15-
~*(?i)\bwikipedia\b|\bwiki\b wikipedia; # Wikipedia keyword
15+
~*(?i)\bwikipedia\b|\bwiki\b wikipedia;
1616
{{- range .CustomKeywords }}
1717
~*(?i)^{{ escape .Phrase }}$ {{ .Dest }};
1818
{{- end }}
19-
default google; # default Google search
19+
default google;
2020
}
2121

2222
# Map engine names to full URL targets. Update this list in Go.

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)