From d8ecbef86e9b8da73521b33342703bd6c60d8d3f Mon Sep 17 00:00:00 2001 From: ange Date: Sat, 5 Apr 2025 08:00:54 +0000 Subject: [PATCH] feat: aliases --- Dockerfile | 3 ++- aliases.txt | 5 +++++ go.mod | 2 +- html/contact.html | 2 +- {static => js}/copy.js | 0 src/aliases.go | 25 +++++++++++++++++++++++++ src/main.go | 1 + src/route.go | 2 +- src/views.go | 10 ++++++---- tmpl/header.tmpl | 2 +- 10 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 aliases.txt rename {static => js}/copy.js (100%) create mode 100644 src/aliases.go diff --git a/Dockerfile b/Dockerfile index 22f3387..2539050 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM docker.io/golang:1.23 AS build +FROM docker.io/golang:1.24 AS build WORKDIR /build/ COPY go.mod go.sum . RUN go mod download @@ -11,5 +11,6 @@ COPY html/ html/ COPY css/ css/ COPY static/ static/ COPY tmpl/ tmpl/ +COPY aliases.txt aliases.txt EXPOSE 3000 CMD ["./app"] diff --git a/aliases.txt b/aliases.txt new file mode 100644 index 0000000..01094a9 --- /dev/null +++ b/aliases.txt @@ -0,0 +1,5 @@ +ssh /static/ssh +pgp /static/pgp.asc +termux.sh https://git.gmoker.com/ange/termux/raw/branch/main/install.sh +dotfiles.sh +archinstall.sh diff --git a/go.mod b/go.mod index 3a3a5a6..e7f9816 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module main -go 1.23.3 +go 1.24.1 diff --git a/html/contact.html b/html/contact.html index f8aea08..d2be560 100644 --- a/html/contact.html +++ b/html/contact.html @@ -2,7 +2,7 @@ {{template "head.tmpl" .}} - + {{template "header.tmpl" .}} diff --git a/static/copy.js b/js/copy.js similarity index 100% rename from static/copy.js rename to js/copy.js diff --git a/src/aliases.go b/src/aliases.go new file mode 100644 index 0000000..d9fa210 --- /dev/null +++ b/src/aliases.go @@ -0,0 +1,25 @@ +package main + +import ( + "bufio" + "log" + "os" + "strings" +) + +var ALIASES map[string]string + +func generateAliases() { + f, err := os.Open("aliases.txt") + if err != nil { + log.Fatal(err) + } + sc := bufio.NewScanner(f) + + ALIASES = make(map[string]string) + for sc.Scan() { + sp := strings.Fields(sc.Text()) + + ALIASES[sp[0]] = sp[1] + } +} diff --git a/src/main.go b/src/main.go index 823c600..bb5fca5 100644 --- a/src/main.go +++ b/src/main.go @@ -7,6 +7,7 @@ import ( func main() { http.HandleFunc("/", route) + generateAliases() generateTmpl() if err := http.ListenAndServe(":3000", nil); err != nil { log.Fatal(err) diff --git a/src/route.go b/src/route.go index 3c9abe9..1b92eca 100644 --- a/src/route.go +++ b/src/route.go @@ -17,7 +17,7 @@ var routes = []struct { }{ {[]string{"GET"}, url(""), index}, {[]string{"GET"}, url("/static/.+"), static}, - {[]string{"GET"}, url("/(.+\\.css)"), css}, + {[]string{"GET"}, url("/(.+\\.(css|js))"), file}, {[]string{"GET"}, url("/([^/]+)"), html}, } diff --git a/src/views.go b/src/views.go index 8b8e30d..0c5677f 100644 --- a/src/views.go +++ b/src/views.go @@ -1,8 +1,8 @@ package main import ( - "path/filepath" "net/http" + "path/filepath" ) func index(w http.ResponseWriter, r *http.Request) { @@ -13,12 +13,14 @@ func static(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, r.URL.Path) } -func css(w http.ResponseWriter, r *http.Request) { - http.ServeFile(w, r, filepath.Join("css", getParam(r, 0))) +func file(w http.ResponseWriter, r *http.Request) { + http.ServeFile(w, r, filepath.Join(getParam(r, 1), getParam(r, 0))) } func html(w http.ResponseWriter, r *http.Request) { - if t, found := TMPL[getParam(r, 0)]; found { + if a, found := ALIASES[getParam(r, 0)]; found { + http.Redirect(w, r, a, http.StatusFound) + } else if t, found := TMPL[getParam(r, 0)]; found { w.Write(t) } else { http.NotFound(w, r) diff --git a/tmpl/header.tmpl b/tmpl/header.tmpl index a90a18f..e602fcf 100644 --- a/tmpl/header.tmpl +++ b/tmpl/header.tmpl @@ -1,6 +1,6 @@