Back to a plain shell script

This commit is contained in:
perennial
2024-09-23 14:14:16 +10:00
parent 48265616e4
commit 67df71a48b
7 changed files with 122 additions and 83 deletions
+6 -6
View File
@@ -2,16 +2,16 @@
# Refer to https://pixivfe-docs.pages.dev/environment-variables/ for detailed explanations of these variables
## Required variables
PIXIVFE_PORT=8282
# PIXIVFE_UNIXSOCKET=/srv/http/pages/pixivfe
# PIXIVFE_TOKEN=changethis
PIXIVFE_PORT='8282'
# PIXIVFE_UNIXSOCKET='/srv/http/pages/pixivfe'
# PIXIVFE_TOKEN='changethis'
## Optional variables
PIXIVFE_HOST=127.0.0.1
PIXIVFE_HOST='127.0.0.1'
# PIXIVFE_REQUESTLIMIT=
# PIXIVFE_IMAGEPROXY=
# PIXIVFE_USERAGENT=Mozilla/5.0 (Windows NT 10.0; rv:123.0) Gecko/20100101 Firefox/123.0
# PIXIVFE_ACCEPTLANGUAGE=en-US,en;q=0.5
# PIXIVFE_USERAGENT='Mozilla/5.0 (Windows NT 10.0; rv:123.0) Gecko/20100101 Firefox/123.0'
# PIXIVFE_ACCEPTLANGUAGE='en-US,en;q=0.5'
# PIXIVFE_PROXY_CHECK_INTERVAL=
# PIXIVFE_TOKEN_LOAD_BALANCING=
# PIXIVFE_REPO_URL=
+1 -1
View File
@@ -9,7 +9,7 @@ COPY . .
# Build the application
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg \
make build
./manage.sh build
# Stage for creating the non-privileged user
FROM alpine:3.20 AS user-stage
-60
View File
@@ -1,60 +0,0 @@
# Makefile for PixivFE
# Variables
BINARY_NAME=pixivfe
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
GIT_COMMIT_DATE := $(shell git show -s --format=%cd --date=format:"%Y.%m.%d")
GIT_COMMIT_HASH := $(shell git rev-parse --short HEAD)
REVISION := $(GIT_COMMIT_DATE)-$(GIT_COMMIT_HASH)
UNCOMMITTED_CHANGES := $(shell git status --porcelain)
ifneq ($(UNCOMMITTED_CHANGES),)
REVISION := $(REVISION)+dirty
endif
# Include environment variables from .env if it exists
-include .env
.PHONY: all fmt build test run clean install-pre-commit help
all: fmt build test
fmt:
@echo "Formatting Go code..."
go fmt ./...
build:
@echo "Building $(BINARY_NAME)..."
go mod tidy
CGO_ENABLED=0 go build -v -ldflags="-extldflags=-static -X codeberg.org/vnpower/pixivfe/v2/config.REVISION=$(REVISION)" -o $(BINARY_NAME)
test:
@echo "Running tests..."
go test ./server/template
run: build
@echo "Running $(BINARY_NAME)..."
./$(BINARY_NAME)
clean:
@echo "Cleaning up..."
rm -f $(BINARY_NAME)
# Additional target to install test script as pre-commit hook
install-pre-commit:
@echo "Installing pre-commit hook..."
echo '#!/bin/sh' > .git/hooks/pre-commit
echo 'go test ./server/template' >> .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
# Help target
help:
@echo "Available targets:"
@echo " all - Run fmt, build, and test"
@echo " fmt - Format Go code"
@echo " build - Build the binary"
@echo " test - Run tests"
@echo " run - Build and run the binary"
@echo " clean - Remove the binary"
@echo " install-pre-commit - Install testing pre-commit hook"
@echo " help - Show this help message"
+2 -10
View File
@@ -31,7 +31,6 @@ Prerequisites:
- [Go](https://go.dev/doc/install) (to build PixivFE from source)
- [Sass](https://github.com/sass/dart-sass/) (will be run by PixivFE in development mode)
- [Make](https://www.gnu.org/software/make/) (to use the Makefile)
To install Dart Sass, you can choose any of the following methods.
@@ -45,20 +44,13 @@ Then, run the project:
# Clone the PixivFE repository
git clone https://codeberg.org/VnPower/PixivFE.git && cd PixivFE
# Build the project
make build
# Run PixivFE in development mode (styles and templates reload automatically)
PIXIVFE_DEV=1 <other_environment_variables> make run
PIXIVFE_DEV=1 <other_environment_variables> ./manage.sh run
```
## Using the Makefile
The project includes a GNU Makefile to simplify common development tasks. Run `make help` to view the available commands.
## Hosting PixivFE
You can use PixivFE for personal use! Assuming that you use an operating system that can run POSIX shell scripts, install `go`, clone this repository, and use the Makefile to build and run the project.
You can use PixivFE for personal use! Assuming that you use an operating system that can run POSIX shell scripts, install `go`, clone this repository, and use the `manage.sh` shell script to build and run the project.
I recommend self-hosting your own instance for personal use, instead of relying entirely on official instances.
To deploy PixivFE using Docker or the compiled binary, see [Hosting PixivFE](https://pixivfe-docs.pages.dev/hosting-pixivfe/).
+1 -1
View File
@@ -29,7 +29,7 @@ This file contains guidelines for code development.
## Adding features
- Edit the `Makefile` if you want to create scripts that help with the process of developing and/or using PixivFE.
- Edit the `manage.sh` if you want to create scripts that help with the process of developing and/or using PixivFE.
- Don't create or add any shell files or configuration files of external programs in the root directory unless necessary.
- Add comments where necessary to explain complex logic or non-obvious code behavior.
+4 -5
View File
@@ -44,7 +44,7 @@ Copy `.env.example` to `.env` and configure the variables as needed. Refer to th
!!! note
Ensure you set `PIXIVFE_HOST=0.0.0.0` in the `.env` file.
This allows PixivFE to bind to all network interfaces inside the container, which is necessary for Docker's network management to function correctly. The network access restrictions will be handled by Docker itself, not within PixivFE.
#### 3. Set token
@@ -73,7 +73,7 @@ Copy `.env.example` to `.env` and configure the variables as needed. Refer to th
!!! note
Ensure you set `PIXIVFE_HOST=0.0.0.0` in the `.env` file.
This allows PixivFE to bind to all network interfaces inside the container, which is necessary for Docker's network management to function correctly. The network access restrictions will be handled by Docker itself, not within PixivFE.
#### 3. Deploying PixivFE
@@ -112,13 +112,12 @@ Copy `.env.example` to `.env` and configure the variables as needed. Refer to th
### 3. Building and running PixivFE
PixivFE uses a [Makefile](https://www.gnu.org/software/make/manual/make.html#Introduction) to simplify the build and run process.
PixivFE provides a shell script named `manage.sh` to simplify the build and run process.
To build and run PixivFE, use the following commands:
```bash
make build
make run
./manage.sh run
```
This will build the PixivFE binary and start it. It will be accessible at `localhost:8282`.
Executable
+108
View File
@@ -0,0 +1,108 @@
#!/bin/sh
# Variables
BINARY_NAME="pixivfe"
GOOS=${GOOS:-$(go env GOOS)}
GOARCH=${GOARCH:-$(go env GOARCH)}
GIT_COMMIT_DATE=$(git show -s --format=%cd --date=format:"%Y.%m.%d")
GIT_COMMIT_HASH=$(git rev-parse --short HEAD)
REVISION="${GIT_COMMIT_DATE}-${GIT_COMMIT_HASH}"
UNCOMMITTED_CHANGES=$(git status --porcelain)
if [ -n "$UNCOMMITTED_CHANGES" ]; then
REVISION="${REVISION}+dirty"
fi
fmt() {
echo "Formatting Go code..."
go fmt ./...
}
build() {
echo "Building ${BINARY_NAME}..."
go mod tidy
CGO_ENABLED=0 go build -v -ldflags="-extldflags=-static -X codeberg.org/vnpower/pixivfe/v2/config.REVISION=${REVISION}" -o "${BINARY_NAME}"
}
test() {
echo "Running tests..."
go test ./server/template
}
run() {
build
echo "Running ${BINARY_NAME}..."
if [ "$1" != "--do-not-load-env-file" ] && [ -f .env ]; then
echo ".env file found, loading environment variables"
set -a
. ./.env
set +a
else
echo "Not loading .env file"
fi
./"${BINARY_NAME}"
}
clean() {
echo "Cleaning up..."
rm -f "${BINARY_NAME}"
}
install_pre_commit() {
echo "Installing pre-commit hook..."
echo '#!/bin/sh' > .git/hooks/pre-commit
echo 'go test ./server/template' >> .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
}
help() {
echo "Available commands:"
echo " all - Run fmt, build, and test"
echo " fmt - Format Go code"
echo " build - Build the binary"
echo " test - Run tests"
echo " run [--do-not-load-env-file] - Build and run the binary"
echo " clean - Remove the binary"
echo " install-pre-commit - Install testing pre-commit hook"
echo " help - Show this help message"
echo ""
echo "Options:"
echo " --do-not-load-env-file - Do not load the .env file when running"
}
all() {
fmt
build
test
}
# Function to handle command execution
execute_command() {
case "$1" in
fmt) fmt ;;
build) build ;;
test) test ;;
run)
if [ "$2" = "--do-not-load-env-file" ]; then
run "--do-not-load-env-file"
else
run
fi
;;
clean) clean ;;
install-pre-commit) install_pre_commit ;;
help) help ;;
all) all ;;
*)
echo "Unknown command: $1"
echo "Use 'help' to see available commands"
exit 1
;;
esac
}
# Main execution
if [ $# -eq 0 ]; then
help
else
execute_command "$@"
fi