Skip to content

Commit

Permalink
Merge pull request #6 from rorycl/web-docker
Browse files Browse the repository at this point in the history
v0.2.4: improve error handling; deploy via docker
  • Loading branch information
rorycl authored May 9, 2024
2 parents 3061c7c + 9d14299 commit e77d8d4
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ go.work
cover.html
cover.rpt
coverage.out
deploy
28 changes: 28 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# build the webapp including dependencies from / and /cmd

FROM golang:1.22 AS deps

# setup module environment
WORKDIR /build
ADD *mod ./
ADD *sum ./
RUN go mod download
ADD cmd/web/*mod ./cmd/web/
ADD cmd/web/*sum ./cmd/web/
RUN cd /build/cmd/web && go mod download

# build
FROM deps as dev
ADD *go ./
ADD cmd/*go ./cmd/
ADD cmd/web/ ./cmd/web/
RUN cd cmd/web && \
CGO_ENABLED=0 GOOS=linux \
go build -ldflags "-w -X main.docker=true" -o /build/webserver .

# install into minimal image
FROM gcr.io/distroless/base AS base
WORKDIR /
EXPOSE 8000
COPY --from=dev /build/webserver /
CMD ["/webserver", "--address", "0.0.0.0", "--port", "8000"]
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# cexfind

A Go module with console, cli and web app clients for rapid and
effective searches for equipment on Cex/Webuy using the unofficial
`webuy.io` json search endpoint.
effective searches for second hand equipment for sale at Cex/Webuy using
the unofficial `webuy.io` json search endpoint.

Note that these programs only work for queries made in the UK (or via a
proxy terminating in the UK).
proxy terminating in the UK). This is intended to be a fun project and
is not intended for commercial use.

This is intended to be a fun project and is not intended for commercial
use.
The web client is presently deployed on Google Cloud Run. [Try it
out!](https://cexfind-min-poyflf5akq-nw.a.run.app/).

## Usage

Expand Down
13 changes: 11 additions & 2 deletions cex.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ func Search(queries []string, strict bool) ([]Box, error) {
results := makeQueries(queries, strict)
for br := range results {
if br.err != nil {
err = fmt.Errorf("\"%s\": %w", br.query, br.err)
if err != nil {
err = fmt.Errorf("\"%s\": %w\n%w", br.query, br.err, err)
} else {
err = fmt.Errorf("\"%s\": %w", br.query, br.err)
}
continue
}
if _, ok := idMap[br.box.ID]; ok { // don't add duplicates
Expand All @@ -138,7 +142,12 @@ func Search(queries []string, strict bool) ([]Box, error) {
}
allBoxes.sort()
if len(allBoxes) == 0 {
return allBoxes, errors.New("no results")
if err != nil {
err = fmt.Errorf("%w", err)
} else {
err = errors.New("no results")
}
return allBoxes, err
}
return allBoxes, err
}
2 changes: 1 addition & 1 deletion cmd/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var Server func(address, port string) = Serve
var usage = `
run a webserver to search Cex/Webuy for second hand equipment
eg <programme> [-a 127.0.0.1] [-p 8001]
eg <programme> -address 192.168.4.5 -port 8001
`

// indirect Exit for testing
Expand Down
6 changes: 4 additions & 2 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ var (
urlDetail = "https://uk.webuy.com/product-detail?id="
// save web output to temp file if DEBUG true
debug = false
//
NoResultsFoundError error = errors.New("no results found")
)

// jsonResults encompasses the interesting fields in a Cex web search result
Expand Down Expand Up @@ -127,7 +129,7 @@ func postQuery(queryBytes []byte) (jsonResults, error) {
if errors.As(err, &ju) {
// no results tend to provide data that cannot be parsed,
// used for a general "home" type page
return r, errors.New("no results found")
return r, NoResultsFoundError
}
// assume html page; try and extract heading
reason := headingExtract(responseBytes)
Expand All @@ -137,7 +139,7 @@ func postQuery(queryBytes []byte) (jsonResults, error) {
return r, errors.New(reason)
}
if len(r.Results) < 1 || len(r.Results[0].Hits) < 1 {
return r, errors.New("no results")
return r, NoResultsFoundError
}
return r, nil
}
Expand Down

0 comments on commit e77d8d4

Please sign in to comment.