commit f4f8f937e236d5e076baf4f424b750e7e234291e
Author: Yongbin Kim <iam@yongbin.kim>
Date: Tue, 13 Sep 2022 15:43:39 +0900
First commit
Signed-off-by: Yongbin Kim <iam@yongbin.kim>
Diffstat:
A | go.mod | | | 3 | +++ |
A | main.go | | | 80 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | template.html | | | 10 | ++++++++++ |
3 files changed, 93 insertions(+), 0 deletions(-)
diff --git a/go.mod b/go.mod
@@ -0,0 +1,3 @@
+module go.lair.cx/goget
+
+go 1.18
diff --git a/main.go b/main.go
@@ -0,0 +1,80 @@
+package main
+
+import (
+ "flag"
+ "html/template"
+ "log"
+ "net/http"
+ "os"
+ "strings"
+
+ _ "embed"
+)
+
+type TemplateData struct {
+ PackagePath string
+ ClonePath string
+ SourcePath string
+ ModuleName string
+}
+
+var mux = http.NewServeMux()
+
+var (
+ flagAddr = flag.String("addr", ":8080", "Address")
+ flagPackagePath = flag.String("package-path", "", "Package path")
+ flagClonePath = flag.String("clone-path", "", "Clone path")
+ flagSourcePath = flag.String("source-path", "", "Source path")
+)
+
+//go:embed template.html
+var templateSource []byte
+var tpl *template.Template
+
+func init() {
+ tpl = template.Must(template.New("main").Parse(string(templateSource)))
+}
+
+func main() {
+ flag.Parse()
+
+ if len(*flagPackagePath) == 0 || len(*flagClonePath) == 0 || len(*flagSourcePath) == 0 {
+ log.Println("invalid usage")
+ os.Exit(1)
+ }
+
+ server := &http.Server{
+ Addr: *flagAddr,
+ Handler: http.HandlerFunc(handler),
+ }
+ log.Println("[INFO] Running on " + *flagAddr)
+ log.Fatal(server.ListenAndServe())
+}
+
+func handler(w http.ResponseWriter, r *http.Request) {
+ moduleName := r.URL.Path[1:]
+ if len(moduleName) == 0 {
+ w.WriteHeader(http.StatusNotFound)
+ w.Write([]byte("Module not found"))
+ return
+ }
+
+ if strings.ContainsRune(moduleName, '/') {
+ w.WriteHeader(http.StatusNotFound)
+ w.Write([]byte("Module not found"))
+ return
+ }
+
+ data := &TemplateData{
+ PackagePath: *flagPackagePath,
+ ClonePath: *flagClonePath,
+ SourcePath: *flagSourcePath,
+ ModuleName: moduleName,
+ }
+
+ w.WriteHeader(200)
+ err := tpl.Execute(w, data)
+ if err != nil {
+ log.Printf("[EROR] template error: %s\n", err.Error())
+ }
+}
diff --git a/template.html b/template.html
@@ -0,0 +1,10 @@
+<!doctype html>
+<html>
+ <head>
+ <meta name="go-import" content="{{ .PackagePath }}/{{ .ModuleName }} git {{ .ClonePath }}/{{ .ModuleName }}">
+ <meta name="go-source" content="{{ .PackagePath }}/{{ .ModuleName }} _ {{ .SourcePath }}/{{ .ModuleName }}/files.html {{ .SourcePath }}/{{ .ModuleName }}/file{/dir}/{file}.html#l{line}">
+ </head>
+ <body>
+ <p>Nothing to see here. <a href="https://git.lair.cx/{{ .ModuleName }}/">Repository</a>
+ </body>
+</html>