new args. use forgepb for versions and dates

This commit is contained in:
Jeff Carr 2024-12-14 10:47:08 -06:00
parent 0775662416
commit dead6fe01f
8 changed files with 230 additions and 216 deletions

View File

@ -13,7 +13,7 @@ all: build
@echo "make enable # enable daemon on boot"
@echo "make log # watch the daemon log"
build:
build: goimports
GO111MODULE=off go build \
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
@ -27,7 +27,7 @@ install:
log:
@#systemctl status gowebd.service
@journalctl -f -xeu gowebd.service
journalctl -f -xeu gowebd.service
enable:
systemctl enable gowebd.service
@ -43,17 +43,13 @@ stop:
systemctl stop gowebd.service
run: build
./gowebd --port 2233
./gowebd --port 2233 --repomap resources/repomap --hostname test.wit.com
# setcap 'cap_net_bind_service=+ep' gowebd # allow the binary to open ports below 1024
goimports:
reset
goimports -w *.go
redomod:
rm -f go.*
GO111MODULE= go mod init
GO111MODULE= go mod tidy
clean:
rm -f go.*
rm -f go.wit.com

View File

@ -13,8 +13,10 @@ import (
var argv args
type args struct {
ListRepos bool `arg:"--list-repos" help:"list all repositories"`
Port int `arg:"--port" default:"2520" help:"port to run on"`
ListRepos bool `arg:"--list-repos" help:"list all repositories"`
Port int `arg:"--port" default:"2520" help:"port to run on"`
RepoMap string `arg:"--repomap" default:"/etc/gowebd/repomap" help:"repomap file"`
Hostname string `arg:"--hostname" default:"go.wit.com" help:"hostname to use"`
}
func (args) Version() string {

133
http.go Normal file
View File

@ -0,0 +1,133 @@
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) {
// dumpClient(accessf, clientf, r)
var tmp string
// tmp = r.URL.String()
tmp = cleanURL(r.URL.Path)
parts := strings.Split(tmp, "?")
log.Info("client sent url =", tmp)
log.Info("parts are:", parts)
requrl := parts[0]
url, repourl := findkey(requrl)
log.Warn("gowebd URL =", url, "REPO URL =", repourl, "REQUEST URL =", requrl)
if repourl != "" {
repoHTML(w, url, repourl)
return
}
if tmp == "/" {
indexHeader(w)
indexBodyStart(w)
indexBodyScanConfig(w)
indexDivEnd(w)
pfile, err := os.ReadFile(FOOTER)
if err == nil {
fmt.Fprint(w, string(pfile))
} else {
log.Warn(err, "no footer file found in", FOOTER)
log.Warn("falling back to the resources/footer.html")
writeFile(w, "footer.html")
}
indexBodyEnd(w)
return
}
if tmp == "/install.sh" {
writeFile(w, "install.sh")
return
}
if tmp == "/me" {
j, err := dumpJsonClient(r)
if err != nil {
fmt.Fprintln(w, "BAD ZOOT")
return
}
fmt.Fprintln(w, j)
return
}
if tmp == "/register" {
regfile, _ := os.OpenFile("/home/jcarr/regfile.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if registerClient(regfile, r) {
fmt.Fprintln(w, "OK")
} else {
fmt.Fprintln(w, "FAILED")
}
return
}
if tmp == "/goReference.svg" {
writeFile(w, "goReference.svg")
return
}
if tmp == "/skeleton.v2.css" {
writeFile(w, "skeleton.v2.css")
return
}
if tmp == "/favicon.ico" {
writeFile(w, "ipv6.png")
return
}
// used for uptime monitor checking
if tmp == "/uptime" {
writeFile(w, "uptime.html")
return
}
log.Warn("BAD URL =", url, "REPO URL =", repourl)
badurl(w, r.URL.String())
// fmt.Fprintln(w, "BAD", tmp)
}
func writeFile(w http.ResponseWriter, filename string) {
// fmt.Fprintln(w, "GOT TEST?")
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)
// w.Close()
/*
filename = "/tmp/" + name + ".so"
log.Error(err, "write out file here", name, filename, len(pfile))
f, _ := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0600)
f.Close()
*/
}
func badurl(w http.ResponseWriter, badurl string) {
fmt.Fprintln(w, "<!DOCTYPE html>")
fmt.Fprintln(w, "<html>")
fmt.Fprintln(w, " <head>")
fmt.Fprintln(w, " <meta http-equiv=\"refresh\" content=\"3; url=https://"+HOSTNAME+"/\">")
fmt.Fprintln(w, " </head>")
fmt.Fprintln(w, " <body>")
fmt.Fprintln(w, " IPv4 IS NOT SUPPORTED<br>")
fmt.Fprintln(w, " MANY OF THESE REPOS REQUIRE IPv6.<br>")
fmt.Fprintln(w, " <br>")
fmt.Fprintln(w, " bad url", badurl, "<a href=\"https://go.wit.com/\">redirecting</a>")
fmt.Fprintln(w, " </body>")
fmt.Fprintln(w, "</html>")
}

