Skip to content

Commit

Permalink
fix: missed dependencies in README
Browse files Browse the repository at this point in the history
  • Loading branch information
alxarno committed Jan 13, 2025
1 parent 2e314a6 commit 1bb3caf
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 121 deletions.
93 changes: 22 additions & 71 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,102 +1,53 @@
ME = $(lastword $(MAKEFILE_LIST))
.DEFAULT_GOAL := help
.PHONY: help
help: # prints this help
@bash -c "$$AUTOGEN_HELP_BASH" < $(ME)
.PHONY: run watch clean test ubuntu coverage lint quality fmt

BINARY_NAME=tinytune
VERSION=0.0.1

COMMIT_HASH=$(shell git rev-parse --short HEAD)
BUILD_TIMESTAMP=$(shell date '+%Y-%m-%dT%H:%M:%S')

LDFLAGS=-ldflags "-X 'main.Version=${VERSION}' -X 'main.CommitHash=${COMMIT_HASH}' -X 'main.BuildTimestamp=${BUILD_TIMESTAMP}' -X 'main.Mode=Production'"

CGO_LDFLAGS=-ljemalloc
CGO_CFLAGS=-fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free

.PHONY: build
build: ## build executables
echo "Building frontend assets"
GO=CGO_CFLAGS="${CGO_CFLAGS}" CGO_LDFLAGS="${CGO_LDFLAGS}" go

LINUX_AMD64 = out/${BINARY_NAME}_linux_amd64
WEB_ASSETS = web/assets/index.min.js

RUN_FOLDER = ./test/

build: ${LINUX_AMD64}

${WEB_ASSETS}:
make -C ./web build
mkdir -p out/
echo "Building executable"
GOARCH=amd64 \
GOOS=linux \
CGO_CFLAGS="${CGO_CFLAGS}" \
CGO_LDFLAGS="${CGO_LDFLAGS}" \
go build ${LDFLAGS} -o out/${BINARY_NAME}_linux_amd64 cmd/tinytune/tinytune.go
chmod +x out/${BINARY_NAME}_linux_amd64
echo "Done"

.PHONY: run

${LINUX_AMD64}: ${WEB_ASSETS}
GOARCH=amd64 GOOS=linux ${GO} build ${LDFLAGS} -o ${LINUX_AMD64} cmd/tinytune/tinytune.go

run: ## run tinytune server
CGO_CFLAGS="${CGO_CFLAGS}" CGO_LDFLAGS="${CGO_LDFLAGS}" go run cmd/tinytune/tinytune.go ./test/
${GO} run cmd/tinytune/tinytune.go "${RUN_FOLDER}"

.PHONY: watch
watch: ## run tinytune server and frontend in hot-reload way
reflex -r '\.(html|go)$\' -s make run & make -C ./web watch

.PHONY: clean
clean: ## clean
clear: ## clean
go clean
rm -rf out/
rm -f coverage*.out
make -C ./web clear

.PHONY: test
test: ## run server tests
go test -timeout 2m -race -failfast ./...

.PHONY: ubuntu
ubuntu: ## Install deps for ubuntu (libvips, ffmpeg)
sudo apt install build-essential libvips pkg-config libvips-dev libjemalloc-dev ffmpeg -y
npm i --prefix ./web

.PHONY: coverage
coverage: ## coverage
make test
go tool cover -html=coverage.out

.PHONY: lint
lint: ## run server linting
golangci-lint run --fix

.PHONY: quality
quality: ## check-quality
make fmt
make lint

.PHONY: fmt
fmt: ## run server prettyfier
go fmt ./...

$(VERBOSE).SILENT:


