2 Commits
test ... v1.0.2

Author SHA1 Message Date
3734b919b6 .gitea/workflows/registry.yml hinzugefügt
Some checks failed
release-tag / release-image (push) Failing after 25s
build-binaries / build (.exe, amd64, windows) (push) Successful in 51s
build-binaries / release (push) Successful in 13s
build-binaries / publish-agent (push) Successful in 9s
2025-11-06 07:50:11 +00:00
8dee673c41 main.go aktualisiert
All checks were successful
build-binaries / build (.exe, amd64, windows) (push) Has been skipped
build-binaries / release (push) Has been skipped
build-binaries / publish-agent (push) Has been skipped
2025-11-06 07:48:29 +00:00
2 changed files with 307 additions and 137 deletions

View File

@@ -0,0 +1,51 @@
name: release-tag
on:
push:
branches:
- 'main'
jobs:
release-image:
runs-on: ubuntu-fast
env:
DOCKER_ORG: ${{ vars.DOCKER_ORG }}
DOCKER_LATEST: latest
RUNNER_TOOL_CACHE: /toolcache
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker BuildX
uses: docker/setup-buildx-action@v2
with: # replace it with your local IP
config-inline: |
[registry."${{ vars.DOCKER_REGISTRY }}"]
http = true
insecure = true
- name: Login to DockerHub
uses: docker/login-action@v2
with:
registry: ${{ vars.DOCKER_REGISTRY }} # replace it with your local IP
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Get Meta
id: meta
run: |
echo REPO_NAME=$(echo ${GITHUB_REPOSITORY} | awk -F"/" '{print $2}') >> $GITHUB_OUTPUT
echo REPO_VERSION=$(git describe --tags --always | sed 's/^v//') >> $GITHUB_OUTPUT
- name: Build and push
uses: docker/build-push-action@v4
with:
context: .
file: ./Dockerfile
platforms: |
linux/amd64
push: true
tags: | # replace it with your local IP and tags
${{ vars.DOCKER_REGISTRY }}/${{ env.DOCKER_ORG }}/${{ steps.meta.outputs.REPO_NAME }}:${{ steps.meta.outputs.REPO_VERSION }}
${{ vars.DOCKER_REGISTRY }}/${{ env.DOCKER_ORG }}/${{ steps.meta.outputs.REPO_NAME }}:${{ env.DOCKER_LATEST }}

393
main.go
View File

