first commit
All checks were successful
Build docker container / Build image (push) Successful in 1m4s

This commit is contained in:
Mans Ziesel 2024-09-06 19:45:20 +02:00
commit 7780311ea6
7 changed files with 177 additions and 0 deletions

51
.air.toml Normal file
View File

@ -0,0 +1,51 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"
[build]
args_bin = []
bin = "/usr/bin/make run"
cmd = "/usr/bin/make build"
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata", "build"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = ["src"]
include_ext = ["go", "tpl", "tmpl", "html"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
post_cmd = []
pre_cmd = []
rerun = false
rerun_delay = 500
send_interrupt = false
stop_on_error = false
[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"
[log]
main_only = false
time = false
[misc]
clean_on_exit = false
[proxy]
app_port = 0
enabled = false
proxy_port = 0
[screen]
clear_on_rebuild = false
keep_scroll = true

View File

@ -0,0 +1,31 @@
name: Build docker container
on:
push:
branches:
- main
jobs:
build:
name: Build image
runs-on: ubuntu-latest
container: ghcr.io/catthehacker/ubuntu:act-latest
env:
IMAGE_NAME: goredire
REGISTRY: git.mziesel.nl
REPO_OWNER: mans
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
registry: git.mziesel.nl
username: ${{ secrets.LOGIN_USER }}
password: ${{ secrets.LOGIN_PASS }}
- name: Build and push
run: |
TODAY=$(date +'%Y-%m-%d')
docker build -t ${REGISTRY}/${REPO_OWNER}/${IMAGE_NAME}:${TODAY} -t ${REGISTRY}/${REPO_OWNER}/${IMAGE_NAME}:latest .
docker push ${REGISTRY}/${REPO_OWNER}/${IMAGE_NAME}:${TODAY}
docker push ${REGISTRY}/${REPO_OWNER}/${IMAGE_NAME}:latest

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
build
tmp

32
Dockerfile Normal file
View File

@ -0,0 +1,32 @@
# Stage 1: Build the Go application
FROM golang:1.23 AS builder
# Set the Current Working Directory inside the container
WORKDIR /app
# Copy go mod and sum files
COPY go.mod ./
# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed
RUN go mod download
# Copy the source code into the container
COPY . .
# Build the Go app
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o server .
# Stage 2: Create a small image with just the compiled binary
FROM alpine:latest
# Set the Current Working Directory inside the container
WORKDIR /root/
# Copy the pre-built binary file from the previous stage
COPY --from=builder /app/server .
# Expose the port that the application will run on
EXPOSE 8080
# Command to run the executable
CMD ["./server"]

25
Makefile Normal file
View File

@ -0,0 +1,25 @@
IMAGE_NAME=goredir
CONTAINER_NAME=goredir
build: *.go
mkdir -p build
go build -o build/goredir main.go
run: build
./build/goredir
clean:
rm -r build
package:
docker-build:
docker build -t $(IMAGE_NAME) .
docker-run:
docker run -d -p 8880:8080 --name $(CONTAINER_NAME) $(IMAGE_NAME)
docker-clean:
docker rmi $(IMAGE_NAME)
.PHONY: clean run docker-run docker-build docker-clean

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.mzsl.nl/mans/goredire
go 1.23.0

33
main.go Normal file
View File

@ -0,0 +1,33 @@
package main
import (
"fmt"
"net/http"
"strings"
)
func greet(w http.ResponseWriter, r *http.Request) {
// Check if the Host header ends with "mzsl.nl"
if !strings.HasSuffix(r.Host, "mzsl.nl") {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("404, url not found"))
return
}
// Replace "localhost" with "mziesel.nl" in the Host header
newHost := strings.Replace(r.Host, "localhost", "mziesel.nl", 1)
// Construct the new URL for redirection
newUrl := "https://" + newHost + r.URL.String()
fmt.Println(newUrl)
// Set the Location header and respond with a 301 Moved Permanently status
w.Header().Set("Location", newUrl)
w.WriteHeader(http.StatusMovedPermanently)
w.Write([]byte("url found, redirecting..."))
}
func main() {
http.HandleFunc("/", greet)
http.ListenAndServe(":8080", nil)
}