feat: initial commit

This commit is contained in:
2022-05-23 23:24:09 +01:00
commit 2d5e7d73bc
2 changed files with 56 additions and 0 deletions

3
go.mod Normal file
View File

@@ -0,0 +1,3 @@
module github.com/jimeh/dotkatapult
go 1.18

53
main.go Normal file
View File

@@ -0,0 +1,53 @@
package main
import (
"fmt"
"log"
"net"
"net/http"
"os"
"strings"
"time"
)
func main() {
srv := &http.Server{
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
IdleTimeout: 30 * time.Second,
Handler: http.HandlerFunc(handler),
}
port := "8080"
if v := os.Getenv("PORT"); v != "" {
port = v
}
ln, err := net.Listen("tcp", ":"+port)
if err != nil {
log.Fatal(err)
}
log.Fatal(srv.Serve(ln))
}
func handler(w http.ResponseWriter, req *http.Request) {
target := "katapult.io"
hostname := strings.SplitN(req.Host, ":", 2)[0]
if strings.HasSuffix(hostname, ".katapult") {
sub := strings.TrimSuffix(hostname, ".katapult")
switch sub {
case "my":
target = "my.katapult.io"
case "io":
target = "katapult.io"
default:
target = "my.katapult.io/o/" + sub
}
}
w.Header().Set("Connection", "close")
url := fmt.Sprintf("https://%s", target)
http.Redirect(w, req, url, http.StatusTemporaryRedirect)
}