aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorla-ninpre <aaoth@aaoth.xyz>2023-12-01 17:08:43 +0300
committerla-ninpre <aaoth@aaoth.xyz>2023-12-01 17:08:43 +0300
commitf80a85811342f014532d8ef191538c836b25d19d (patch)
tree48e857e0c634ce3c09f26f24b508f61ff2e23f82
downloadgomod-index-f80a85811342f014532d8ef191538c836b25d19d.tar.gz
gomod-index-f80a85811342f014532d8ef191538c836b25d19d.zip
init
-rw-r--r--.gitignore1
-rw-r--r--LICENCE15
-rw-r--r--README.md16
-rw-r--r--go.mod3
-rw-r--r--main.go84
5 files changed, 119 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..378bf05
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+gomod-index \ No newline at end of file
diff --git a/LICENCE b/LICENCE
new file mode 100644
index 0000000..0706f33
--- /dev/null
+++ b/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.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..43a5c58
--- /dev/null
+++ b/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').
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..57d1b4b
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,3 @@
+module aaoth.xyz/la-ninpre/gomod-index
+
+go 1.21.4
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..780f9e4
--- /dev/null
+++ b/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))
+}