define AUTOGEN_HELP_BASH
declare -A targets; declare -a torder
targetre='^([A-Za-z]+):.* *# *(.*)'
if [[ $$TERM && $$TERM != dumb && -t 1 ]]; then
ul=$$'\e[0;4m'; bbold=$$'\e[34;1m'; reset=$$'\e[0m'
fi
if [[ -n "$(TITLE)" ]]; then
printf "\n %sMakefile targets - $(TITLE)%s\n\n" "$$ul" "$$reset"
else
printf "\n %sMakefile targets%s\n\n" "$$ul" "$$reset"
fi
while read -r line; do
if [[ $$line =~ $$targetre ]]; then
target=$${BASH_REMATCH[1]}; help=$${BASH_REMATCH[2]}
torder+=("$$target")
targets[$$target]=$$help
if (( $${#target} > max )); then max=$${#target}; fi
fi
done
for t in "$${torder[@]}"; do
printf " %smake %-*s%s %s\n" "$$bbold" $$max "$$t" "$$reset" \
"$${targets[$$t]}"
done
if [[ -n "$(HOMEPAGE)" ]]; then
printf "\n Homepage:\n $(HOMEPAGE)\n\n"
else
printf "\n"
fi
endef
export AUTOGEN_HELP_BASH
quality: fmt lint ## check-quality
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ The latest version of the TinyTune can be found on the GitHub [releases page](ht
### Linux
```
# check that you have ffmpeg installed
ffmpeg -v
# install dependencies
apt install ffmpeg libvips libjemalloc2
wget https://github.com/alxarno/tinytune/releases/download/latest/tinytune_linux_amd64
Expand Down
2 changes: 1 addition & 1 deletion internal/crawler_os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ func TestCrawlerOS(t *testing.T) {

files, err := NewCrawlerOS("../test").Scan("../test/index.tinytune")
require.NoError(err)
require.Len(files, 20)
require.Len(files, 21)
}
8 changes: 7 additions & 1 deletion pkg/index/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,13 @@ func (m *Meta) setContentType() {
videoFormats := []string{"3gp", "avi", "f4v", "flv", "hevc", "m4v", "mlv", "mov", "mp4", "m4a", "3g2", "mj2", "mpeg", "ogv", "webm", "wmv"}
imageFormats := []string{"jpeg", "png", "jpg", "webp", "bmp", "gif"}

ext := strings.ToLower(filepath.Ext(string(m.AbsolutePath)))[1:]
ext := strings.ToLower(filepath.Ext(string(m.AbsolutePath)))
ext = strings.TrimPrefix(ext, ".")

if ext == "" {
return
}

m.Extension = ext

switch {
Expand Down
9 changes: 7 additions & 2 deletions pkg/preview/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sync/atomic"
"time"

"github.com/alxarno/tinytune/pkg/throttle"
"github.com/davidbyttow/govips/v2/vips"
)

Expand Down Expand Up @@ -176,8 +177,12 @@ func (p Previewer) Pull(ctx context.Context, src Source) (Data, error) {
if src.Size() > BigVideoSizeB {
p.bigVideoQueue <- struct{}{}

err := throttle(ctx, BigVideoThrottleMaxOccupiedPercent, BigVideoThrottleMaxWaiting)
if errors.Is(err, context.Canceled) {
throttler := throttle.NewThrottler(
BigVideoThrottleMaxOccupiedPercent,
BigVideoThrottleMaxWaiting,
)

if errors.Is(throttler.Throttle(ctx), context.Canceled) {
return defaultPreview, context.Canceled
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/preview/thottle.go → pkg/throttle/linux.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package preview
package throttle

import (
"bufio"
Expand Down Expand Up @@ -69,14 +69,14 @@ func isMemoryPassCheck(maxMemoryOccupied float64) bool {
return memoryOccupied < maxMemoryOccupied
}

func throttle(ctx context.Context, maxMemoryOccupied float64, maxWaitingTime time.Duration) error {
if isMemoryPassCheck(maxMemoryOccupied) {
func (t throttler) Throttle(ctx context.Context) error {
if isMemoryPassCheck(t.maxMemoryOccupied) {
return nil
}

ticker := time.NewTicker(time.Second)

timeout, cancel := context.WithTimeout(ctx, maxWaitingTime)
timeout, cancel := context.WithTimeout(ctx, t.maxWaitingTime)
defer cancel()

for {
Expand All @@ -88,7 +88,7 @@ func throttle(ctx context.Context, maxMemoryOccupied float64, maxWaitingTime tim

return nil
case <-ticker.C:
if isMemoryPassCheck(maxMemoryOccupied) {
if isMemoryPassCheck(t.maxMemoryOccupied) {
return nil
}
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/throttle/throttle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package throttle

import (
"context"
"time"
)

type Throttler interface {
Throttle(ctx context.Context) error
}

type throttler struct {
maxMemoryOccupied float64
maxWaitingTime time.Duration
}

//nolint:ireturn
func NewThrottler(maxMemoryOccupied float64, maxWaitingTime time.Duration) Throttler {
return &throttler{maxMemoryOccupied, maxWaitingTime}
}
1 change: 1 addition & 0 deletions test/NONAME
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NONAME TEST FILE
52 changes: 13 additions & 39 deletions web/Makefile
Original file line number Diff line number Diff line change
@@ -1,46 +1,20 @@
ME = $(lastword $(MAKEFILE_LIST))
.DEFAULT_GOAL := help
.PHONY: help
help: # prints this help
@bash -c "$$AUTOGEN_HELP_BASH" < $(ME)
.PHONY: watch clear

VENDOR = assets/vendor.bundle.js
INDEX =assets/index.min.js

$(VERBOSE).SILENT:

.PHONY: build
build: ## build all assets (.js, .css)
npm run build-all
build: ${VENDOR} ${INDEX} ## build all assets (.js, .css)

${VENDOR}:
npm run build-vendor

${INDEX}:
npm run build

.PHONY: watch
watch: ## build all assets in hot-reload way
npm run watch

define AUTOGEN_HELP_BASH
declare -A targets; declare -a torder
targetre='^([A-Za-z]+):.* *# *(.*)'
if [[ $$TERM && $$TERM != dumb && -t 1 ]]; then
ul=$$'\e[0;4m'; bbold=$$'\e[34;1m'; reset=$$'\e[0m'
fi
if [[ -n "$(TITLE)" ]]; then
printf "\n %sMakefile targets - $(TITLE)%s\n\n" "$$ul" "$$reset"
else
printf "\n %sMakefile targets%s\n\n" "$$ul" "$$reset"
fi
while read -r line; do
if [[ $$line =~ $$targetre ]]; then
target=$${BASH_REMATCH[1]}; help=$${BASH_REMATCH[2]}
torder+=("$$target")
targets[$$target]=$$help
if (( $${#target} > max )); then max=$${#target}; fi
fi
done
for t in "$${torder[@]}"; do
printf " %smake %-*s%s %s\n" "$$bbold" $$max "$$t" "$$reset" \
"$${targets[$$t]}"
done
if [[ -n "$(HOMEPAGE)" ]]; then
printf "\n Homepage:\n $(HOMEPAGE)\n\n"
else
printf "\n"
fi
endef
export AUTOGEN_HELP_BASH
clear:
rm -rf ${VENDOR} ${INDEX}

0 comments on commit 1bb3caf

Please sign in to comment.