66 lines
2.2 KiB
Go
66 lines
2.2 KiB
Go
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)
|
|
})
|
|
}
|