From f80a85811342f014532d8ef191538c836b25d19d Mon Sep 17 00:00:00 2001 From: la-ninpre Date: Fri, 1 Dec 2023 17:08:43 +0300 Subject: init --- .gitignore | 1 + LICENCE | 15 +++++++++++ README.md | 16 ++++++++++++ go.mod | 3 +++ main.go | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 119 insertions(+) create mode 100644 .gitignore create mode 100644 LICENCE create mode 100644 README.md create mode 100644 go.mod create mode 100644 main.go 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 "" -m "" -g "" +... +``` +this will listen on "liten on" and will redirect +go get requests to "/mod" to git repo at "/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 = ` + + +aaoth.xyz go + + + + + +

redirecting to repository...

+ +` + +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)) +} -- cgit v1.2.3