Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add Search Details #31

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
757a134
chore: refactor
luigibarbato Dec 11, 2022
f53f2bc
chore: format code
luigibarbato Dec 12, 2022
495d45b
feat: add special chars capability,deploy workflow
luigibarbato Dec 13, 2022
c209638
chore: pin fly action version, optimize workflow logic, optimize fly …
luigibarbato Dec 13, 2022
cb13c4b
chore: small refactor,add some bleve test
luigibarbato Dec 27, 2022
d17be92
chore: packages refactor
luigibarbato Jun 29, 2023
1e0a179
chore: disable auto deploy for now
luigibarbato Jun 29, 2023
8cfa54a
fix: remove useless test
luigibarbato Jun 29, 2023
cfb5eb8
chore(deps): bump github.com/stretchr/testify from 1.8.1 to 1.8.4
dependabot[bot] Jun 29, 2023
136a376
feat: add dependabot auto-merge
luigibarbato Jun 29, 2023
999772e
chore: use custom private secret for dependabot
luigibarbato Jun 29, 2023
27feb67
chore(deps): bump github.com/spf13/viper from 1.13.0 to 1.16.0
dependabot[bot] Jun 29, 2023
f9eec0e
chore(deps): bump github.com/deepmap/oapi-codegen from 1.11.0 to 1.13.0
dependabot[bot] Jun 29, 2023
ffb63af
chore(deps): bump go.uber.org/zap from 1.17.0 to 1.24.0
dependabot[bot] Jul 6, 2023
0b18e49
chore: update to bleve v2 and go 1.20
luigibarbato Jul 24, 2023
9f25282
fix: use auto-merge from gh docs
luigibarbato Jul 24, 2023
53398b7
chore: dependabot auto-rebase instead of auto-merge
luigibarbato Jul 24, 2023
06ede3b
chore(deps): bump github.com/deepmap/oapi-codegen from 1.13.2 to 1.13.3
dependabot[bot] Aug 3, 2023
1b76211
chore(deps): bump github.com/deepmap/oapi-codegen from 1.13.3 to 1.13.4
dependabot[bot] Aug 7, 2023
4d30510
feat: add wiki informer
luigibarbato Aug 30, 2023
321cf05
chore: refactor
luigibarbato Sep 6, 2023
6c53fbc
chore: remove old test case
luigibarbato Sep 6, 2023
90404a0
chore: use search service
luigibarbato Oct 8, 2023
ec133e7
chore: refactor
luigibarbato Oct 8, 2023
f0aee1e
chore: rename search fetch method
luigibarbato Oct 9, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 45 additions & 15 deletions api/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ paths:
content:
application/json:
schema:
type: object
$ref: "#/components/schemas/ServerVersion"
operationId: get-v1-version
x-stoplight:
Expand All @@ -55,6 +56,30 @@ paths:
schema:
type: object
properties: {}
/v1/informer/wiki/{query}:
get:
responses:
"200":
description: OK
content:
application/json:
schema:
type: object
$ref: "#/components/schemas/WikiResult"
"500":
description: Internal Server Error
content:
application/json:
schema:
type: object
$ref: "#/components/schemas/Error"
parameters:
- name: query
in: path
required: true
schema:
type: string

components:
schemas:
FeedItem:
Expand Down Expand Up @@ -97,6 +122,27 @@ components:
- title
x-stoplight:
id: g5p7hclip2ydk

WikiResult:
type: object
properties:
title:
type: string
link:
type: string
summary:
type: string
thumbnail:
type: string
language:
type: string

required:
- title
- link
- summary
- thumbnail
- language
Error:
type: object
properties:
Expand Down
2 changes: 1 addition & 1 deletion fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ processes = []
interval = "15s"
restart_limit = 2
timeout = "10s"


3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ module github.com/unconditionalday/server
go 1.21

require (
github.com/anaskhan96/soup v1.2.5
github.com/deepmap/oapi-codegen v1.15.0
github.com/getkin/kin-openapi v0.120.0
github.com/labstack/echo/v4 v4.11.1
github.com/labstack/gommon v0.4.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/pflag v1.0.5
go.uber.org/zap v1.26.0
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
)

