Compare commits
31 Commits
Author | SHA1 | Date | |
---|---|---|---|
e3f0c84e7a | |||
21f8b5fb2c | |||
5599432db2 | |||
1f256c1dc6 | |||
342b21b355 | |||
0ec24e3a83 | |||
c48cb45bf0 | |||
b954cb7162 | |||
376acdd0b5 | |||
1bba61d476 | |||
b0fe17cea2 | |||
a4c6607194 | |||
3f7a895593 | |||
7a5018bc5e | |||
1b325169cf | |||
d8580bf90f | |||
3beed131a8 | |||
64efaf5514 | |||
8d320f96cd | |||
75c5e7ea4c | |||
e6e668451a | |||
9528fca452 | |||
cf95ee2acb | |||
4a673d1894 | |||
0d22da3643 | |||
0107f77570 | |||
d5f658244b | |||
72128f97a3 | |||
78e2568e2b | |||
75ba450d9b | |||
dd7f0c0a28 |
@ -4,35 +4,86 @@ on:
|
|||||||
push:
|
push:
|
||||||
tags:
|
tags:
|
||||||
- "*"
|
- "*"
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
changelog:
|
build-and-push-container:
|
||||||
name: Generate changelog
|
name: Build Container
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
permissions:
|
permissions:
|
||||||
contents: write
|
contents: write
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Docker meta
|
||||||
uses: actions/checkout@v4
|
id: meta
|
||||||
|
uses: docker/metadata-action@v5
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
images: |
|
||||||
|
git.dev-null.rocks/alexohneander/gosearch
|
||||||
|
tags: |
|
||||||
|
type=schedule,pattern={{date 'YYYYMMDDHHMM'}}
|
||||||
|
type=semver,pattern={{version}}
|
||||||
|
type=semver,pattern={{major}}.{{minor}}
|
||||||
|
type=semver,pattern={{major}}
|
||||||
|
type=sha
|
||||||
|
|
||||||
- name: Generate changelog
|
- name: Set up Docker Context for Buildx
|
||||||
uses: orhun/git-cliff-action@v4
|
id: buildx-context
|
||||||
with:
|
|
||||||
config: cliff.toml
|
|
||||||
args: --verbose
|
|
||||||
env:
|
|
||||||
OUTPUT: CHANGELOG.md
|
|
||||||
GITHUB_REPO: ${{ github.repository }}
|
|
||||||
|
|
||||||
- name: Commit
|
|
||||||
run: |
|
run: |
|
||||||
git checkout <branch>
|
export DOCKER_HOST=tcp://docker:2376/
|
||||||
git config user.name 'github-actions[bot]'
|
export DOCKER_TLS_CERTDIR="/certs"
|
||||||
git config user.email 'github-actions[bot]@users.noreply.github.com'
|
export DOCKER_TLS_VERIFY=1
|
||||||
set +e
|
export DOCKER_CERT_PATH="/certs/client"
|
||||||
git add CHANGELOG.md
|
docker context create builders
|
||||||
git commit -m "Update changelog"
|
|
||||||
git push https://${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}.git <branch>
|
- name: Setup Docker Buildx
|
||||||
|
id: buildx
|
||||||
|
uses: docker/setup-buildx-action@v3
|
||||||
|
with:
|
||||||
|
# buildkitd-flags: --debug
|
||||||
|
endpoint: builders
|
||||||
|
env:
|
||||||
|
DOCKER_HOST: "tcp://docker:2376/"
|
||||||
|
DOCKER_TLS_CERTDIR: "/certs"
|
||||||
|
DOCKER_TLS_VERIFY: 1
|
||||||
|
DOCKER_CERT_PATH: "/certs/client"
|
||||||
|
|
||||||
|
- name: Login to Gitea
|
||||||
|
uses: docker/login-action@v3
|
||||||
|
with:
|
||||||
|
registry: git.dev-null.rocks
|
||||||
|
username: ${{ gitea.actor }}
|
||||||
|
password: ${{ secrets.GA_TOKEN }}
|
||||||
|
|
||||||
|
- name: Build and push the image
|
||||||
|
uses: docker/build-push-action@v5
|
||||||
|
with:
|
||||||
|
file: Dockerfile
|
||||||
|
platforms: linux/amd64
|
||||||
|
push: true
|
||||||
|
tags: ${{ steps.meta.outputs.tags }}
|
||||||
|
labels: ${{ steps.meta.outputs.labels }}
|
||||||
|
env:
|
||||||
|
DOCKER_HOST: "tcp://docker:2376/"
|
||||||
|
DOCKER_TLS_CERTDIR: "/certs"
|
||||||
|
DOCKER_TLS_VERIFY: 1
|
||||||
|
DOCKER_CERT_PATH: "/certs/client"
|
||||||
|
|
||||||
|
# - name: Generate changelog
|
||||||
|
# uses: orhun/git-cliff-action@v4
|
||||||
|
# with:
|
||||||
|
# config: cliff.toml
|
||||||
|
# args: --verbose
|
||||||
|
# env:
|
||||||
|
# OUTPUT: CHANGELOG.md
|
||||||
|
# GITHUB_REPO: ${{ github.repository }}
|
||||||
|
|
||||||
|
# - name: Commit
|
||||||
|
# run: |
|
||||||
|
# git checkout <branch>
|
||||||
|
# git config user.name 'github-actions[bot]'
|
||||||
|
# git config user.email 'github-actions[bot]@users.noreply.github.com'
|
||||||
|
# set +e
|
||||||
|
# git add CHANGELOG.md
|
||||||
|
# git commit -m "Update changelog"
|
||||||
|
# git push https://${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}.git <branch>
|
@ -10,7 +10,7 @@ on:
|
|||||||
jobs:
|
jobs:
|
||||||
renovate:
|
renovate:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
container: ghcr.io/renovatebot/renovate:39.215.2
|
container: ghcr.io/renovatebot/renovate:39.264.0
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- run: renovate
|
- run: renovate
|
||||||
|
25
.gitea/workflows/sonarqube.yaml
Normal file
25
.gitea/workflows/sonarqube.yaml
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
types: [opened, synchronize, reopened]
|
||||||
|
|
||||||
|
name: SonarQube Scan
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
name: Build and analyze
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
|
||||||
|
- uses: SonarSource/sonarqube-scan-action@v4
|
||||||
|
env:
|
||||||
|
SONAR_TOKEN: ${{ secrets.SONARQUBE_TOKEN }}
|
||||||
|
SONAR_HOST_URL: ${{ secrets.SONARQUBE_HOST }}
|
||||||
|
- uses: SonarSource/sonarqube-quality-gate-action@v1
|
||||||
|
timeout-minutes: 5
|
||||||
|
env:
|
||||||
|
SONAR_TOKEN: ${{ secrets.SONARQUBE_TOKEN }}
|
31
CHANGELOG.md
31
CHANGELOG.md
@ -2,7 +2,35 @@
|
|||||||
|
|
||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
## [unreleased]
|
## [0.1.1] - 2025-03-26
|
||||||
|
|
||||||
|
### 🚀 Features
|
||||||
|
|
||||||
|
- *(index)* Add save and load functionality for Index using gob encoding/decoding
|
||||||
|
|
||||||
|
### 🐛 Bug Fixes
|
||||||
|
|
||||||
|
- Updated AddDocToIndex function to ignore additional special characters in scanner.Text()
|
||||||
|
- Add sonarqube properties
|
||||||
|
- Add default log level
|
||||||
|
|
||||||
|
### 🚜 Refactor
|
||||||
|
|
||||||
|
- Search logic to improve performance and reduce false positives by making query case-insensitive for simple queries and using trimmed whitespace in phrases
|
||||||
|
|
||||||
|
### 📚 Documentation
|
||||||
|
|
||||||
|
- Add first release in Changelog
|
||||||
|
- Update changelog
|
||||||
|
- Add sonarqube badge
|
||||||
|
|
||||||
|
### ⚙️ Miscellaneous Tasks
|
||||||
|
|
||||||
|
- Add sonarqube pipeline
|
||||||
|
- Update sonarqube pipeline
|
||||||
|
- Add quality gate
|
||||||
|
|
||||||
|
## [0.1.0] - 2025-03-25
|
||||||
|
|
||||||
### 🚀 Features
|
### 🚀 Features
|
||||||
|
|
||||||
@ -30,5 +58,6 @@ All notable changes to this project will be documented in this file.
|
|||||||
- Added golang workflow for building and testing project
|
- Added golang workflow for building and testing project
|
||||||
- Update Go version in build-and-test workflow to 1.23
|
- Update Go version in build-and-test workflow to 1.23
|
||||||
- Added renovate configuration files for Gitea workflow automation
|
- Added renovate configuration files for Gitea workflow automation
|
||||||
|
- Add release pipeline
|
||||||
|
|
||||||
<!-- generated by git-cliff -->
|
<!-- generated by git-cliff -->
|
||||||
|
26
Dockerfile
Normal file
26
Dockerfile
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
# Use the official Golang image for building
|
||||||
|
FROM golang:1.24 AS builder
|
||||||
|
# Set working directory
|
||||||
|
WORKDIR /app
|
||||||
|
# Copy Go modules and dependencies
|
||||||
|
COPY go.mod go.sum ./
|
||||||
|
RUN go mod download
|
||||||
|
# Copy source code
|
||||||
|
COPY . .
|
||||||
|
# Build the application
|
||||||
|
RUN go build -o gosearch .
|
||||||
|
|
||||||
|
# Use a minimal base image for final deployment
|
||||||
|
FROM alpine:3.22
|
||||||
|
# Set working directory in the container
|
||||||
|
WORKDIR /
|
||||||
|
# Copy the built binary from the builder stage
|
||||||
|
COPY --from=builder /app/gosearch .
|
||||||
|
# Add executing User
|
||||||
|
RUN addgroup gosearch \
|
||||||
|
&& useradd -g gosearch gosearch
|
||||||
|
USER gosearch
|
||||||
|
# Expose the application port
|
||||||
|
EXPOSE 3000
|
||||||
|
# Run the application
|
||||||
|
CMD ["./gosearch"]
|
@ -1,3 +1,7 @@
|
|||||||
|
[](https://sonar.dev-null.rocks/dashboard?id=gosearch)
|
||||||
|
[](https://sonar.dev-null.rocks/dashboard?id=gosearch)
|
||||||
|
[](https://sonar.dev-null.rocks/dashboard?id=gosearch)
|
||||||
|
|
||||||
# gosearch
|
# gosearch
|
||||||
|
|
||||||
### Diagram of the Architecture
|
### Diagram of the Architecture
|
||||||
|
2
go.mod
2
go.mod
@ -2,7 +2,7 @@ module git.dev-null.rocks/alexohneander/gosearch
|
|||||||
|
|
||||||
go 1.23.5
|
go 1.23.5
|
||||||
|
|
||||||
require github.com/gofiber/fiber/v2 v2.52.6
|
require github.com/gofiber/fiber/v2 v2.52.8
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/mattn/go-runewidth v0.0.16 // indirect
|
github.com/mattn/go-runewidth v0.0.16 // indirect
|
||||||
|
2
go.sum
2
go.sum
@ -2,6 +2,8 @@ github.com/andybalholm/brotli v1.1.1 h1:PR2pgnyFznKEugtsUo0xLdDop5SKXd5Qf5ysW+7X
|
|||||||
github.com/andybalholm/brotli v1.1.1/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA=
|
github.com/andybalholm/brotli v1.1.1/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA=
|
||||||
github.com/gofiber/fiber/v2 v2.52.6 h1:Rfp+ILPiYSvvVuIPvxrBns+HJp8qGLDnLJawAu27XVI=
|
github.com/gofiber/fiber/v2 v2.52.6 h1:Rfp+ILPiYSvvVuIPvxrBns+HJp8qGLDnLJawAu27XVI=
|
||||||
github.com/gofiber/fiber/v2 v2.52.6/go.mod h1:YEcBbO/FB+5M1IZNBP9FO3J9281zgPAreiI1oqg8nDw=
|
github.com/gofiber/fiber/v2 v2.52.6/go.mod h1:YEcBbO/FB+5M1IZNBP9FO3J9281zgPAreiI1oqg8nDw=
|
||||||
|
github.com/gofiber/fiber/v2 v2.52.8 h1:xl4jJQ0BV5EJTA2aWiKw/VddRpHrKeZLF0QPUxqn0x4=
|
||||||
|
github.com/gofiber/fiber/v2 v2.52.8/go.mod h1:YEcBbO/FB+5M1IZNBP9FO3J9281zgPAreiI1oqg8nDw=
|
||||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
|
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
|
||||||
|
@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
func SearchQuery(c *fiber.Ctx) error {
|
func SearchQuery(c *fiber.Ctx) error {
|
||||||
query := c.Params("query")
|
query := c.Params("query")
|
||||||
query = strings.TrimSpace(strings.ToLower(query))
|
query = strings.TrimSpace(query)
|
||||||
|
|
||||||
terms, queryType := parseQuery(query)
|
terms, queryType := parseQuery(query)
|
||||||
results := search.Search(terms, queryType, index.Index, index.DocFreq, len(index.Documents))
|
results := search.Search(terms, queryType, index.Index, index.DocFreq, len(index.Documents))
|
||||||
@ -29,12 +29,12 @@ func SearchQuery(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
// parseQuery parses the query to determine query type and terms
|
// parseQuery parses the query to determine query type and terms
|
||||||
func parseQuery(query string) ([]string, string) {
|
func parseQuery(query string) ([]string, string) {
|
||||||
if strings.Contains(query, "AND") {
|
if strings.Contains(strings.ToLower(query), "AND") {
|
||||||
return strings.Split(query, " AND "), "AND"
|
return strings.Split(strings.ToLower(query), " AND "), "AND"
|
||||||
} else if strings.Contains(query, "OR") {
|
} else if strings.Contains(strings.ToLower(query), "OR") {
|
||||||
return strings.Split(query, " OR "), "OR"
|
return strings.Split(strings.ToLower(query), " OR "), "OR"
|
||||||
}
|
}
|
||||||
return strings.Fields(query), "SIMPLE"
|
return strings.Fields(strings.ToLower(query)), "SIMPLE"
|
||||||
}
|
}
|
||||||
|
|
||||||
// phraseMatch checks if all terms appear in the given document in sequence
|
// phraseMatch checks if all terms appear in the given document in sequence
|
||||||
|
@ -2,6 +2,7 @@ package http
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/gofiber/fiber/v2/log"
|
||||||
"github.com/gofiber/fiber/v2/middleware/logger"
|
"github.com/gofiber/fiber/v2/middleware/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -12,6 +13,7 @@ func StartService() {
|
|||||||
app.Use(logger.New(logger.Config{
|
app.Use(logger.New(logger.Config{
|
||||||
Format: "[${ip}]:${port} ${status} - ${method} ${path}\n",
|
Format: "[${ip}]:${port} ${status} - ${method} ${path}\n",
|
||||||
}))
|
}))
|
||||||
|
log.SetLevel(log.LevelInfo)
|
||||||
|
|
||||||
// Configure Routes
|
// Configure Routes
|
||||||
app = configureRoutes(app)
|
app = configureRoutes(app)
|
||||||
|
2
main.go
2
main.go
@ -7,7 +7,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Initialize Index
|
// Initialize Index
|
||||||
index.CreateIndex()
|
index.InitIndex("default")
|
||||||
|
|
||||||
// Start HTTP Server
|
// Start HTTP Server
|
||||||
http.StartService()
|
http.StartService()
|
||||||
|
@ -2,6 +2,12 @@ package index
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
"encoding/gob"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -13,7 +19,40 @@ var Index InvertedIndex
|
|||||||
var DocFreq DocumentFrequency
|
var DocFreq DocumentFrequency
|
||||||
var Documents []string
|
var Documents []string
|
||||||
|
|
||||||
func CreateIndex() {
|
type SavedIndex struct {
|
||||||
|
Index InvertedIndex
|
||||||
|
DocFreq DocumentFrequency
|
||||||
|
Documents []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func InitIndex(name string) {
|
||||||
|
createIndex()
|
||||||
|
|
||||||
|
// check if index as file exists
|
||||||
|
// if not, create one and save it
|
||||||
|
indexFilePath := "/tmp/" + name + ".db"
|
||||||
|
if _, err := os.Stat(indexFilePath); errors.Is(err, os.ErrNotExist) {
|
||||||
|
f, err := os.Create(indexFilePath)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
} else {
|
||||||
|
var savedIndex SavedIndex
|
||||||
|
|
||||||
|
err = readStructFromFile(indexFilePath, &savedIndex)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Fehler beim Lesen:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
Index = savedIndex.Index
|
||||||
|
DocFreq = savedIndex.DocFreq
|
||||||
|
Documents = savedIndex.Documents
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func createIndex() {
|
||||||
index := make(InvertedIndex)
|
index := make(InvertedIndex)
|
||||||
docFreq := make(DocumentFrequency)
|
docFreq := make(DocumentFrequency)
|
||||||
var docs []string
|
var docs []string
|
||||||
@ -23,6 +62,21 @@ func CreateIndex() {
|
|||||||
DocFreq = docFreq
|
DocFreq = docFreq
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func updateIndex(name string) {
|
||||||
|
savedIndex := SavedIndex{
|
||||||
|
Index: Index,
|
||||||
|
DocFreq: DocFreq,
|
||||||
|
Documents: Documents,
|
||||||
|
}
|
||||||
|
|
||||||
|
indexFilePath := "/tmp/" + name + ".db"
|
||||||
|
err := writeStructToFile(indexFilePath, savedIndex)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Fehler beim Schreiben:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func AddDocToIndex(url string, content string) {
|
func AddDocToIndex(url string, content string) {
|
||||||
Documents = append(Documents, url)
|
Documents = append(Documents, url)
|
||||||
|
|
||||||
@ -33,7 +87,7 @@ func AddDocToIndex(url string, content string) {
|
|||||||
scanner.Split(bufio.ScanWords)
|
scanner.Split(bufio.ScanWords)
|
||||||
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
word := strings.ToLower(strings.Trim(scanner.Text(), ",.!?"))
|
word := strings.ToLower(strings.Trim(scanner.Text(), ",.!?&<>;:=§$%{}[]()|"))
|
||||||
|
|
||||||
if Index[word] == nil {
|
if Index[word] == nil {
|
||||||
Index[word] = make(map[string]int)
|
Index[word] = make(map[string]int)
|
||||||
@ -46,4 +100,35 @@ func AddDocToIndex(url string, content string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
go updateIndex("default")
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeStructToFile(filename string, data interface{}) error {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
enc := gob.NewEncoder(&buf)
|
||||||
|
err := enc.Encode(data)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error when encoding the structs: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.WriteFile(filename, buf.Bytes(), 0644)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error when writing to the file: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func readStructFromFile(filename string, data interface{}) error {
|
||||||
|
content, err := os.ReadFile(filename)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error reading the file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := bytes.NewBuffer(content)
|
||||||
|
dec := gob.NewDecoder(buf)
|
||||||
|
err = dec.Decode(data)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error when decoding the structs: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
1
sonar-project.properties
Normal file
1
sonar-project.properties
Normal file
@ -0,0 +1 @@
|
|||||||
|
sonar.projectKey=gosearch
|
Loading…
x
Reference in New Issue
Block a user