Initial commit

This commit is contained in:
Leon Mika 2023-08-06 22:06:50 +10:00
commit f8126e8a37
4 changed files with 83 additions and 0 deletions

11
Dockerfile Normal file
View file

@ -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"]

5
go.mod Normal file
View file

@ -0,0 +1,5 @@
module github.com/lmika/rt-redirect
go 1.20
require github.com/gorilla/mux v1.8.0 // indirect

2
go.sum Normal file
View file

@ -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=

65
main.go Normal file
View file

@ -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)
})
}