summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRagnis Armus <ragnis@armus.ee>2018-07-12 23:01:34 +0300
committerRagnis Armus <ragnis@armus.ee>2018-07-12 23:01:34 +0300
commit9aa8e334e042861490f3d21c03b53e06d6fcedbd (patch)
treed7e194c6126bf67c1ffc09f9077793718a730921
parent8389d48005858c4e6af2e90373e0c18d84330456 (diff)
add config flags
-rw-r--r--main.go26
1 files changed, 22 insertions, 4 deletions
diff --git a/main.go b/main.go
index f09ccce..eb402d6 100644
--- a/main.go
+++ b/main.go
@@ -1,9 +1,11 @@
package main
import (
+ "flag"
"fmt"
"log"
"net/http"
+ "strings"
"radr.ee/cgit-go-import/cgitrc"
)
@@ -14,7 +16,8 @@ type importRepo struct {
}
var (
- handlers = make(map[string]http.Handler)
+ importPrefix string
+ handlers = make(map[string]http.Handler)
)
func serveStatus(w http.ResponseWriter, status int) {
@@ -40,7 +43,8 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
func createRepoHandler(cfg *cgitrc.Config, r *cgitrc.Repo) http.Handler {
body := []byte(fmt.Sprintf(
- "<head><meta name=\"go-import\" content=\"radr.ee/%s git %s\"></head>",
+ "<head><meta name=\"go-import\" content=\"%s%s git %s\"></head>",
+ importPrefix,
r.URL,
cfg.ResolveRepoCloneURL(r),
))
@@ -51,7 +55,21 @@ func createRepoHandler(cfg *cgitrc.Config, r *cgitrc.Repo) http.Handler {
}
func main() {
- cfg, err := cgitrc.Open("/etc/cgitrc")
+ var (
+ bindAddr string
+ configFile string
+ )
+
+ flag.StringVar(&bindAddr, "bind", ":8080", "server bind address")
+ flag.StringVar(&configFile, "config", "/etc/cgitrc", "path to cgit config file")
+ flag.StringVar(&importPrefix, "prefix", "", "go import prefix")
+ flag.Parse()
+
+ if len(importPrefix) > 0 && !strings.HasSuffix(importPrefix, "/") {
+ importPrefix += "/"
+ }
+
+ cfg, err := cgitrc.Open(configFile)
if err != nil {
log.Fatal(err)
}
@@ -59,5 +77,5 @@ func main() {
handlers["/"+r.URL] = createRepoHandler(cfg, r)
}
- http.ListenAndServe(":8080", http.HandlerFunc(handleRequest))
+ log.Fatal(http.ListenAndServe(bindAddr, http.HandlerFunc(handleRequest)))
}