Skip to content

Commit 965ca32

Browse files
authored
PR #1 basic version of query heuristics
Ipmlement basic version of query heuristics and a generated nginx config.
2 parents c967c2b + 5592006 commit 965ca32

File tree

5 files changed

+133
-1
lines changed

5 files changed

+133
-1
lines changed

cmd/sea/main.go

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,85 @@
11
package main
22

3+
import (
4+
"bytes"
5+
"embed"
6+
"flag"
7+
"fmt"
8+
"os"
9+
"strings"
10+
"text/template"
11+
12+
"github.com/pelletier/go-toml/v2"
13+
)
14+
15+
// Config represents the TOML configuration structure.
16+
type Config struct {
17+
Listen int `toml:"listen"`
18+
ServerName string `toml:"server_name"`
19+
CustomKeywords []KeywordRule `toml:"custom_keywords"`
20+
}
21+
22+
// KeywordRule maps a phrase to a destination.
23+
type KeywordRule struct {
24+
Phrase string `toml:"phrase"`
25+
Dest string `toml:"dest"`
26+
}
27+
28+
//go:embed nginx.conf.tmpl
29+
var templateFS embed.FS
30+
331
func main() {
4-
println("Hello, world!")
32+
var cfgPath string
33+
flag.StringVar(&cfgPath, "config", "config.toml", "path to TOML configuration")
34+
flag.Parse()
35+
36+
cfg, err := loadConfig(cfgPath)
37+
if err != nil {
38+
fmt.Fprintf(os.Stderr, "error loading config: %v\n", err)
39+
os.Exit(1)
40+
}
41+
42+
out, err := generateNginx(cfg)
43+
if err != nil {
44+
fmt.Fprintf(os.Stderr, "error generating nginx config: %v\n", err)
45+
os.Exit(1)
46+
}
47+
48+
fmt.Print(out)
49+
}
50+
51+
func loadConfig(path string) (Config, error) {
52+
cfg := Config{Listen: 80, ServerName: "search.local"}
53+
data, err := os.ReadFile(path)
54+
if err != nil {
55+
if os.IsNotExist(err) {
56+
return cfg, nil
57+
}
58+
return cfg, err
59+
}
60+
if err := toml.Unmarshal(data, &cfg); err != nil {
61+
return cfg, err
62+
}
63+
return cfg, nil
64+
}
65+
66+
// generateNginx assembles the nginx configuration using heuristics
67+
// and any custom keyword rules from the configuration file.
68+
func generateNginx(cfg Config) (string, error) {
69+
tmpl, err := template.New("nginx.conf.tmpl").Funcs(template.FuncMap{
70+
"escape": escapeSpace,
71+
}).ParseFS(templateFS, "nginx.conf.tmpl")
72+
if err != nil {
73+
return "", err
74+
}
75+
76+
var b bytes.Buffer
77+
if err := tmpl.Execute(&b, cfg); err != nil {
78+
return "", err
79+
}
80+
return b.String(), nil
81+
}
82+
83+
func escapeSpace(s string) string {
84+
return strings.ReplaceAll(s, " ", "\\ ")
585
}

cmd/sea/nginx.conf.tmpl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
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;
11+
{{- range .CustomKeywords }}
12+
~*(?i)^{{ escape .Phrase }}$ {{ .Dest }};
13+
{{- end }}
14+
}
15+
16+
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";
21+
}
22+
23+
server {
24+
listen {{ .Listen }};
25+
server_name {{ .ServerName }};
26+
27+
location / {
28+
return 302 $target;
29+
}
30+
}

config.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
listen = 80
2+
server_name = "search.local"
3+
4+
[[custom_keywords]]
5+
phrase = "nginx"
6+
dest = "wikipedia"
7+
8+
[[custom_keywords]]
9+
phrase = "New York"
10+
dest = "wikipedia"
11+
12+
[[custom_keywords]]
13+
phrase = "openai"
14+
dest = "wikipedia"
15+
16+
[[custom_keywords]]
17+
phrase = "cat pictures"
18+
dest = "google_images"

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/dense-analysis/sea
22

33
go 1.22.2
4+
5+
require github.com/pelletier/go-toml/v2 v2.2.4

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
2+
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=

0 commit comments

Comments
 (0)