100 lines
2.7 KiB
Markdown
100 lines
2.7 KiB
Markdown
# gitea-auto-urls
|
|
|
|
Automatically generates Traefik middleware to shorten Gitea repository URLs.
|
|
|
|
## Purpose
|
|
|
|
Removes the username from single-user Gitea instances: `https://git.pihkaal.xyz/pihkaal/repo-name` → `https://git.pihkaal.xyz/repo-name`.
|
|
Note that this will work only if you have a single user on Gitea, this is not meant to be used with multiple users. This projects is done based on my needs.
|
|
|
|
## How it works
|
|
|
|
1. Fetches all repositories from Gitea API
|
|
2. Generates Traefik rewrite middleware using a template
|
|
3. Outputs configuration to `/etc/traefik/dynamic/gitea-rewrites.yml`
|
|
4. Traefik auto-reloads when the file changes
|
|
|
|
## Quick start
|
|
|
|
```shell
|
|
$ pnpm build
|
|
$ pnpm start
|
|
```
|
|
|
|
## Traefik Configuration
|
|
|
|
Configure Traefik to watch the dynamic configuration directory:
|
|
|
|
```yaml
|
|
# add this to your traefik config to be able to put dynamic config in /etc/traefik/dynamic
|
|
providers:
|
|
file:
|
|
directory: "/etc/traefik/dynamic"
|
|
watch: true
|
|
```
|
|
|
|
## Docker Setup
|
|
|
|
You can add this to your gitea stack:
|
|
|
|
```yaml
|
|
services:
|
|
# ...
|
|
auto-urls:
|
|
image: git.pihkaal.xyz/pihkaal/gitea-auto-urls:latest
|
|
container_name: gitea-auto-urls
|
|
restart: always
|
|
environment:
|
|
- PORT=3000
|
|
- GITEA_URL
|
|
- GITEA_USERNAME
|
|
- GITEA_TOKEN
|
|
- GITEA_WEBHOOK_SECRET
|
|
- TEMPLATE_FILE=/config/gitea-rewrites.yml.in
|
|
- OUTPUT_FILE=/config/gitea-rewrites.yml
|
|
volumes:
|
|
- /etc/traefik/dynamic:/config
|
|
networks:
|
|
- gitea
|
|
```
|
|
|
|
## Configure in gitea
|
|
|
|
### Create the token
|
|
|
|
Navigate to `/user/settings/applications`, then click "Generate a new token". Only required permissions are `read:repository` and `read:user`.
|
|
|
|

|
|
|
|
### Create the webhook
|
|
|
|
Navigate to `/user/settings/hooks`, then click "Add webhook".
|
|
|
|

|
|
|
|
In "Target URL", put the server address. If you use docker and added this service to your gitea stack, just put `http://container_name:PORT` (e.g `http://gitea-auto-urls:3000`).
|
|
In "Secret", put the same secret you used for `GITEA_WEBHOOK_SECRET`. You can easily generate it using:
|
|
|
|
```shell
|
|
$ openssl rand -base64 32
|
|
```
|
|
|
|
Lastly, for the triggers select "Custom Events", then "Repository".
|
|
|
|
And you're done, just hit "Add Webhook" and the service will start working.
|
|
The Gitea webhooks interface shows you the logs, so you can see here if it reached the service.
|
|
|
|
## Template Format
|
|
|
|
You can put anything in here, `{{REPOSITORIES}}` will be pipe-separated repos list (`repo-a|repo-b`) and `{{USERNAME}}` just contain `GITEA_USERNAME`.
|
|
Minimal config: (see `gitea-rewrites.yml.in`):
|
|
|
|
```yaml
|
|
http:
|
|
middlewares:
|
|
gitea-rewrites:
|
|
replacePathRegex:
|
|
regex: "^/({{REPOSITORIES}})(.*)$"
|
|
replacement: "/{{USERNAME}}/$1$2"
|
|
```
|