package main import ( "fmt" "net/http" "os" "strings" "go.wit.com/log" ) // remove '?' part and trailing '/' func cleanURL(url string) string { url = "/" + strings.Trim(url, "/") return url } func okHandler(w http.ResponseWriter, r *http.Request) { var route string route = cleanURL(r.URL.Path) log.HttpMode(w) defer log.HttpMode(nil) if route == "/help" { log.Info("uptime/ uptime uptime fun!") log.Info("list/ list modified repos") log.Info("list?readonly=true shows every repo") log.Info("gitpull/ run a git pull in each repo") log.Info("") log.Info("Examples:") log.Info("") log.Info("not done yet:") log.Info("changes/ show latest changes") log.Info("repo?repo=go.wit.com/apps/autotypist show this repo") log.Info("clone?repo=go.wit.com/apps/virtigo go-clone a new repo") log.Info("build?repo=go.wit.com/apps/autotypist build the autotypist") log.Info("debian?repo=go.wit.com/apps/autotypist make a debian package of the autotypist") return } if route == "/uptime" { s := "uptime uptime uptime fun!" log.Info(s) return } if route == "/list" { readonly := r.URL.Query().Get("readonly") onlydirty := r.URL.Query().Get("onlydirty") perfect := r.URL.Query().Get("perfect") var count int loop := me.repos.View.ReposSortByName() for loop.Scan() { repo := loop.Repo() count += 1 header := repo.StandardHeader() if onlydirty == "true" { if repo.CheckDirty() { log.Info(header + "") } continue } if repo.ReadOnly() { if readonly == "true" { log.Info(header + "readonly") } continue } if repo.State() == "PERFECT" { if perfect == "false" { continue } } if repo.State() != "merge to main" { log.Info(header + "") continue } if repo.CheckDirty() { log.Info(header + "") continue } log.Info(header + "") } log.Info(fmt.Sprintf("EVERYTHING WORKED repo count = %d", count)) return } // turn off timestamps for STDOUT (systemd adds them) if route == "/daemon" { log.Info("daemon mode ???") log.DaemonMode(false) log.Info("daemon mode false") return } if route == "/repo" { reponame := r.URL.Query().Get("repo") if reponame == "" { s := "reponame is blank " + cleanURL(r.URL.Path) log.Info(s) return } s := "reponame is " + reponame log.Info(s) return } if route == "/quit" { log.Warn("writing out config file and exiting virtigo") fmt.Fprintln(w, "writing out config file and exiting virtigo") os.Exit(0) return } if route == "/favicon.ico" { // w.Header().Set("Content-Type", "image/svg+xml") w.Header().Set("Content-Type", "image/png") writeFile(w, "ipv6.png") return } if route == "/goReference.svg" { w.Header().Set("Content-Type", "image/svg+xml") writeFile(w, "goReference.svg") return } log.Warn("BAD URL =", route) fmt.Fprintln(w, "BAD URL =", route) } // write a file out to the http socket func writeFile(w http.ResponseWriter, filename string) { fullname := "resources/" + filename pfile, err := resources.ReadFile(fullname) if err != nil { log.Println("ERROR:", err) // w.Write(pfile) return } var repohtml string repohtml = string(pfile) if filename == "goReference.svg" { w.Header().Set("Content-Type", "image/svg+xml") } fmt.Fprintln(w, repohtml) log.Println("writeFile() found internal file:", filename) } // starts and sits waiting for HTTP requests func startHTTP() { http.HandleFunc("/", okHandler) p := fmt.Sprintf(":%d", myargv.Port) log.Println("Running on port", p) err := http.ListenAndServe(p, nil) if err != nil { log.Println("Error starting server:", err) } }