init
la-ninpre aaoth@aaoth.xyz
Fri, 01 Dec 2023 17:08:43 +0300
5 files changed,
119 insertions(+),
0 deletions(-)
A
LICENCE
@@ -0,0 +1,15 @@
+ISC License + +Copyright (c) 2020-2022 la-ninpre + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR +OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +PERFORMANCE OF THIS SOFTWARE.
A
README.md
@@ -0,0 +1,16 @@
+# gomod-index + +simple and stupid go module thingy. + +## usage + +``` +$ gomod-index -l "<listen on>" -m "<mod root>" -g "<repo root>" +... +``` +this will listen on "liten on" and will redirect +go get requests to "<mod root>/mod" to git repo at "<repo root>/mod". + +## licence + +ISC (see 'LICENCE').
A
main.go
@@ -0,0 +1,84 @@
+package main + +import ( + "flag" + "html/template" + "log" + "net/http" + "net/url" + "strings" +) + +var ( + lFlag = flag.String("l", ":8080", "address and port to listen") + mFlag = flag.String("m", "aaoth.xyz", "module root path") + gFlag = flag.String("g", "https://git.aaoth.xyz", "repository root path") +) + +const indexTmpl = `<!DOCTYPE html> +<html lang="en"> +<head> +<title>aaoth.xyz go</title> +<meta charset="utf-8"> +<meta name="go-import" content="{{.Mod}} git {{.Repo}}"> +<meta http-equiv="refresh" content="0; url={{.Repo}}"> +</head> +<body> +<p>redirecting to <a href="{{.Repo}}">repository</a>...</p> +</body> +</html>` + +var t = template.Must(template.New("index").Parse(indexTmpl)) + +type Data struct { + Mod string + Repo string +} + +func indexHandler(w http.ResponseWriter, req *http.Request) { + err := req.ParseForm() + if err != nil { + log.Println("parse form:", err) + http.Error(w, "bad request", http.StatusBadRequest) + return + } + if goGet, ok := req.Form["go-get"]; !ok || len(goGet) < 1 || goGet[0] != "1" { + http.Error(w, "bad request", http.StatusBadRequest) + return + } + path := strings.Trim(req.URL.EscapedPath(), "/") + parts := strings.Split(path, "/") + if len(parts) < 2 { + log.Println("requested", req.RequestURI, "got", parts) + http.Error(w, "bad request", http.StatusBadRequest) + return + } + mod, err := url.JoinPath(*mFlag, parts[0], parts[1]) + if err != nil { + log.Println("url join:", err) + http.Error(w, "internal server error", http.StatusInternalServerError) + return + } + repo, err := url.JoinPath(*gFlag, parts[0], parts[1]) + if err != nil { + log.Println("url join:", err) + http.Error(w, "internal server error", http.StatusInternalServerError) + return + } + data := Data{mod, repo} + err = t.ExecuteTemplate(w, "index", data) + if err != nil { + log.Println("url join:", err) + http.Error(w, "internal server error", http.StatusInternalServerError) + return + } + log.Println("go get", mod) +} + +func main() { + flag.Parse() + + mux := http.NewServeMux() + mux.HandleFunc("/", indexHandler) + log.Fatal(http.ListenAndServe(*lFlag, mux)) +}