@@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"flag"
"fmt"
"html/template"
"log"
"net/http"
@@ -17,14 +18,18 @@ import (
type record map[string]string
var (
addr = flag.String("addr", ":8080", "HTTP listen address")
csvPath = flag.String("csv", "data.csv", "path to CSV with header row")
keyCol = flag.String("key", "", "column name to search in (default: first column)")
valueCol = flag.String("value", "", "column name to return (default: second column)")
addr = flag.String("addr", ":8080", "HTTP listen address")
csvPath = flag.String("csv", "data.csv", "path to CSV with header row")
keyCol = flag.String("key", "", "column name to search in (default: first column)")
valueCol = flag.String("value", "", "column name to return (default: second column)")
suffixLen = flag.Int("suffix", 0, "if >0, also match by last N characters of the scanned code")
sep = flag.String("sep", ",", "CSV field separator (e.g. ',' or ';')")
records []record
headers []string
mu sync.RWMutex
records []record
headers []string
mu sync.RWMutex
exactMap map[string]record // canon(key) -> record
suffixMap map[string][]record // lastN(canon(key), N) -> records
)
func main() {
@@ -33,24 +38,22 @@ func main() {
if err := loadCSV(*csvPath); err != nil {
log.Fatalf("CSV laden fehlgeschlagen: %v", err)
}
log.Printf("CSV geladen: %s (%d Zeilen, %d Spalten)", *csvPath, len(records), len(headers))
log.Printf("CSV geladen: %s (%d Zeilen, %d Spalten) | key=%q value=%q sep=%q suffix=%d",
*csvPath, len(records), len(headers), *keyCol, *valueCol, *sep, *suffixLen)
http.HandleFunc("/", handleIndex)
http.HandleFunc("/search", handleSearch)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(staticDir()))))
fmt.Println(headers)
fmt.Println(records)
log.Printf("Starte Server auf %s …", *addr)
if err := http.ListenAndServe(*addr, nil); err != nil {
log.Fatal(err)
}
}
func staticDir() string {
dir := "static"
if _, err := os.Stat(dir); err == nil {
return dir
}
return "."
func errUnknownCol(kind, name string, headers []string) error {
return errors.New(kind + " column '" + name + "' nicht gefunden. Verfügbare Spalten: " + strings.Join(headers, ", "))
}
func loadCSV(path string) error {
@@ -61,6 +64,11 @@ func loadCSV(path string) error {
defer f.Close()
r := csv.NewReader(f)
if *sep != "" {
r.Comma = []rune(*sep)[0]
} else {
r.Comma = ','
}
r.FieldsPerRecord = -1
rows, err := r.ReadAll()
@@ -72,25 +80,27 @@ func loadCSV(path string) error {
}
headers = rows[0]
idxKey := 0
idxVal := 1
if len(headers) < 2 {
return errors.New("mindestens zwei Spalten benötigt")
}
// Spaltenindizes bestimmen (EqualFold)
idxKey := 0
idxVal := 1
if *keyCol != "" {
idxKey = indexOf(headers, *keyCol)
if idxKey == -1 {
if i := indexOf(headers, *keyCol); i >= 0 {
idxKey = i
} else {
return errUnknownCol("key", *keyCol, headers)
}
}
if *valueCol != "" {
idxVal = indexOf(headers, *valueCol)
if idxVal == -1 {
if i := indexOf(headers, *valueCol); i >= 0 {
idxVal = i
} else {
return errUnknownCol("value", *valueCol, headers)
}
}
if *keyCol == "" {
*keyCol = headers[idxKey]
}
@@ -98,6 +108,7 @@ func loadCSV(path string) error {
*valueCol = headers[idxVal]
}
// Zeilen einlesen + trimmen
var recs []record
for i := 1; i < len(rows); i++ {
row := rows[i]
@@ -108,23 +119,162 @@ func loadCSV(path string) error {
}
m := make(record, len(headers))
for c, h := range headers {
val := ""
if c < len(row) {
m[h] = row[c]
} else {
m[h] = ""
val = strings.TrimSpace(row[c])
}
m[h] = val
}
recs = append(recs, m)
}
// Indizes bauen
ex := make(map[string]record, len(recs))
var sx map[string][]record
if *suffixLen > 0 {
sx = make(map[string][]record, len(recs))
}
for _, rec := range recs {
k := rec[*keyCol]
if k == "" {
continue
}
ck := canon(k)
ex[ck] = rec
if *suffixLen > 0 {
suf := lastN(ck, *suffixLen)
if suf != "" {
sx[suf] = append(sx[suf], rec)
}
}
}
mu.Lock()
records = recs
exactMap = ex
suffixMap = sx
mu.Unlock()
// kleine Sichtprobe für Debug
log.Printf("Header: %v", headers)
if len(recs) > 0 {
log.Printf("Sample Key raw=%q canon=%q", recs[0][*keyCol], canon(recs[0][*keyCol]))
}
return nil
}
func errUnknownCol(kind, name string, headers []string) error {
return errors.New(kind + " column '" + name + "' nicht gefunden. Verfügbare Spalten: " + strings.Join(headers, ", "))
func handleIndex(w http.ResponseWriter, r *http.Request) {
tpl := template.Must(template.New("index").Parse(indexHTML))
data := struct {
File string
KeyCol string
ValCol string
Suffix int
HasSuf bool
}{
File: filepath.Base(*csvPath),
KeyCol: *keyCol,
ValCol: *valueCol,
Suffix: *suffixLen,
HasSuf: *suffixLen > 0,
}
_ = tpl.Execute(w, data)
}
func handleSearch(w http.ResponseWriter, r *http.Request) {
q := strings.TrimSpace(r.URL.Query().Get("q"))
qc := canon(q) // WICHTIG: normalisieren
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if q == "" {
_ = json.NewEncoder(w).Encode(searchResp{
Query: q,
Message: "Gib einen Barcode ein.",
Headers: headers,
SuffixLen: *suffixLen,
SearchKeyCol: *keyCol,
ReturnValueCol: *valueCol,
})
return
}
mu.RLock()
defer mu.RUnlock()
resp := searchResp{
Query: q,
Headers: headers,
SuffixLen: *suffixLen,
SearchKeyCol: *keyCol,
ReturnValueCol: *valueCol,
}
// 1) Exakter Treffer (case/whitespace-insensitive)
if rec, ok := exactMap[qc]; ok {
resp.ExactHit = &item{
Key: rec[*keyCol],
Value: rec[*valueCol],
FullRow: rec,
}
_ = json.NewEncoder(w).Encode(resp)
return
}
// 2) Suffix-Treffer (letzte N Zeichen) mit canon
if *suffixLen > 0 && len(qc) >= *suffixLen {
suf := lastN(qc, *suffixLen)
if cands, ok := suffixMap[suf]; ok && len(cands) > 0 {
resp.SuffixUsed = true
resp.TopCandidate = &item{
Key: cands[0][*keyCol],
Value: cands[0][*valueCol],
FullRow: cands[0],
}
if len(cands) > 1 {
limit := len(cands)
if limit > 10 {
limit = 10
}
resp.Candidates = toItems(cands[1:limit])
}
_ = json.NewEncoder(w).Encode(resp)
return
}
}
// 3) Teiltreffer (Substring) in der Key-Spalte
var partial []item
for _, rec := range records {
if strings.Contains(canon(rec[*keyCol]), qc) {
partial = append(partial, item{
Key: rec[*keyCol],
Value: rec[*valueCol],
FullRow: rec,
})
if len(partial) >= 20 {
break
}
}
}
if len(partial) > 0 {
resp.TopCandidate = &partial[0]
if len(partial) > 1 {
if len(partial) > 10 {
resp.Candidates = partial[1:10]
} else {
resp.Candidates = partial[1:]
}
}
} else {
resp.Message = "Kein Treffer."
}
_ = json.NewEncoder(w).Encode(resp)
}
func canon(s string) string {
return strings.ToLower(strings.TrimSpace(s))
}
func indexOf(ss []string, s string) int {
@@ -136,13 +286,28 @@ func indexOf(ss []string, s string) int {
return -1
}
func lastN(s string, n int) string {
if n <= 0 {
return ""
}
r := []rune(s)
if len(r) <= n {
return s
}
return string(r[len(r)-n:])
}
type searchResp struct {
Query string `json:"query"`
ExactHit *item `json:"exact_hit,omitempty"`
TopCandidate *item `json:"top_candidate,omitempty"`
Candidates []item `json:"candidates,omitempty"`
Message string `json:"message,omitempty"`
Headers []string `json:"headers,omitempty"`
Query string `json:"query"`
ExactHit *item `json:"exact_hit,omitempty"`
SuffixUsed bool `json:"suffix_used,omitempty"`
TopCandidate *item `json:"top_candidate,omitempty"`
Candidates []item `json:"candidates,omitempty"`
Message string `json:"message,omitempty"`
Headers []string `json:"headers,omitempty"`
SuffixLen int `json:"suffix_len,omitempty"`
SearchKeyCol string `json:"search_key_col,omitempty"`
ReturnValueCol string `json:"return_value_col,omitempty"`
}
type item struct {
@@ -151,71 +316,16 @@ type item struct {
FullRow record `json:"full_row,omitempty"`
}
func handleIndex(w http.ResponseWriter, r *http.Request) {
tpl := template.Must(template.New("index").Parse(indexHTML))
data := struct {
File string
KeyCol string
ValCol string
}{
File: filepath.Base(*csvPath),
KeyCol: *keyCol,
ValCol: *valueCol,
func toItems(rs []record) []item {
out := make([]item, 0, len(rs))
for _, rec := range rs {
out = append(out, item{
Key: rec[*keyCol],
Value: rec[*valueCol],
FullRow: rec,
})
}
_ = tpl.Execute(w, data)
}
func handleSearch(w http.ResponseWriter, r *http.Request) {
q := strings.TrimSpace(r.URL.Query().Get("q"))
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if q == "" {
_ = json.NewEncoder(w).Encode(searchResp{Query: q, Message: "Gib einen Suchbegriff ein.", Headers: headers})
return
}
mu.RLock()
defer mu.RUnlock()
lq := strings.ToLower(q)
var exact *item
var partial []item
for _, rec := range records {
k := rec[*keyCol]
if strings.EqualFold(k, q) {
exact = &item{
Key: k,
Value: rec[*valueCol],
FullRow: rec,
}
break
}
if strings.Contains(strings.ToLower(k), lq) {
partial = append(partial, item{
Key: k,
Value: rec[*valueCol],
FullRow: rec,
})
}
}
resp := searchResp{Query: q, Headers: headers}
if exact != nil {
resp.ExactHit = exact
} else if len(partial) > 0 {
resp.TopCandidate = &partial[0]
if len(partial) > 1 {
limit := len(partial)
if limit > 10 {
limit = 10
}
resp.Candidates = partial[:limit]
}
} else {
resp.Message = "Kein Treffer."
}
_ = json.NewEncoder(w).Encode(resp)
return out
}
const indexHTML = `
@@ -227,8 +337,8 @@ const indexHTML = `
<title>CSV-Suche</title>
<style>
body { font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif; margin: 2rem; }
.wrap { max-width: 840px; margin: 0 auto; }
input[type="text"] { width: 100%; font-size: 1.25rem; padding: .75rem 1rem; box-sizing: border-box; }
.wrap { max-width: 880px; margin: 0 auto; }
input[type="text"] { width: 100%; font-size: 1.35rem; padding: .8rem 1rem; box-sizing: border-box; }
.muted { color: #666; font-size: .95rem; margin-top: .5rem; }
.result { margin-top: 1rem; padding: 1rem; border: 1px solid #ddd; border-radius: .5rem; }
.hit { font-weight: 600; margin-bottom: .5rem; }
@@ -237,16 +347,17 @@ const indexHTML = `
.row { padding: .25rem 0; border-bottom: 1px dashed #eee; }
code { background: #f6f8fa; padding: .15rem .35rem; border-radius: .25rem; }
.pill { display: inline-block; padding: .1rem .5rem; border: 1px solid #ddd; border-radius: 999px; margin-right: .25rem; font-size: .85rem; }
.green { color: #0a7d32; }
</style>
</head>
<body>
<div class="wrap">
<h1>CSV-Suche</h1>
<h1>Barcode-Suche</h1>
<p class="muted">
Datei: <code>{{ .File }}</code> • Suche in <span class="pill">{{ .KeyCol }}</span> → zeige <span class="pill">{{ .ValCol }}</span>
Datei: <code>{{ .File }}</code> • Suche in <span class="pill">{{ .KeyCol }}</span> → zeige <span class="pill">{{ .ValCol }}</span>{{if .HasSuf}} • Suffix: <span class="pill">{{ .Suffix }}</span>{{end}}
</p>
<input id="q" type="text" placeholder="Suchtext eingeben …" autocomplete="off" autofocus />
<input id="q" type="text" placeholder="Barcode scannen …" autocomplete="off" autofocus />
<div id="result" class="result" aria-live="polite"></div>
</div>
@@ -256,66 +367,75 @@ const indexHTML = `
var qEl = document.getElementById('q');
var resEl = document.getElementById('result');
// Fokus immer halten
window.addEventListener('load', function() { qEl.focus(); qEl.select(); });
// Fokus halten & Starttext
window.addEventListener('load', function() {
qEl.focus(); qEl.select();
resEl.innerHTML = '<span class="muted">Gib einen Barcode ein und bestätige mit Enter.</span>';
});
window.addEventListener('click', function() { qEl.focus(); });
var timer = null;
qEl.addEventListener('input', function() {
clearTimeout(timer);
timer = setTimeout(runSearch, 120);
});
// NUR Enter löst eine Suche aus
qEl.addEventListener('keydown', function(e) {
if (e.key === 'Enter') {
e.preventDefault();
runSearch(true);
var submitted = qEl.value.trim();
if (!submitted) { qEl.focus(); return; }
runSearch(true, submitted, true); // highlight=true, clearAfter=true
}
});
function runSearch(highlight) {
var q = qEl.value.trim();
if (!q) {
resEl.innerHTML = '<span class="muted">Gib einen Suchbegriff ein.</span>';
return;
/**
* runSearch(highlight, qOverride, clearAfter)
* - highlight: bool (nur als Flag nutzbar, falls du später Styling willst)
* - qOverride: string | null, wenn gesetzt wird dieser Wert verwendet
* - clearAfter: bool, Eingabefeld nach dem Request leeren
*/
function runSearch(highlight, qOverride, clearAfter) {
var q = (qOverride !== null && qOverride !== undefined) ? qOverride : qEl.value.trim();
if (!q) {
resEl.innerHTML = '<span class="muted">Gib einen Barcode ein und bestätige mit Enter.</span>';
return;
}
fetch('/search?q=' + encodeURIComponent(q))
.then(function(r) { return r.json(); })
.then(function(data) {
render(data, !!highlight);
.then(function(d) { render(d, !!highlight); })
.catch(function() { resEl.innerHTML = '<span class="muted">Fehler bei der Suche.</span>'; })
.finally(function() {
if (clearAfter) { qEl.value = ''; }
qEl.focus();
var len = qEl.value.length;
try { qEl.setSelectionRange(len, len); } catch(e) {}
})
.catch(function() {
resEl.innerHTML = '<span class="muted">Fehler bei der Suche.</span>';
try {
var len = qEl.value.length;
qEl.setSelectionRange(len, len);
} catch(e) {}
});
}
function render(d, highlight) {
if (d.ExactHit) {
if (d.exact_hit) {
resEl.innerHTML =
'<div class="hit">Exakter Treffer für <code>' + escapeHtml(d.Query) + '</code>:</div>' +
'<div class="value">' + escapeHtml(d.ExactHit.Value || '') + '</div>' +
renderRow(d.ExactHit.FullRow, d.Headers);
'<div class="hit">Exakter Treffer für <code>' + escapeHtml(d.query) + '</code>:</div>' +
'<div class="value"><h2>' + escapeHtml(d.exact_hit.value || '') + '</h2></div>' +
renderRow(d.exact_hit.full_row, d.headers);
return;
}
if (d.TopCandidate) {
if (d.top_candidate) {
var badge = d.suffix_used ? ' <span class="pill green">Suffix-Match (' + (d.suffix_len||'') + ')</span>' : '';
var list = '';
if (d.Candidates && d.Candidates.length > 0) {
if (d.candidates && d.candidates.length > 0) {
list = '<div class="list"><div class="muted">Weitere Kandidaten:</div>' +
d.Candidates.map(function(c) {
return '<div class="row"><code>' + escapeHtml(c.Key) + '</code> → ' + escapeHtml(c.Value || '') + '</div>';
d.candidates.map(function(c) {
return '<div class="row"><code>' + escapeHtml(c.key) + '</code> → ' + escapeHtml(c.value || '') + '</div>';
}).join('') +
'</div>';
}
resEl.innerHTML =
'<div class="hit">Bester Treffer zu <code>' + escapeHtml(d.Query) + '</code>:</div>' +
'<div class="value">' + escapeHtml(d.TopCandidate.Value || '') + '</div>' +
renderRow(d.TopCandidate.FullRow, d.Headers) +
'<div class="hit">Bester Treffer zu <code>' + escapeHtml(d.query) + '</code>:' + badge + '</div>' +
'<div class="value"><h2>' + escapeHtml(d.top_candidate.value || '') + '</h2></div>' +
renderRow(d.top_candidate.full_row, d.headers) +
list;
return;
}
resEl.innerHTML = '<span class="muted">' + (d.Message || 'Kein Treffer.') + '</span>';
resEl.innerHTML = '<span class="muted">' + (d.message || 'Kein Treffer.') + '</span>';
}
function renderRow(row, headers) {
@@ -331,11 +451,10 @@ const indexHTML = `
return ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;', "'":'&#039;'}[m]);
});
}
// initial
runSearch(false);
})();
</script>
</body>
</html>
`