From 5535fdd1643a39397c78c9683709795ca10f96d2 Mon Sep 17 00:00:00 2001 From: iratusmachina Date: Tue, 31 Dec 2024 11:58:54 -0500 Subject: [PATCH] Adding sample folder for use with the README.md. --- Dockerfile | 62 ++++++++++++++++++++++++++ README.md | 20 +++++++-- sample/config.json | 4 +- sample/gocustomurls.service | 11 ----- sample/plain/commands.md | 37 +++++++++++++++ sample/plain/gocustomurls.service | 15 +++++++ sample/rules.json | 14 ++++++ sample/withDocker/Dockerfile.1 | 62 ++++++++++++++++++++++++++ sample/withDocker/commands.md | 44 ++++++++++++++++++ sample/withDocker/docker-compose.yml | 21 +++++++++ sample/withDocker/gocustomurls.service | 18 ++++++++ 11 files changed, 291 insertions(+), 17 deletions(-) create mode 100644 Dockerfile delete mode 100644 sample/gocustomurls.service create mode 100644 sample/plain/commands.md create mode 100644 sample/plain/gocustomurls.service create mode 100644 sample/rules.json create mode 100644 sample/withDocker/Dockerfile.1 create mode 100644 sample/withDocker/commands.md create mode 100644 sample/withDocker/docker-compose.yml create mode 100644 sample/withDocker/gocustomurls.service diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2f0127b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,62 @@ +# syntax=docker/dockerfile:1.4 +FROM golang:1.20-alpine AS build + +WORKDIR /app + +RUN < ``` -A sample config file is located [here](./sample/config.json) +A sample config file is located [here](./sample/config.json) and a sample rules file is located [here](./sample/rules.json) ## SystemD -A sample systemd file is located [here](./sample/gocustomurls.service) +A sample systemd file is located [here](./sample/plain/gocustomurls.service). You should run this set of commands from [here](./sample/plain/commands.md) + + +## Docker + +A sample Dockefile for use with systemd is [here](./sample/withDocker/Dockerfile.1). You should run this set of commands from [here](./sample/withDocker/commands.md). + +If you do not want to use systemd, you can run this instead: + +```sh +$ HOME=${PWD} docker build -t appimage/gocustomurls --build-arg UID=$(id -u) --build-arg GID=$(id -g) --build-arg USERNAME=$(whoami) --build-arg PORT=7070 --no-cache --progress=plain . +$ +``` ## Testing @@ -52,9 +64,9 @@ jbowen.dev/cereal (download) * [x] ~~Figure how to use logrotate (a linux utility)~~ * [x] Figure how to do log rotation as part of this app's function * [x] Add tests -* [ ] Add systemd.service and explanation +* [x] Add systemd.service and explanation * [ ] Add Dockerfile and explanation -* [ ] Figure how to use `goreleaser` [here](https://nfpm.goreleaser.com/) to release deb and rpm packages (so basically split it, goreleaser for github and woodpecker for Woodpecker) * [ ] Add mirror to Github * [ ] Add Github action +* [ ] Figure how to use `goreleaser` [here](https://nfpm.goreleaser.com/) to release deb and rpm packages (so basically split it, goreleaser for github and woodpecker for Woodpecker) * [ ] Update README.md diff --git a/sample/config.json b/sample/config.json index 39f2a72..99f0d7f 100644 --- a/sample/config.json +++ b/sample/config.json @@ -1,6 +1,6 @@ { - "rulesPath":"HOME/rules.json", - "logPath":"HOME/app.log", + "rulesPath":"/var/lib/$MYUSER/rules.json", + "logPath":"/var/lib/$MYUSER/app.log", "compress":false, "sizeToRotate":"1kB", "port":"9999" diff --git a/sample/gocustomurls.service b/sample/gocustomurls.service deleted file mode 100644 index c7bc4ba..0000000 --- a/sample/gocustomurls.service +++ /dev/null @@ -1,11 +0,0 @@ -[Unit] -Description=A custom url mapper for go packages! -After=network-online.target - -[Service] -ExecStart=/opt/gocustomurls --conf $HOME/app.conf -Restart=always -SyslogIdentifier=gocustomurls - -[Install] -WantedBy=multi-user.target diff --git a/sample/plain/commands.md b/sample/plain/commands.md new file mode 100644 index 0000000..bba2bb8 --- /dev/null +++ b/sample/plain/commands.md @@ -0,0 +1,37 @@ +# Set of commands + +Also found [here](https://iratusmachina.com/#production-go-systemd-attempt-four) + +```sh +$ cp /usr/local/bin +$ sudo mkdir -p /var/lib/ +$ sed -i 's/$MYUSER//g' gocustomurls.service +$ sed -i 's/$MYUSER//g' config.json +$ sudo cp gocustomurls.service /etc/systemd/system/ +$ sudo useradd --system --comment 'Go Custom Urls Service' --no-create-home +useradd: failed to reset the lastlog entry of UID 992: No such file or directory +$ sudo passwd -d +passwd: password changed. +$ getent passwd +:x:992:991:Go Custom Urls Service:/home/:/bin/bash +$ sudo cp config.json /var/lib/ +$ sudo cp rules.json /var/lib/ +$ sudo ls /var/lib// +config.json rules.json +$ sudo chmod -R 770 /var/lib/ +$ sudo chown -R : /var/lib/ +$ sudo systemctl daemon-reload +$ sudo systemctl start gocustomurls.service +$ sudo systemctl status gocustomurls.service +● gocustomurls.service - GocustomUrls. A custom url mapper for go packages! + Loaded: loaded (/etc/systemd/system/gocustomurls.service; disabled; preset: disabled) + Drop-In: /usr/lib/systemd/system/service.d + └─10-timeout-abort.conf + Active: active (running) since Sat 2024-07-20 06:52:09 UTC; 23s ago + Main PID: 4020 (gocustomurls) + Tasks: 6 (limit: 2319) + Memory: 7.1M (peak: 7.5M) + CPU: 14ms + CGroup: /system.slice/gocustomurls.service + └─4020 /usr/local/bin/gocustomurls -conf /var/lib//config.json +``` diff --git a/sample/plain/gocustomurls.service b/sample/plain/gocustomurls.service new file mode 100644 index 0000000..8479001 --- /dev/null +++ b/sample/plain/gocustomurls.service @@ -0,0 +1,15 @@ +[Unit] +Description=GocustomUrls. A custom url mapper for go packages! +After=network-online.target + +[Service] +WorkingDirectory=/var/lib/$MYUSER +Type=simple +User=$MYUSER +Group=$MYUSER +ExecStart=/usr/local/bin/gocustomurls -conf /var/lib/$MYUSER/config.json +SyslogIdentifier=gocustomurls +StandardError=journal + +[Install] +WantedBy=multi-user.target diff --git a/sample/rules.json b/sample/rules.json new file mode 100644 index 0000000..f9e08dc --- /dev/null +++ b/sample/rules.json @@ -0,0 +1,14 @@ +{ + "mappings": [ + { + "vanity_url":"scale.dev/x/migrate", + "protocol":"git", + "real_url":"https://codeberg.org/Gusted/mCaptcha.git" + }, + { + "vanity_url":"localhost:7070/x/touche", + "protocol":"git", + "real_url":"https://github.com/dariubs/GoBooks.git" + } + ] +} diff --git a/sample/withDocker/Dockerfile.1 b/sample/withDocker/Dockerfile.1 new file mode 100644 index 0000000..f0cdff3 --- /dev/null +++ b/sample/withDocker/Dockerfile.1 @@ -0,0 +1,62 @@ +# syntax=docker/dockerfile:1.4 +FROM golang:1.20-alpine AS build + +WORKDIR /app + +RUN < +useradd: failed to reset the lastlog entry of UID 992: No such file or directory +$ sudo passwd -d +passwd: password changed. +$ getent passwd +:x:992:991:Go Custom Urls Service:/home/:/bin/bash +$ sudo mkdir -p /var/lib//appfiles/otherfiles +$ sudo cp docker-compose.yml /var/lib//appfiles/ +$ sudo cp Dockerfile.1 /var/lib//appfiles/Dockerfile +$ sed -i 's/$MYUSER//g' gocustomurls.service +$ sed -i 's/$MYUSER//g' config.json +$ sudo cp config.json /var/lib//appfiles/othefiles +$ sudo cp rules.json /var/lib//appfiles/otherfiles +$ sudo chmod -R 770 /var/lib/ +$ sudo chown -R : /var/lib/ +$ sudo cp gocustomurls.service /etc/systemd/system/ +$ sudo systemctl daemon-reload +$ sudo systemctl start gocustomurls.service +● gocustomurls.service - GocustomUrls. A custom url mapper for go packages! + Loaded: loaded (/etc/systemd/system/gocustomurls.service; disabled; preset: disabled) + Drop-In: /usr/lib/systemd/system/service.d + └─10-timeout-abort.conf + Active: active (exited) since Sat 2024-08-03 18:08:46 UTC; 50s ago + Process: 24117 ExecStart=/bin/bash -c HOME=/var/lib/ USER_ID=$(id -u) GROUP_ID=$(id -g) USERNAME=$(whoami) docker compose --file /var/lib//docker-compose.yml up --detach --force-recreate (code=exited, status=0/SUCCESS) + Main PID: 24117 (code=exited, status=0/SUCCESS) + CPU: 125ms + +Aug 03 18:08:45 fedoramachine gocustomurls[24132]: #17 DONE 0.0s +Aug 03 18:08:45 fedoramachine gocustomurls[24132]: #18 [app] resolving provenance for metadata file +Aug 03 18:08:45 fedoramachine gocustomurls[24132]: #18 DONE 0.0s +Aug 03 18:08:45 fedoramachine gocustomurls[24132]: Network _default Creating +Aug 03 18:08:45 fedoramachine gocustomurls[24132]: Network _default Created +Aug 03 18:08:45 fedoramachine gocustomurls[24132]: Container gocustomurls_app Creating +Aug 03 18:08:45 fedoramachine gocustomurls[24132]: Container gocustomurls_app Created +Aug 03 18:08:45 fedoramachine gocustomurls[24132]: Container gocustomurls_app Starting +Aug 03 18:08:46 fedoramachine gocustomurls[24132]: Container gocustomurls_app Started +Aug 03 18:08:46 fedoramachine systemd[1]: Finished gocustomurls.service - GocustomUrls. A custom url mapper for go packages!. + +``` diff --git a/sample/withDocker/docker-compose.yml b/sample/withDocker/docker-compose.yml new file mode 100644 index 0000000..51d31be --- /dev/null +++ b/sample/withDocker/docker-compose.yml @@ -0,0 +1,21 @@ + +services: + app: + build: + context: . + dockerfile: Dockerfile + args: + - USERNAME=${USERNAME} + - UID=${USER_ID} + - GID=${GROUP_ID} + - PORT=${PORT:-7070} + labels: + - "maintainer=iratusmachina" + image: appimage/gocustomurls + logging: + driver: journald + container_name: gocustomurls_app + ports: + - "7070:7070" + volumes: + - ${PWD}/otherfiles:/home/${USERNAME} diff --git a/sample/withDocker/gocustomurls.service b/sample/withDocker/gocustomurls.service new file mode 100644 index 0000000..9f7c826 --- /dev/null +++ b/sample/withDocker/gocustomurls.service @@ -0,0 +1,18 @@ +[Unit] +Description=GocustomUrls. A custom url mapper for go packages! +After=docker.service +PartOf=docker.service + +[Service] +Type=oneshot +RemainAfterExit=yes +WorkingDirectory=/var/lib/$MYUSER +User=$MYUSER +Group=$MYUSER +ExecStart=/bin/bash -c "HOME=/var/lib/$MYUSER USER_ID=$(id -u) GROUP_ID=$(id -g) USERNAME=$(whoami) docker compose --file /var/lib/$MYUSER/docker-compose.yml up --detach --force-recreate" +ExecStop=/bin/bash -c "HOME=/var/lib/$MYUSER USER_ID=$(id -u) GROUP_ID=$(id -g) USERNAME=$(whoami) docker compose --file /var/lib/$MYUSER/docker-compose.yml down -v" +SyslogIdentifier=gocustomurls +StandardError=journal + +[Install] +WantedBy=multi-user.target \ No newline at end of file