commit 7780311ea6b9200dd4e37e4362b3cc5eae4064e2 Author: Mans Ziesel Date: Fri Sep 6 19:45:20 2024 +0200 first commit diff --git a/.air.toml b/.air.toml new file mode 100644 index 0000000..6bdba5b --- /dev/null +++ b/.air.toml @@ -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 diff --git a/.gitea/workflows/release.yaml b/.gitea/workflows/release.yaml new file mode 100644 index 0000000..f4bb836 --- /dev/null +++ b/.gitea/workflows/release.yaml @@ -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 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d59a62b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build +tmp diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3281165 --- /dev/null +++ b/Dockerfile @@ -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"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b8f0186 --- /dev/null +++ b/Makefile @@ -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 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..6a39e61 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.mzsl.nl/mans/goredire + +go 1.23.0 diff --git a/main.go b/main.go new file mode 100644 index 0000000..6a9cfe7 --- /dev/null +++ b/main.go @@ -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) +}