View File

@ -3,9 +3,8 @@ package main
import (
"fmt"
"net/http"
"strconv"
"strings"
"time"
"go.wit.com/log"
)
@ -105,31 +104,16 @@ func indexBodyRepo(w http.ResponseWriter, gourl string, giturl string, desc stri
// }
var vtime, version string
gourl = strings.TrimSpace(gourl)
tmp, _ := versionMap[gourl]
parts := strings.Split(tmp, " ")
if len(parts) > 0 {
vtime = parts[0]
}
if len(parts) > 1 {
version = parts[1]
}
log.Warn("gourl ", gourl, "vtime =", vtime, "version =", version)
if vtime != "" {
// Convert the string to an integer
gitTagTimestampInt, _ := strconv.ParseInt(vtime, 10, 64)
// Parse the Unix timestamp into a time.Time object
gitTagDate := time.Unix(gitTagTimestampInt, 0)
// Get the current time
currentTime := time.Now()
// Calculate the duration between the git tag date and the current time
duration := currentTime.Sub(gitTagDate)
vtime = formatDuration(duration)
if tmp, ok := versionMap[gourl]; ok {
// log.Info("gopath", gopath, "tmp", tmp)
parts := strings.Fields(tmp)
if len(parts) == 2 {
// log.Info("GOT HERE gopath", gopath, "tmp", tmp)
version = parts[0]
vtime = parts[1]
}
}
log.Info("gopath", gourl, "dur", vtime, "version", version)
fmt.Fprintln(w, " <td>"+version+"</td>") // version
fmt.Fprintln(w, " <td>"+vtime+"</td>") // dev version

View File

@ -17,8 +17,8 @@ type RequestInfo struct {
Headers map[string][]string `json:"headers"`
Cookies map[string]string `json:"cookies"`
QueryParams map[string][]string `json:"queryParams"`
Body string `json:"body"`
// Add other fields as needed
Body string `json:"body"`
}
// dumpClient returns a string with JSON formatted http.Request information
@ -68,41 +68,3 @@ func dumpJsonClient(r *http.Request) (string, error) {
return string(formattedJSON), nil
}
/*
package main
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
)
type RequestInfo struct {
// ... (other fields)
Body string `json:"body"`
// ... (other fields)
}
func dumpClient(r *http.Request) (string, error) {
// ... (rest of your code to collect other request info)
info := RequestInfo{
// ... (other fields)
Body: string(bodyBytes),
// ... (other fields)
}
// Marshal the struct to a JSON string
jsonString, err := json.Marshal(info)
if err != nil {
return "", err
}
return string(jsonString), nil
}
// ... (rest of your code)
*/

173
main.go
View File

@ -4,10 +4,10 @@ import (
"embed"
"fmt"
"net/http"
"os"
"strings"
"time"
"go.wit.com/lib/protobuf/forgepb"
"go.wit.com/log"
)
@ -24,135 +24,51 @@ var repoMap map[string]string
var versionMap map[string]string
var configfile []string
var keysSorted []string
var forge *forgepb.Forge
var HOSTNAME string = "go.wit.com"
// remove '?' part and trailing '/'
func cleanURL(url string) string {
url = "/" + strings.Trim(url, "/")
return url
}
func okHandler(w http.ResponseWriter, r *http.Request) {
// dumpClient(accessf, clientf, r)
var tmp string
// tmp = r.URL.String()
tmp = cleanURL(r.URL.Path)
parts := strings.Split(tmp, "?")
log.Info("client sent url =", tmp)
log.Info("parts are:", parts)
requrl := parts[0]
url, repourl := findkey(requrl)
log.Warn("gowebd URL =", url, "REPO URL =", repourl, "REQUEST URL =", requrl)
if repourl != "" {
repoHTML(w, url, repourl)
return
}
if tmp == "/" {
indexHeader(w)
indexBodyStart(w)
indexBodyScanConfig(w)
indexDivEnd(w)
pfile, err := os.ReadFile("/etc/gowebd/footer.html")
if err == nil {
fmt.Fprint(w, string(pfile))
} else {
log.Warn(err, "no footer filee found in /etc/gowebd/footer.html")
log.Warn("falling back to the resources/footer.html")
writeFile(w, "footer.html")
}
indexBodyEnd(w)
return
}
if tmp == "/install.sh" {
writeFile(w, "install.sh")
return
}
if tmp == "/me" {
j, err := dumpJsonClient(r)
if err != nil {
fmt.Fprintln(w, "BAD ZOOT")
return
}
fmt.Fprintln(w, j)
return
}
if tmp == "/register" {
regfile, _ := os.OpenFile("/home/jcarr/regfile.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if registerClient(regfile, r) {
fmt.Fprintln(w, "OK")
} else {
fmt.Fprintln(w, "FAILED")
}
return
}
if tmp == "/goReference.svg" {
writeFile(w, "goReference.svg")
return
}
if tmp == "/skeleton.v2.css" {
writeFile(w, "skeleton.v2.css")
return
}
if tmp == "/favicon.ico" {
writeFile(w, "ipv6.png")
return
}
// used for uptime monitor checking
if tmp == "/uptime" {
writeFile(w, "uptime.html")
return
}
log.Warn("BAD URL =", url, "REPO URL =", repourl)
badurl(w, r.URL.String())
// fmt.Fprintln(w, "BAD", tmp)
}
func writeFile(w http.ResponseWriter, filename string) {
// fmt.Fprintln(w, "GOT TEST?")
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)
// w.Close()
/*
filename = "/tmp/" + name + ".so"
log.Error(err, "write out file here", name, filename, len(pfile))
f, _ := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0600)
f.Close()
*/
}
var REPOMAP string = "/etc/gowebd/repomap"
var FOOTER string = "/etc/gowebd/footer.html"
func main() {
// accessf, _ = os.OpenFile("/home/jcarr/accessclient.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
// clientf, _ = os.OpenFile("/home/jcarr/httpclient.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
readconfigfile()
readVersionFile()
if argv.RepoMap != "" {
REPOMAP = argv.RepoMap
}
if argv.Hostname != "" {
HOSTNAME = argv.Hostname
}
forge = forgepb.Init()
versionMap = make(map[string]string)
// make the instructions to install be:
// curl -sSL https://go.wit.com/install.sh | bash
all := forge.Repos.SortByGoPath()
for all.Scan() {
repo := all.Next()
// build instruction:
// go install go.wit.com/apps/getgui@latest
log.Info("forge protobuf has:", repo.GoPath)
}
// parse the repomap file
readRepomap()
// readVersionFile()
var vtime string
var version string
for gopath, thing := range versionMap {
if tmp, ok := versionMap[gopath]; ok {
// log.Info("gopath", gopath, "tmp", tmp)
parts := strings.Fields(tmp)
if len(parts) == 2 {
// log.Info("GOT HERE gopath", gopath, "tmp", tmp)
version = parts[0]
vtime = parts[1]
}
}
log.Info("gopath", gopath, "info", thing, "dur", vtime, "version", version)
}
// for i, s := range versionMap {
// log.Println("found i =", i, "with", "s =", s)
// }
log.Println("found log =", versionMap["go.wit.com/log"])
http.HandleFunc("/", okHandler)
// go https()
// go https() // use caddy instead
p := fmt.Sprintf(":%d", argv.Port)
log.Println("HOSTNAME set to:", HOSTNAME)
log.Println("Running on port", p)
@ -162,21 +78,6 @@ func main() {
}
}
func badurl(w http.ResponseWriter, badurl string) {
fmt.Fprintln(w, "<!DOCTYPE html>")
fmt.Fprintln(w, "<html>")
fmt.Fprintln(w, " <head>")
fmt.Fprintln(w, " <meta http-equiv=\"refresh\" content=\"3; url=https://" + HOSTNAME + "/\">")
fmt.Fprintln(w, " </head>")
fmt.Fprintln(w, " <body>")
fmt.Fprintln(w, " IPv4 IS NOT SUPPORTED<br>")
fmt.Fprintln(w, " MANY OF THESE REPOS REQUIRE IPv6.<br>")
fmt.Fprintln(w, " <br>")
fmt.Fprintln(w, " bad url", badurl, "<a href=\"https://go.wit.com/\">redirecting</a>")
fmt.Fprintln(w, " </body>")
fmt.Fprintln(w, "</html>")
}
func formatDuration(d time.Duration) string {
seconds := int(d.Seconds()) % 60
minutes := int(d.Minutes()) % 60

View File

@ -1,5 +1,8 @@
package main
// parses /etc/gowebd/repomap
// this file defines what repositories show up on go.wit.com
import (
"bufio"
"fmt"
@ -12,6 +15,8 @@ import (
"go.wit.com/log"
)
// this makes the "go-source" / "go-import" page
// this redirects the go path "go.wit.com/apps/go-clone" to the git path "gitea.wit.com/wit/go-clone"
func repoHTML(w http.ResponseWriter, gourl string, realurl string) {
realurl = "https://" + realurl
log.Info("go repo =", gourl, "real url =", realurl)
@ -39,12 +44,12 @@ func findkey(url string) (string, string) {
// parts := strings.Split(key, "/")
}
func readconfigfile() {
func readRepomap() {
var pfile []byte
var err error
repoMap = make(map[string]string)
pfile, err = os.ReadFile("/etc/gowebd/repomap")
pfile, err = os.ReadFile(REPOMAP)
if err != nil {
log.Error(err, "no repository map file found in /etc/gowebd/")
log.Error(err, "falling back to the repository map file built into the gowebd binary")
@ -75,13 +80,29 @@ func readconfigfile() {
sort.Strings(keysSorted)
// sort.Reverse(keys)
sort.Sort(sort.Reverse(sort.StringSlice(keysSorted)))
for _, key := range keysSorted {
log.Info("repo =", key, "real url =", repoMap[key])
for _, gopath := range keysSorted {
repo := forge.Repos.FindByGoPath(gopath)
if repo != nil {
version := repo.GetLastTag()
age := forge.NewestAge(repo)
log.Info("repo =", gopath, "real url =", repoMap[gopath], version, formatDuration(age))
versionMap[gopath] = version + " " + formatDuration(age)
/*
all := repo.Tags.SortByAge()
for all.Scan() {
r := all.Next()
dur := time.Since(r.GetAuthordate().AsTime())
name := r.Refname
log.Info("tag =", name, formatDuration(dur))
}
*/
} else {
log.Info("repo =", gopath, "real url =", repoMap[gopath], "not found")
}
}
}
func readVersionFile() {
versionMap = make(map[string]string)
file, err := os.Open(filepath.Join(os.Getenv("HOME"), "go.wit.com.versions"))
if err != nil {
return

15
resources/repomap Normal file
View File

@ -0,0 +1,15 @@
# gui core packages
go.wit.com/gui gitea.wit.com/gui/gui The GUI API intended for Control Panels
go.wit.com/widget gitea.wit.com/gui/widget Primitive Definitions for Buttons, Dropdowns, etc.
# Tutorials
go.wit.com/apps/helloworld gitea.wit.com/gui/helloworld A simple Demo
go.wit.com/apps/basicwindow gitea.wit.com/jcarr/basicwindow A bit more of a Demo
go.wit.com/apps/gadgetwindow gitea.wit.com/jcarr/gadgetwindow A more complicated Demo used for debugging the toolkits
# Applications (mirrors.wit.com has .deb packages)
go.wit.com/apps/go-clone gitea.wit.com/gui/go-clone recursively 'git clone' golang packages
go.wit.com/apps/go-deb gitea.wit.com/jcarr/go-deb Turn anything into a .deb package