198 lines
4.4 KiB
Go
198 lines
4.4 KiB
Go
package main
|
|
|
|
import (
|
|
"embed"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"go.wit.com/log"
|
|
)
|
|
|
|
//go:embed resources/*
|
|
var resources embed.FS
|
|
|
|
var accessf, clientf *os.File
|
|
|
|
var repoMap map[string]string
|
|
var versionMap map[string]string
|
|
var configfile []string
|
|
var keysSorted []string
|
|
|
|
// remove '?' part and trailing '/'
|
|
func cleanURL(url string) string {
|
|
url = "/" + strings.Trim(url, "/")
|
|
return url
|
|
}
|
|
|
|
func okHandler(w http.ResponseWriter, r *http.Request) {
|
|
dumpClient(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("go.wit.com 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)
|
|
indexBodyEnd(w)
|
|
return
|
|
}
|
|
if tmp == "/old" {
|
|
writeFile(w, "index.html")
|
|
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 == "/update" {
|
|
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 == "/list" {
|
|
writeFile(w, "repomap")
|
|
return
|
|
}
|
|
if tmp == "/test" {
|
|
writeFile(w, "test.html")
|
|
return
|
|
}
|
|
if tmp == "/goReference.svg" {
|
|
writeFile(w, "goReference.svg")
|
|
return
|
|
}
|
|
if tmp == "/skeleton.v2.css" {
|
|
writeFile(w, "skeleton.v2.css")
|
|
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 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()
|
|
|
|
// make the instructions to install be:
|
|
// curl -sSL https://go.wit.com/install.sh | bash
|
|
|
|
// build instruction:
|
|
// go install go.wit.com/apps/getgui@latest
|
|
|
|
// 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()
|
|
err := http.ListenAndServe(":2520", nil)
|
|
if err != nil {
|
|
log.Println("Error starting server:", err)
|
|
}
|
|
}
|
|
|
|
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://go.wit.com/\">")
|
|
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://git.wit.org/\">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
|
|
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
|
|
}
|