gowebd/main.go

163 lines
3.6 KiB
Go

package main
import (
"embed"
"fmt"
"net/http"
"os"
"strings"
"time"
"go.wit.com/log"
)
//go:embed files/*
var htmlFiles 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" {
findFile(w, "files/index.html")
return
}
if tmp == "/me" {
j, err := dumpJsonClient(r)
if err != nil {
fmt.Fprintln(w, "BAD ZOOT")
return
}
fmt.Fprintln(w, j)
return
}
if tmp == "/list" {
findFile(w, "files/repomap")
return
}
if tmp == "/test" {
findFile(w, "files/test.html")
return
}
if tmp == "/skeleton.v2.css" {
findFile(w, "files/skeleton.v2.css")
return
}
log.Warn("BAD URL =", url, "REPO URL =", repourl)
badurl(w, r.URL.String())
// fmt.Fprintln(w, "BAD", tmp)
}
func findFile(w http.ResponseWriter, filename string) {
// fmt.Fprintln(w, "GOT TEST?")
pfile, err := htmlFiles.ReadFile(filename)
if err != nil {
log.Println("ERROR:", err)
// w.Write(pfile)
return
}
var repohtml string
repohtml = string(pfile)
fmt.Fprintln(w, repohtml)
log.Println("findFile() 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()
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(":80", 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
}