require (
Expand Down Expand Up @@ -111,7 +113,6 @@ require (
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/arch v0.4.0 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ github.com/SlyMarbo/rss v1.0.5 h1:DPcZ4aOXXHJ5yNLXY1q/57frIixMmAvTtLxDE3fsMEI=
github.com/SlyMarbo/rss v1.0.5/go.mod h1:w6Bhn1BZs91q4OlEnJVZEUNRJmlbFmV7BkAlgCN8ofM=
github.com/ajg/form v1.5.1 h1:t9c7v8JUKu/XxOGBU0yjNpaMloxGEJhUkqFRq0ibGeU=
github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY=
github.com/anaskhan96/soup v1.2.5 h1:V/FHiusdTrPrdF4iA1YkVxsOpdNcgvqT1hG+YtcZ5hM=
github.com/anaskhan96/soup v1.2.5/go.mod h1:6YnEp9A2yywlYdM4EgDz9NEHclocMepEtku7wg6Cq3s=
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ=
Expand Down Expand Up @@ -297,7 +299,6 @@ github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3x
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/labstack/echo/v4 v4.11.1 h1:dEpLU2FLg4UVmvCGPuk/APjlH6GDpbEPti61srUUUs4=
github.com/labstack/echo/v4 v4.11.1/go.mod h1:YuYRTSM3CHs2ybfrL8Px48bO6BAnYIN4l8wSTMP6BDQ=
github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
Expand Down Expand Up @@ -334,6 +335,7 @@ github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/mschoch/smat v0.2.0 h1:8imxQsjDm8yFEAVBe7azKmKSgzSkZXDuKkSq9374khM=
github.com/mschoch/smat v0.2.0/go.mod h1:kc9mz7DoBKqDyiRL7VZN8KvXQMWeTaVnttLRXOlotKw=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
Expand Down Expand Up @@ -430,6 +432,7 @@ github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.etcd.io/bbolt v1.3.7 h1:j+zJOnnEjF/kyHlDDgGnVL/AIqIJPq8UoB2GSNfkUfQ=
go.etcd.io/bbolt v1.3.7/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw=
Expand Down Expand Up @@ -766,7 +769,6 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
120 changes: 120 additions & 0 deletions internal/client/wikipedia/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package wikipedia

import (
"crypto/sha256"
"errors"
"time"
)

// Find and delete string s in string slice
func FindAndDel(arr []string, s string) []string {
index := 0
for i, v := range arr {
if v == s {
index = i
break
}
}
return append(arr[:index], arr[index+1:]...)
}

/*
Create a cache that store:

- Key: API request URL

- Value: RequestResponse
*/
func MakeWikiCache(expiration time.Duration, maxMemory int) *Cache {
if expiration != 0 {
expiration = (12 * time.Hour)
}

if maxMemory != 0 {
maxMemory = 500
}

c := &Cache{
Memory: map[string]RequestResult{},
MaxMemory: maxMemory,
Expiration: expiration,
HashedKeyQueue: make([]string, 0, maxMemory),
CreatedTime: map[string]time.Time{},
}

return c
}

// Cache to store Wikipedia request result
type Cache struct {
Memory map[string]RequestResult // Map store request result
HashedKeyQueue []string // Key queue. Delete the first item if reach max cache
CreatedTime map[string]time.Time // Map store created time
Expiration time.Duration // Cache expiration
MaxMemory int // Max cache memory
}

// Hash a string into SHA256
func HashCacheKey(s string) string {
hasher := sha256.New()
hasher.Write([]byte(s))

return string(hasher.Sum(nil))
}

// Get WikiCache current number of cache
func (cache Cache) GetLen() int {
return len(cache.HashedKeyQueue)
}

// Add cache into the WikiCache
func (cache *Cache) Add(s string, res RequestResult) {
if len(cache.Memory) >= cache.MaxMemory {
cache.Pop()
}

key := HashCacheKey(s)
if cache.Memory == nil {
cache.Memory = map[string]RequestResult{}
cache.CreatedTime = map[string]time.Time{}
cache.HashedKeyQueue = make([]string, 0, cache.MaxMemory)
}
if _, ok := cache.Memory[key]; !ok {
cache.Memory[key] = res
cache.CreatedTime[key] = time.Now()
cache.HashedKeyQueue = append(cache.HashedKeyQueue, key)
}
}

// Get response from the Cache
func (cache *Cache) Get(s string) (RequestResult, error) {
key := HashCacheKey(s)
if value, ok := cache.Memory[key]; ok {
if time.Since(cache.CreatedTime[key]) <= cache.Expiration {
cache.HashedKeyQueue = FindAndDel(cache.HashedKeyQueue, key)
cache.HashedKeyQueue = append(cache.HashedKeyQueue, key)
return value, nil
} else {
cache.HashedKeyQueue = FindAndDel(cache.HashedKeyQueue, key)
delete(cache.Memory, key)
return RequestResult{}, errors.New("the data is outdated")
}
}
return RequestResult{}, errors.New("cache key not exist")
}

// Delete the first key in the Cache
func (cache *Cache) Pop() {
if len(cache.HashedKeyQueue) == 0 {
return
}
delete(cache.Memory, cache.HashedKeyQueue[0])
cache.HashedKeyQueue = cache.HashedKeyQueue[1:]
}

// Clear the whole Cache
func (cache *Cache) Clear() {
*cache = Cache{}
// This line to avoid declare but not used error
_ = cache
}
Loading