V1.0.3: Added visit updates
This commit is contained in:
parent
e1d7d78aed
commit
1527a78e5d
4
.gitignore
vendored
4
.gitignore
vendored
@ -9,6 +9,10 @@
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Built go binary
|
||||
|
||||
go-pb-nurl
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
|
4
Makefile
4
Makefile
@ -1,7 +1,7 @@
|
||||
PROJECT = go-pb-nurl
|
||||
|
||||
VERSION=`git describe --tags`
|
||||
BUILD=`date +%FT%T%z`
|
||||
BUILD=`date +%yw%Vb`
|
||||
|
||||
|
||||
ECR_REPO = git.caliban.io/martin
|
||||
@ -20,7 +20,7 @@ build:
|
||||
# go build ${LDFLAGS} -o ${PROJECT} server.go
|
||||
|
||||
# docker build ./docker/. -t $(APP_IMAGE) --build-arg VERSION=$(VERSION) --no-cache=$(NO_CACHE) --compress=true
|
||||
# docker build --platform linux/amd64 --no-cache -force-rm --tag ${APP_IMAGE} --file ./docker/Dockerfile .
|
||||
docker build --platform linux/amd64 --no-cache -force-rm --tag ${APP_IMAGE} --file ./docker/Dockerfile .
|
||||
|
||||
|
||||
#push docker image to registry
|
||||
|
1395
data-old.json
Normal file
1395
data-old.json
Normal file
File diff suppressed because it is too large
Load Diff
15
docker-compose.yml
Normal file
15
docker-compose.yml
Normal file
@ -0,0 +1,15 @@
|
||||
---
|
||||
|
||||
services:
|
||||
nurl:
|
||||
container_name: nurl
|
||||
image: git.caliban.io/martin/go-pb-nurl:1.0.1
|
||||
restart: always
|
||||
ports:
|
||||
- "7000:7000"
|
||||
healthcheck:
|
||||
test: curl --fail http://localhost:7000/!/hb || exit 1
|
||||
interval: 40s
|
||||
timeout: 30s
|
||||
retries: 3
|
||||
start_period: 60s
|
29
docker/Dockerfile
Normal file
29
docker/Dockerfile
Normal file
@ -0,0 +1,29 @@
|
||||
FROM debian:12-slim
|
||||
# FROM alpine:latest
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
RUN mkdir -p /app/dist
|
||||
|
||||
COPY ./go-pb-nurl /app/
|
||||
|
||||
|
||||
COPY ./pb_data /app/pb_data
|
||||
COPY ./pb_public /app/pb_public
|
||||
|
||||
RUN apt-get update && apt-get install -y ca-certificates
|
||||
|
||||
# Need the following to get a go app to run inside a docker container
|
||||
# as per: https://www.fairlyusefulcode.co.uk/post/go-alpine-linux/ -- DEAD!!!
|
||||
# RUN apk upgrade musl
|
||||
# RUN apk add gcompat
|
||||
|
||||
# RUN mkdir /lib64 && ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2
|
||||
# RUN ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2
|
||||
|
||||
RUN chmod +x /app/go-pb-nurl
|
||||
|
||||
EXPOSE 7000
|
||||
|
||||
CMD [ "/app/go-pb-nurl", "serve", "--http=0.0.0.0:7000", "--dev"]
|
||||
|
48
main.go
48
main.go
@ -98,7 +98,7 @@ func main() {
|
||||
log.Println("ERROR!! postshort save NewRecord")
|
||||
log.Println(err)
|
||||
|
||||
return err
|
||||
// return err
|
||||
}
|
||||
|
||||
}
|
||||
@ -111,6 +111,10 @@ func main() {
|
||||
e.Router.GET("/*", apis.StaticDirectoryHandler(os.DirFS("./pb_public"), false))
|
||||
e.Router.GET("/build/*", apis.StaticDirectoryHandler(os.DirFS("./pb_public/build"), false))
|
||||
|
||||
e.Router.GET("/favicon.png", func(c echo.Context) error {
|
||||
return c.File("/pb_public/favicon.png")
|
||||
})
|
||||
|
||||
e.Router.GET("/api/v1/list", func(c echo.Context) error {
|
||||
|
||||
apis.ActivityLogger(app)
|
||||
@ -300,7 +304,7 @@ func getEncoded(c echo.Context, app *pocketbase.PocketBase) error {
|
||||
}
|
||||
|
||||
func findById(id int, app *pocketbase.PocketBase) (string, error) {
|
||||
record, recErr := app.Dao().FindRecordsByExpr("urls", dbx.NewExp("_id = {:id}", dbx.Params{"id": id}))
|
||||
record, recErr := app.Dao().FindRecordsByExpr("urls", dbx.NewExp("_id = {:id} and deleted = false", dbx.Params{"id": id}))
|
||||
|
||||
if recErr != nil {
|
||||
log.Println(recErr)
|
||||
@ -313,6 +317,15 @@ func findById(id int, app *pocketbase.PocketBase) (string, error) {
|
||||
|
||||
longUrl := rec.GetString("long_url")
|
||||
|
||||
go func() {
|
||||
err := UpdateReadCount(app, id)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
}
|
||||
}()
|
||||
|
||||
return longUrl, nil
|
||||
}
|
||||
|
||||
@ -332,3 +345,34 @@ func GetJSONRawBody(c echo.Context) UrlRequest {
|
||||
|
||||
return urlBody
|
||||
}
|
||||
|
||||
func UpdateReadCount(app *pocketbase.PocketBase, id int) error {
|
||||
|
||||
log.Println("$$:UpdateReadCount", id)
|
||||
|
||||
record, recErr := app.Dao().FindRecordsByExpr("urls", dbx.NewExp("_id = {:id}", dbx.Params{"id": id}))
|
||||
|
||||
if recErr != nil {
|
||||
log.Println(recErr)
|
||||
return recErr
|
||||
}
|
||||
|
||||
if len(record) == 0 {
|
||||
return errors.New("record not found")
|
||||
} else {
|
||||
log.Println("-- Update url record", record)
|
||||
|
||||
rec := record[0]
|
||||
visits := rec.GetInt("visits") + 1
|
||||
|
||||
rec.Set("visits", visits)
|
||||
|
||||
if err := app.Dao().SaveRecord(rec); err != nil {
|
||||
log.Println("ERROR!! UpdateReadCount SaveRecord")
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
BIN
pb_data/data.db
BIN
pb_data/data.db
Binary file not shown.
BIN
pb_data/logs.db
BIN
pb_data/logs.db
Binary file not shown.
File diff suppressed because one or more lines are too long
BIN
pb_public/build/favicon.png
Normal file
BIN
pb_public/build/favicon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 13 KiB |
BIN
pb_public/favicon.ico
Normal file
BIN
pb_public/favicon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.1 KiB |
@ -6,8 +6,8 @@
|
||||
|
||||
<title>nUrl</title>
|
||||
|
||||
<link rel='icon' type='image/png' href='/favicon.png'>
|
||||
<link rel='stylesheet' href='/global.css'>
|
||||
<link rel='icon' type='image/png' href='/build/favicon.png'>
|
||||
<link rel='stylesheet' href='/build/global.css'>
|
||||
<link rel='stylesheet' href='/build/bundle.css'>
|
||||
|
||||
<script defer src='/build/bundle.js'></script>
|
||||
|
Loading…
Reference in New Issue
Block a user