dump /me as JSON and /list for the autotypist

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-01-17 16:05:28 -06:00
parent 6909acbbe4
commit 44ab3d1679
5 changed files with 128 additions and 12 deletions

View File

@ -4,7 +4,8 @@ go.wit.com/log git.wit.org/wit/log
# gui/ packages # gui/ packages
go.wit.com/gui/gui git.wit.org/gui/gui go.wit.com/gui git.wit.org/jcarr/old-gui/
go.wit.com/gui/gui git.wit.org/jcarr/new-gui
go.wit.com/gui/widget git.wit.org/gui/widget go.wit.com/gui/widget git.wit.org/gui/widget
go.wit.com/gui/toolkits git.wit.org/gui/toolkits go.wit.com/gui/toolkits git.wit.org/gui/toolkits
go.wit.com/gui/debugger git.wit.org/gui/debugger go.wit.com/gui/debugger git.wit.org/gui/debugger
@ -13,17 +14,20 @@ go.wit.com/gui/gadgets git.wit.org/gui/gadgets
# gui libraries # gui libraries
go.wit.com/gui/lib/repostatus git.wit.org/jcarr/repostatus go.wit.com/gui/lib/repostatus git.wit.org/jcarr/repostatus
go.wit.com/gui/lib/hostname git.wit.org/jcarr/hostname go.wit.com/gui/lib/hostname git.wit.org/jcarr/hostname
go.wit.com/gui/lib/linuxstatus git.wit.org/jcarr/hostname
go.wit.com/gui/lib/logsettings git.wit.org/jcarr/logsettings go.wit.com/gui/lib/logsettings git.wit.org/jcarr/logsettings
go.wit.com/gui/lib/digitalocean git.wit.org/gui/digitalocean go.wit.com/gui/lib/digitalocean git.wit.org/gui/digitalocean
go.wit.com/gui/lib/cloudflare git.wit.org/gui/cloudflare go.wit.com/gui/lib/cloudflare git.wit.org/gui/cloudflare
go.wit.com/gui/lib/protobuf/wit git.wit.org/wit/witProtobuf
# Applications # Applications
go.wit.com/apps/helloworld git.wit.org/gui/helloworld
go.wit.com/apps/autotypist git.wit.org/jcarr/myrepos
go.wit.com/apps/control-panel-dns git.wit.org/jcarr/control-panel-dns go.wit.com/apps/control-panel-dns git.wit.org/jcarr/control-panel-dns
go.wit.com/apps/control-panel-digitalocean git.wit.org/wit/control-panel-digitalocean go.wit.com/apps/control-panel-digitalocean git.wit.org/wit/control-panel-digitalocean
go.wit.com/apps/control-panel-cloudflare git.wit.org/wit/control-panel-cloudflare go.wit.com/apps/control-panel-cloudflare git.wit.org/wit/control-panel-cloudflare
go.wit.com/apps/control-panel-vpn git.wit.org/wit/control-panel-vpn go.wit.com/apps/control-panel-vpn git.wit.org/jcarr/control-panel-vpn
go.wit.com/apps/autotypist git.wit.org/jcarr/myrepos
go.wit.com/apps/myrepos git.wit.org/jcarr/myrepos go.wit.com/apps/myrepos git.wit.org/jcarr/myrepos
go.wit.com/apps/go.wit.com git.wit.org/jcarr/go.wit.com go.wit.com/apps/go.wit.com git.wit.org/jcarr/go.wit.com

107
jsonClient.go Normal file
View File

@ -0,0 +1,107 @@
package main
import (
"io/ioutil"
"bytes"
"encoding/json"
"net/http"
)
// RequestInfo holds the extracted data from http.Request
type RequestInfo struct {
Host string `json:"host"`
URL string `json:"url"`
Proto string `json:"proto"`
Addr string `json:"addr"`
Agent string `json:"agent"`
Headers map[string][]string `json:"headers"`
Cookies map[string]string `json:"cookies"`
QueryParams map[string][]string `json:"queryParams"`
// Add other fields as needed
Body string `json:"body"`
}
// dumpClient returns a string with JSON formatted http.Request information
func dumpJsonClient(r *http.Request) (string, error) {
// Extracting Cookies
cookieMap := make(map[string]string)
for _, cookie := range r.Cookies() {
cookieMap[cookie.Name] = cookie.Value
}
// Read the body (Ensure to do this first)
var bodyBytes []byte
if r.Body != nil { // Read the body if it's not nil
bodyBytes, _ = ioutil.ReadAll(r.Body)
r.Body.Close() // Close the body when done reading
r.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes)) // Reset the body
}
info := RequestInfo{
Host: r.Host,
URL: r.URL.String(),
Proto: r.Proto,
Addr: r.RemoteAddr,
Agent: r.UserAgent(),
Headers: r.Header,
Cookies: cookieMap,
QueryParams: r.URL.Query(),
Body: string(bodyBytes),
}
// Marshal the struct to a JSON string
jsonString, err := json.Marshal(info)
if err != nil {
return "", err // Return the error to the caller
}
var jsonData interface{}
err = json.Unmarshal([]byte(jsonString), &jsonData)
if err != nil {
return "", err // Return the error to the caller
}
formattedJSON, err := json.MarshalIndent(jsonData, "", " ")
if err != nil {
return "", err // Return the error to the caller
}
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)
*/

23
main.go
View File

@ -48,8 +48,21 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
findFile(w, "files/index.html") findFile(w, "files/index.html")
return 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" { if tmp == "/test" {
findFile(w, "files/repo.html") findFile(w, "files/test.html")
return return
} }
if tmp == "/skeleton.v2.css" { if tmp == "/skeleton.v2.css" {
@ -86,14 +99,6 @@ func findFile(w http.ResponseWriter, filename string) {
func main() { func main() {
accessf, _ = os.OpenFile("/home/jcarr/accessclient.log", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666) 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) 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() readconfigfile()
http.HandleFunc("/", okHandler) http.HandleFunc("/", okHandler)
https() https()