gowebd/main.go

120 lines
2.9 KiB
Go

package main
import (
"os"
"fmt"
"go.wit.com/log"
"net/http"
"embed"
"strings"
)
//go:embed files/*
var htmlFiles embed.FS
var accessf, clientf *os.File
// 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.Info("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)
indexBodyRepo(w, "go.wit.com/gui/gui", "go.wit.com/gui/gui", "install gitea")
indexBodyEnd(w)
return
}
if tmp == "/old" {
findFile(w, "files/index.html")
return
}
if tmp == "/test" {
findFile(w, "files/repo.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)
/*
pfile, err := htmlFiles.ReadFile("html/repo.html")
if (err != nil) {
log.Println("ERROR:", err)
// w.Write(pfile)
return
}
*/
readconfigfile()
http.HandleFunc("/", okHandler)
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>")
}