autotypist/http.go

250 lines
5.9 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"net/http"
"os"
"strings"
"go.wit.com/lib/gui/repostatus"
"go.wit.com/lib/gui/shell"
"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 == "/release" {
simpleRelease(w, r)
return
}
if route == "/gitpull" {
repoName := r.URL.Query().Get("repo")
if repoName != "" {
// git pull (or go-clone of it doesn't exist)
repo := me.repos.View.FindRepoByName(repoName)
if repo == nil {
cmd := []string{"go-clone", repoName}
header := repo.StandardHeader()
log.Info(header + strings.Join(cmd, " "))
shell.RunHttpOut(cmd, w, r)
} else {
cmd := []string{"git", "pull", "-v"}
header := repo.StandardHeader()
log.Info(header + strings.Join(cmd, " "))
shell.RunPathHttpOut(repo.Status.FullPath(), cmd, w, r)
}
return
}
// git pull every repo. Ignore Dirty repos
loop := me.repos.View.ReposAll()
for loop.Scan() {
repo := loop.Repo()
cmd := []string{"git", "pull", "-v"}
header := repo.StandardHeader()
if repo.CheckDirty() {
log.Info(header + "skip dirty repo")
continue
}
log.Info(header + strings.Join(cmd, " "))
shell.RunPathHttpOut(repo.Status.FullPath(), cmd, w, r)
}
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
}
if route == "/goweblist" {
readonly := r.URL.Query().Get("readonly")
loop := me.repos.View.ReposAll()
for loop.Scan() {
repo := loop.Repo()
lastTag := repo.LastTag()
tag := repo.Status.NewestTag()
gitAge, err := tag.GetDate()
if err != nil {
log.Info(fmt.Sprintf("tag date error", repo.Name()))
}
// if lastTag == "" {
// lastTag = tag.Name()
// }
if repo.ReadOnly() {
if readonly == "true" {
continue
}
}
// dur := time.Since(gitAge)
// log.Info(fmt.Sprintf("%-60s %s %s %s", repo.Name(), lastTag, shell.FormatDuration(dur), lastTag, tag.Name()))
log.Info(fmt.Sprintf("%s %d %s", repo.Name(), gitAge.Unix(), lastTag))
/*
for _, tag := range repo.Tags.ListAll() {
log.Info(fmt.Sprintf("%-60s %s", "", tag.Name()))
}
*/
}
return
}
2024-11-15 20:06:17 -06:00
// 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 == "/listGitDirectories" {
for i, path := range repostatus.ListGitDirectories() {
// log.Info("addRepo()", i, path)
path = strings.TrimPrefix(path, me.goSrcPwd.String())
path = strings.Trim(path, "/")
log.Info(fmt.Sprintf("paths:", i, path))
}
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)
}
}