Compare commits

...

6 Commits

10 changed files with 151 additions and 23 deletions

40
.github/workflows/lint_and_test.yml vendored Normal file
View File

@ -0,0 +1,40 @@
name: Lint and test yml
on: [push]
permissions:
contents: read
jobs:
lint_and_test:
# if: ${{ !github.event.act }} # Skip during local actions testing
if: github.repository == 'garrancode/gocustomurls'
runs-on: [ubuntu-latest, self-hosted]
# runs-on: [ubuntu-latest]
strategy:
matrix:
# Uses some of the solutions found here (https://github.com/actions/setup-go/issues/450)
go-version: ['', 'oldstable', 'stable']
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # git show-ref v4.2.2
with:
fetch-depth: 0
persist-credentials: false # https://yossarian.net/til/post/actions-checkout-can-leak-github-credentials/
- name: Setup Go version ${{ matrix.go-version }}
uses: actions/setup-go@0aaccfd150d50ccaeb58ebd88d36e91967a5f35b # git show-ref v5.4.0
with:
# Uses the condition found in here (https://github.com/actions/setup-go?tab=readme-ov-file#getting-go-version-from-the-gomod-file)
go-version: ${{ matrix.go-version }}
go-version-file: 'go.mod'
- name: Lint with ${{ matrix.go-version }}
uses: golangci/golangci-lint-action@8564da7cb3c6866ed1da648ca8f00a258ef0c802 # git show-ref v6.5.2
with:
args: --timeout=3m --verbose
- name: Test with ${{ matrix.go-version }}
run: |
make coverage-full

40
.github/workflows/local.yml vendored Normal file
View File

@ -0,0 +1,40 @@
name: Local workflow for act
on: [workflow_dispatch]
permissions:
contents: read
jobs:
local:
runs-on: ubuntu-latest
# image to run with act
container:
# image url (https://github.com/catthehacker/docker_images/pkgs/container/ubuntu/405021598?tag=act-latest)
image: ghcr.io/catthehacker/ubuntu:act-latest-20250429@sha256:ed7b477aa8ba68efd4ebdd6c2cc00728cfecf9780babb04383f481201641e793
strategy:
matrix:
# Uses some of the solutions found here (https://github.com/actions/setup-go/issues/450)
go-version: ['']
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # git show-ref v4.2.2
with:
fetch-depth: 0
- name: Setup Go version ${{ matrix.go-version }}
uses: actions/setup-go@0aaccfd150d50ccaeb58ebd88d36e91967a5f35b # git show-ref v5.4.0
with:
# Uses the condition found in here (https://github.com/actions/setup-go?tab=readme-ov-file#getting-go-version-from-the-gomod-file)
# go-version: ${{ matrix.go-version }}
go-version-file: 'go.mod'
- name: Lint with ${{ matrix.go-version }}
uses: golangci/golangci-lint-action@8564da7cb3c6866ed1da648ca8f00a258ef0c802 # git show-ref v6.5.2
with:
args: --timeout=3m --verbose
- name: Test with ${{ matrix.go-version }}
run: |
make coverage-full

View File

