summaryrefslogtreecommitdiff
path: root/http.go
blob: 95ce105397e8b71cf4aae1ae570e3c02e938bb28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"path/filepath"
	"strings"

	"go.wit.com/lib/protobuf/argvpb"
	"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) {
	msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte
	if err != nil {
		log.Info("ioutil.ReadAll() error =", err)
		return
	}
	// fmt.Fprintln(w, "ioutil.ReadAll() msg =", len(msg))
	// dumpClient(accessf, clientf, r)
	var route string
	// tmp = r.URL.String()
	route = cleanURL(r.URL.Path)
	parts := strings.Split(route, "?")
	log.Info("client sent url =", route, parts)
	requrl := parts[0]

	url, repourl := findkey(requrl)
	log.Warn("gowebd URL =", url, "REPO URL =", repourl, "REQUEST URL =", requrl, "msg =", len(msg))
	if repourl != "" {
		repoHTML(w, url, repourl)
		return
	}
	if route == "/" {
		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 route == "/install.sh" {
		writeFile(w, "install.sh")
		return
	}
	if route == "/me" {
		j, err := dumpJsonClient(r)
		if err != nil {
			fmt.Fprintln(w, "BAD ZOOT")
			return
		}
		fmt.Fprintln(w, j)
		return
	}
	if route == "/register" {
		fname := filepath.Join(LIBDIR, "regfile.log")
		regfile, _ := os.OpenFile(fname, 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 route == "/lookup" {
		w.Header().Set("Content-Type", "text")
		fmt.Fprintf(w, "go.wit.com/apps/utils/gowebd Version: %s\n", argvpb.Version())
		fmt.Fprintf(w, "\n")

		all := me.forge.Repos.SortByFullPath()
		for all.Scan() {
			repo := all.Next()

			fmt.Fprintf(w, "Namespace=%s FullPath=%s %s\n", repo.Namespace, repo.FullPath)
		}
		return
	}

	if route == "/goReference.svg" {
		writeFile(w, "goReference.svg")
		return
	}
	if route == "/skeleton.v2.css" {
		writeFile(w, "skeleton.v2.css")
		return
	}
	if route == "/favicon.ico" {
		writeFile(w, "ipv6.png")
		return
	}
	// used for uptime monitor checking
	if route == "/uptime" {
		writeFile(w, "uptime.html")
		return
	}
	log.Warn("BAD URL =", url, "REPO URL =", repourl)
	badurl(w, r.URL.String())
}

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)
}

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>")
}