package main import ( "embed" "fmt" "net/http" "strings" "time" "go.wit.com/lib/gui/prep" "go.wit.com/lib/protobuf/forgepb" "go.wit.com/lib/protobuf/gitpb" "go.wit.com/log" ) // are sent via -ldflags at buildtime var VERSION string var BUILDTIME string //go:embed resources/* var resources embed.FS // used for shell auto completion var ARGNAME string = "gowebd" var repoMap map[string]string var gitMap map[string]*gitpb.Repo var configfile []string var keysSorted []string var HOSTNAME string = "go.wit.com" var REPOMAP string = "/etc/gowebd/repomap" var FOOTER string = "/etc/gowebd/footer.html" var LIBDIR string = "/var/lib/gowebd/" func main() { me = new(mainType) me.sh = prep.Bash(&argv) // adds shell auto complete to go-args if argv.RepoMap != "" { REPOMAP = argv.RepoMap } if argv.Hostname != "" { HOSTNAME = argv.Hostname } gitMap = make(map[string]*gitpb.Repo) repoMap = make(map[string]string) me.forge = forgepb.Init() // parse the repomap file readRepomap() if argv.Test != nil { if s, err := newMakeRepomap(); err != nil { me.sh.BadExit(s, err) } else { me.sh.GoodExit(s) } } http.HandleFunc("/", okHandler) // go https() // use caddy instead p := fmt.Sprintf(":%d", argv.Port) log.Println("HOSTNAME set to:", HOSTNAME) log.Println("Running on port", p) err := http.ListenAndServe(p, nil) if err != nil { log.Println("Error starting server:", err) } } func newMakeRepomap() (string, error) { submit := gitpb.NewRepos() for key, val := range repoMap { if !strings.HasPrefix(key, "go.wit.com") { // only handling go.wit.com here continue } log.Info(key) r := new(gitpb.Repo) r.Namespace = key r.State = val submit.Append(r) } server := me.forge.GetForgeURL() updatepb, regPB, err := submit.HttpPost(server, "check") if err != nil { log.Info("ReposPB HttpPost() failed", err) return "HttpPost() failed", err } if regPB == nil { log.Info("regPB==nil") return "HttpPost() internally returned nil http PB", err } if updatepb == nil { log.Info("server sent nil back nil PB") return "HttpPost() internally returned nil http PB", err } for repo := range updatepb.IterByNamespace() { log.Info(repo.Namespace, repo.URL) } makePullTable(updatepb) return "new dynamic repomap!", nil } func makePullTable(pb *gitpb.Repos) { t := pb.NewTable("pullTable") t.NewUuid() var col *gitpb.RepoFunc // var col int col = t.AddNamespace() col.Width = 30 col = t.AddMasterVersion() // col.SetTitle("mver") col.Width = 15 col = t.AddStringFunc("Tags", func(r *gitpb.Repo) string { if r.Tags == nil { return "nil" } log.Infof("repo: %v\n", r) return log.Sprintf("len(%d)", r.Tags.Len()) }) col.Width = 9 col = t.AddStringFunc("cur tag", func(r *gitpb.Repo) string { if r.CurrentTag == nil { return "nil" } return "ok" }) col.Width = 9 col = t.AddDevelVersion() col.Width = 15 col = t.AddFullPath() col.Width = -1 t.PrintTable() } func formatDuration(d time.Duration) string { seconds := int(d.Seconds()) % 60 minutes := int(d.Minutes()) % 60 hours := int(d.Hours()) % 24 days := int(d.Hours()) / 24 result := "" if days > 0 { result += fmt.Sprintf("%dd ", days) return result } if hours > 0 { result += fmt.Sprintf("%dh ", hours) return result } if minutes > 0 { result += fmt.Sprintf("%dm ", minutes) return result } if seconds > 0 { result += fmt.Sprintf("%ds", seconds) } return result }