@ -65,8 +65,8 @@ jbowen.dev/cereal (download)
* [x] Figure how to do log rotation as part of this app's function
* [x] Add tests
* [x] Add systemd.service and explanation
* [ ] Add Dockerfile and explanation
* [ ] Add mirror to Github
* [ ] Add Github action
* [x] Add Dockerfile and explanation
* [x] Add mirror to Github
* [ ] Add Github action (self-hosted, [act](https://github.com/nektos/act), Github)
* [ ] 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

View File

@ -9,7 +9,10 @@ import (
)
func healthcheck(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
_, err := w.Write([]byte("ok"))
if err != nil {
fmt.Printf("Error writing ok: %+v", err)
}
}
func reloadRules(c *Config) http.HandlerFunc {
@ -20,7 +23,12 @@ func reloadRules(c *Config) http.HandlerFunc {
http.Error(w, e.Error(), http.StatusInternalServerError)
return
}
w.Write([]byte("ok"))
_, err = w.Write([]byte("ok"))
if err != nil {
e := fmt.Errorf("cannot reload rules: %+v", err)
http.Error(w, e.Error(), http.StatusInternalServerError)
return
}
}
}
@ -39,7 +47,11 @@ func serveRules(c *Config) http.HandlerFunc {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(data)
_, err = w.Write(data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
}
@ -71,7 +83,11 @@ func serveRules(c *Config) http.HandlerFunc {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(data)
_, err = w.Write(data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
}
@ -94,6 +110,10 @@ func serveRules(c *Config) http.HandlerFunc {
}
w.Header().Set("Cache-Control", "public, max-age=500")
w.Write(buf.Bytes())
_, err = w.Write(buf.Bytes())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}

View File

@ -52,7 +52,12 @@ func main() {
confFlag := flags.String("conf", "", "Required. Contains all the configurations options")
flags.Parse(os.Args[1:])
err := flags.Parse(os.Args[1:])
if err != nil {
errorLog.Println("Error: cannot parse command-line arguments")
flags.Usage()
os.Exit(1)
}
if len(flags.Args()) > 1 {
errorLog.Println("Error: too many command-line arguments")

View File

@ -1,6 +1,7 @@
package main
import (
"fmt"
"net/http"
)
@ -9,7 +10,10 @@ import (
func serveLogger(l *LogFile) func(http.HandlerFunc) http.HandlerFunc {
return func(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
l.WriteLog(r)
err := l.WriteLog(r)
if err != nil {
fmt.Printf("Error writing log : %+v", err)
}
next(w, r)
}
}

View File

@ -3,10 +3,15 @@
Also found [here](https://iratusmachina.com/#production-go-systemd-attempt-four)
```sh
$ cp <BINARY> /usr/local/bin
$ git clone https://git.iratusmachina.com/iratusmachina/gocustomurls.git gurls
$ cd gurls
$ make build
$ sudo cp <BINARY> /usr/local/bin
$ sudo mkdir -p /var/lib/<your user>
$ sed -i 's/$MYUSER/<your user>/g' gocustomurls.service
$ sed -i 's/$MYUSER/<your user>/g' config.json
$ // change your port to a port you want, say 9090
$ sudo cp gocustomurls.service /etc/systemd/system/
$ sudo useradd --system --comment 'Go Custom Urls Service' --no-create-home <your user>
useradd: failed to reset the lastlog entry of UID 992: No such file or directory
@ -34,4 +39,11 @@ $ sudo systemctl status gocustomurls.service
CPU: 14ms
CGroup: /system.slice/gocustomurls.service
└─4020 /usr/local/bin/gocustomurls -conf /var/lib/<your user>/config.json
$ http --body "http://<vanity_url>?go-get=1"
<html>
<head>
...
</head>
</html>
```

View File

@ -3,23 +3,30 @@
Also found [here](https://iratusmachina.com/#production-go-systemd-attempt-five)
```sh
$ git clone https://git.iratusmachina.com/iratusmachina/gocustomurls.git
$ sudo useradd --system --comment 'Go Custom Urls Service' --no-create-home <your user>
useradd: failed to reset the lastlog entry of UID 992: No such file or directory
$ sudo passwd -d <your user>
passwd: password changed.
$ getent passwd <your user>
<your user>:x:992:991:Go Custom Urls Service:/home/<your user>:/bin/bash
$ sudo mkdir -p /var/lib/<your user>/appfiles/otherfiles
$ sudo cp docker-compose.yml /var/lib/<your user>/appfiles/
$ sudo cp Dockerfile.1 /var/lib/<your user>/appfiles/Dockerfile
$ sudo mkdir -p /var/lib/<your user>/appfiles
$ // change your port in docker-compose.yml to a port you want, say 9090
$ sudo cp docker-compose.yml /var/lib/<your user>
$ sudo cp Dockerfile.1 /var/lib/<your user>/Dockerfile
$ sed -i 's/$MYUSER/<your user>/g' gocustomurls.service
$ sed -i 's/$MYUSER/<your user>/g' config.json
$ sudo cp config.json /var/lib/<your user>/appfiles/othefiles
$ sudo cp rules.json /var/lib/<your user>/appfiles/otherfiles
$ // change your port in config.json to a port you want, say 9090
$ sudo cp config.json /var/lib/<your user>/appfiles
$ sudo cp rules.json /var/lib/<your user>/appfiles
$ sudo chmod -R 770 /var/lib/<your user>
$ sudo chown -R <your user>:<your user> /var/lib/<your user>
$ sudo cp gocustomurls.service /etc/systemd/system/
$ sudo systemctl daemon-reload
$ // You would get an error with permissions like below
$ // denied while trying to connect to the Docker daemon socket at unix://var/run/docker.sock
$ // solve with the command below
$ sudo usermod -aG docker <your user>
$ 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)

View File

@ -18,4 +18,4 @@ services:
ports:
- "7070:7070"
volumes:
- ${PWD}/otherfiles:/home/${USERNAME}
- ${PWD}/appfiles:/home/${USERNAME}

View File

@ -113,12 +113,12 @@ func walkMatch(t *testing.T, root, pattern string) []string {
// return !errors.Is(err, fs.ErrNotExist)
// }
func removeFileForTest(t *testing.T, name string) {
err := os.Remove(name)
if err != nil {
t.Fatal(err)
}
}
// func removeFileForTest(t *testing.T, name string) {
// err := os.Remove(name)
// if err != nil {
// t.Fatal(err)
// }
// }
func writeJsonForTest(t *testing.T, data map[string]any, fp string) {
jsonString, _ := json.Marshal(data)