From f8126e8a3779842d32f2763df9fe13f741cf1384 Mon Sep 17 00:00:00 2001 From: Leon Mika Date: Sun, 6 Aug 2023 22:06:50 +1000 Subject: [PATCH] Initial commit --- Dockerfile | 11 +++++++++ go.mod | 5 +++++ go.sum | 2 ++ main.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+) create mode 100644 Dockerfile create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..935c8c3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM golang:1.20-alpine + +WORKDIR /usr/src/app + +COPY go.mod go.sum ./ +RUN go mod download && go mod verify + +COPY . . +RUN go build -v -o /usr/local/bin/app ./... + +CMD ["app"] diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..845bc78 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/lmika/rt-redirect + +go 1.20 + +require github.com/gorilla/mux v1.8.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..5350288 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= +github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= diff --git a/main.go b/main.go new file mode 100644 index 0000000..70f92c7 --- /dev/null +++ b/main.go @@ -0,0 +1,65 @@ +package main + +import ( + "fmt" + "github.com/gorilla/mux" + "log" + "net/http" + "os" + "time" +) + +const ( + targetURL = "https://workingset.net" + readTimeout = 30 * time.Second +) + +func main() { + port, hasPort := os.LookupEnv("PORT") + if !hasPort { + port = "8080" + } + + routes := []struct { + From string + To string + }{ + {From: "/2022/04/a-case-for-mocking-in-unit-tests", To: "/2022/04/12/a-case-for.html"}, + {From: "/2021/06/parametrising-your-bdd-tests-in-go", To: "/2021/06/23/parametrising-your-bdd.html"}, + {From: "/2021/03/communication-among-stimulus-controllers-part-2", To: "/2021/02/17/communication-among-stimulus.html"}, + {From: "/2021/02/communication-among-stimulus-controllers-part-1", To: "/2021/02/17/communication-among-stimulus.html"}, + {From: "/2021/01/a-simple-source-ip-address-filter-in-go", To: "/2021/01/20/a-simple-source.html"}, + {From: "/2021/01/building-sets-from-maps", To: "/2021/01/09/building-sets-from.html"}, + {From: "/2020/12/test-helpers-and-test-packages-in-go", To: "/2020/12/23/test-helpers-and.html"}, + {From: "/2020/12/a-tour-of-domain-records-for-email", To: "/2020/12/18/a-tour-of.html"}, + {From: "/2020/12/dealing-with-errors-in-go", To: "/2020/12/16/dealing-with-errors.html"}, + {From: "/2020/12/building-and-serving-go-wasm-projects", To: "/2020/12/12/building-and-serving.html"}, + {From: "/2020/12/a-brief-look-at-stimulus", To: "/2020/12/08/a-brief-look.html"}, + {From: "/2020/12/setting-go-variables-during-build", To: "/2020/12/04/setting-go-variables.html"}, + {From: "/2020/12/posts-only-rss-feed-in-hugo", To: "/2020/12/03/posts-only-rss.html"}, + {From: "/posts/index.xml", To: "/feed.xml"}, + } + + r := mux.NewRouter() + for _, route := range routes { + r.Handle(route.From+"/", redirectTo(route.To)) + r.Handle(route.From, redirectTo(route.To)) + } + r.Handle("/", redirectTo("/")) + + svr := http.Server{ + Addr: fmt.Sprintf(":%v", port), + Handler: r, + ReadTimeout: readTimeout, + ReadHeaderTimeout: readTimeout, + } + if err := svr.ListenAndServe(); err != nil { + log.Fatal(err) + } +} + +func redirectTo(url string) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, targetURL+url, http.StatusPermanentRedirect) + }) +}