From 2e881b11ef571f5fb97ea0b3f93f79cf4879c04b Mon Sep 17 00:00:00 2001 From: Edgaru089 Date: Sun, 13 Jun 2021 19:15:21 +0800 Subject: [PATCH] Initial commit --- config.json | 16 ++++++++ main.go | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++ template.html | 11 ++++++ 3 files changed, 134 insertions(+) create mode 100644 config.json create mode 100644 main.go create mode 100644 template.html diff --git a/config.json b/config.json new file mode 100644 index 0000000..67be731 --- /dev/null +++ b/config.json @@ -0,0 +1,16 @@ +{ + "__comment__": "What comment? Just go read the code.", + + "GitServerPrefix": "https://git.edgaru089.ml", + "GoSourceFile": "https://git.edgaru089.ml/{/path}/src/branch/master{/dir}", + "GoSourceLine": "https://git.edgaru089.ml/{/path}/src/branch/master{/dir}/{file}#L{line}", + + "Repos": { + "go/gl02": { + "ImportPath": "edgaru089.ml/go/gl02", + "ShortPath": "go/gl02", + "RepoType": "git", + "RepoPath": "https://git.edgaru089.ml/go/gl02.git" + } + } +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..8378d81 --- /dev/null +++ b/main.go @@ -0,0 +1,107 @@ +package main + +import ( + "encoding/json" + "flag" + "fmt" + "html/template" + "log" + "net/http" + "os" + + _ "embed" +) + +type Repo struct { + ImportPath string // like example.com/package/code + ShortPath string // like package/code + + RepoType string // like git or svn + RepoPath string // like https://git.example.com/package/code.git or git@git.example.com:package/code.git +} + +type Config struct { + GitServerPrefix string // Like https://github.com or https://git.example.com (without ending slash) + + // go-source tag strings. Example: + // GoSourceFile: https://git.example.com/{/path}/src/branch/master{/dir} + // GoSourceLine: https://git.example.com/{/path}/src/branch/master{/dir}/{file}#L{line} + GoSourceFile, GoSourceLine string + + Repos map[string]*Repo // mapped by ShortPath, like package/code +} + +//go:embed template.html +var httpTemplate string + +var ( + ConfigFile, TemplateFile string + ListenAddress string +) + +func init() { + flag.StringVar(&ConfigFile, "config", "config.json", "Config file") + flag.StringVar(&TemplateFile, "template", "template.html", "template HTML file") + flag.StringVar(&ListenAddress, "listen", "0.0.0.0:32148", "listen address") +} + +type Server struct { + config Config + template *template.Template +} + +func (s *Server) ServeHTTP(resp http.ResponseWriter, req *http.Request) { + log.Print("request: ", req.URL.Path) + id := req.URL.Path[1:] + if repo, ok := s.config.Repos[id]; ok { + err := s.template.Execute(resp, repo) + if err != nil { + resp.WriteHeader(500) + resp.Write([]byte("Internal Server Error\n\n")) + resp.Write([]byte(fmt.Sprintf("Error: Executing Template: %s, id=%s\n\nRepo=%v", err.Error(), id, repo))) + } + } else { + resp.WriteHeader(404) + } +} + +func main() { + var c Config + + flag.Parse() + + file, err := os.Open(ConfigFile) + if err != nil { + fmt.Fprint(os.Stderr, "cannot open config file: ", err) + os.Exit(1) + } + + dec := json.NewDecoder(file) + err = dec.Decode(&c) + if err != nil { + fmt.Fprint(os.Stderr, "error parsing config file:", err) + os.Exit(1) + } + + tempbyte, err := os.ReadFile(TemplateFile) + if err != nil { + fmt.Fprint(os.Stderr, "error reading template file:", err) + os.Exit(1) + } + + s := &Server{config: c, template: template.New("")} + s.template.Funcs(template.FuncMap{ + "config": func() Config { + return c + }, + }) + _, err = s.template.Parse(string(tempbyte)) + if err != nil { + fmt.Fprint(os.Stderr, "error parsing template file:", err) + os.Exit(1) + } + + log.Print("listening on ", ListenAddress) + http.ListenAndServe(ListenAddress, s) + +} diff --git a/template.html b/template.html new file mode 100644 index 0000000..7d595a4 --- /dev/null +++ b/template.html @@ -0,0 +1,11 @@ + + + + + + + + go get {{.ImportPath}} + + +