246 lines
4.4 KiB
Go
246 lines
4.4 KiB
Go
|
|
package cmpSnipper
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
func (app *App) HttpRespText(w http.ResponseWriter, data []byte) {
|
|
w.Write(data)
|
|
|
|
return
|
|
}
|
|
|
|
func (app *App) HttpRespJson(w http.ResponseWriter, data any) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
jsonResp, err := json.MarshalIndent(data, "", "\t")
|
|
|
|
if err != nil {
|
|
app.Log("Error happened in JSON marshal. Err: %s", err)
|
|
return
|
|
}
|
|
|
|
app.HttpRespText(w, jsonResp)
|
|
}
|
|
|
|
func HttpRespCodeText(w http.ResponseWriter, req *http.Request, code int, cont string, mesg any) {
|
|
w.WriteHeader(code)
|
|
|
|
w.Header().Set("Content-Type", cont)
|
|
|
|
|
|
}
|
|
|
|
func (app *App) HttpRespForbidden(w http.ResponseWriter, req *http.Request, mesg string) {
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
resp := make(map[string]any)
|
|
resp["code"] = 403
|
|
resp["status"] = "Forbidden"
|
|
resp["message"] = mesg
|
|
|
|
app.HttpRespJson(w, resp)
|
|
}
|
|
|
|
|
|
|
|
func (app *App) HttpReqDir1(w http.ResponseWriter, req *http.Request, targ string, path string) {
|
|
resp := map[string]interface{}{
|
|
"path" : path,
|
|
"directory": targ,
|
|
"content" : nil,
|
|
}
|
|
|
|
entries, err := os.ReadDir(targ)
|
|
|
|
if err != nil {
|
|
app.Log(err)
|
|
app.HttpRespForbidden(w, req, "Server error")
|
|
}
|
|
|
|
var slice []interface{};
|
|
|
|
for _, e := range entries {
|
|
slice = append(slice, map[string]interface{}{
|
|
"name": e.Name(),
|
|
})
|
|
|
|
// fmt.Println(e.Name());
|
|
}
|
|
|
|
resp["content"] = slice
|
|
|
|
app.HttpRespJson(w, resp)
|
|
|
|
return
|
|
}
|
|
|
|
func (app *App) HttpReqDir(w http.ResponseWriter, req *http.Request, targ string, path string) {
|
|
finx := path + "/index.html"
|
|
|
|
data, err := app.FileReadLimit(finx, 1048576);
|
|
|
|
if os.IsNotExist(err) {
|
|
app.HttpReqDir1(w, req, targ, path)
|
|
return
|
|
}
|
|
|
|
if err == app.ErrIsDir {
|
|
app.HttpReqDir1(w, req, targ, path)
|
|
return
|
|
}
|
|
|
|
if err != nil {
|
|
app.Log("Err: %s", err)
|
|
app.HttpRespForbidden(w, req, "Server error")
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
|
|
|
app.HttpRespText(w, data)
|
|
|
|
return
|
|
}
|
|
|
|
func (app *App) HttpReq(w http.ResponseWriter, req *http.Request) {
|
|
// https://go.dev/src/net/http/request.go
|
|
|
|
// fmt.Printf("Request: %+v\n", req)
|
|
|
|
var auri = (strings.Split(req.RequestURI, "/"))
|
|
auri[0] = "/"
|
|
|
|
var aidx = 0
|
|
|
|
app.Log("Auri: %+v", auri)
|
|
|
|
var locTarg map[string]interface{}
|
|
var locEnd bool
|
|
|
|
locList, err := app.ConfGetArr("http", "location")
|
|
|
|
if err != nil {
|
|
app.HttpRespForbidden(w, req, "Invalid http.location")
|
|
return
|
|
}
|
|
|
|
for uid, uri := range auri {
|
|
locEnd = false
|
|
|
|
for _, locIfc := range locList {
|
|
var locMap map[string]interface{};
|
|
|
|
switch v := locIfc.(type) {
|
|
case map[string]interface{}:
|
|
locMap = v;
|
|
}
|
|
|
|
if locMap == nil {
|
|
app.Log("Not Found")
|
|
continue
|
|
}
|
|
|
|
locUri := locMap["uri"]
|
|
|
|
if 1 == 0 {
|
|
// fmt.Printf("Found: %+v\n", locUri)
|
|
continue
|
|
}
|
|
|
|
if locUri != uri {
|
|
// fmt.Printf(" %s != %s\n", locUri, uri)
|
|
continue
|
|
}
|
|
|
|
// fmt.Printf("Found: %s\n", uri)
|
|
|
|
locTarg = locMap
|
|
locEnd = true
|
|
aidx = uid
|
|
|
|
switch v := locMap["list"].(type) {
|
|
case []interface{}:
|
|
locList = v;
|
|
// fmt.Printf("Found list\n");
|
|
}
|
|
}
|
|
|
|
if !locEnd {
|
|
break
|
|
}
|
|
}
|
|
/*
|
|
fmt.Printf("Targ %+v\n", locTarg);
|
|
fmt.Printf("Targ %+v\n", locEnd );
|
|
fmt.Printf("Targ %s \n", locTarg["uri"] );
|
|
fmt.Printf("Targ %+v\n", auri[(aidx+1):]);
|
|
*/
|
|
switch locTarg["type"] {
|
|
case "json":
|
|
targ := locTarg["targ"].(string)
|
|
|
|
app.HttpRespJson(w, locTarg[targ])
|
|
|
|
return
|
|
|
|
case "func":
|
|
targ := locTarg["targ"].(string)
|
|
parm := locTarg["parm"]
|
|
|
|
resp, err := app.CallFuncByName(targ, parm)
|
|
|
|
if err != nil {
|
|
app.Log("Err: %s", err)
|
|
break
|
|
}
|
|
|
|
app.HttpRespJson(w, resp)
|
|
|
|
return
|
|
|
|
case "directory":
|
|
targ := locTarg["targ"].(string)
|
|
|
|
path := targ + "/" + strings.Join(auri[(aidx+1):], "/")
|
|
|
|
app.Log("Location: %s", path)
|
|
|
|
data, err := app.FileReadLimit(path, 1048576);
|
|
|
|
if os.IsNotExist(err) {
|
|
break
|
|
}
|
|
|
|
if err == app.ErrIsDir {
|
|
app.HttpReqDir(w, req, targ, path)
|
|
return
|
|
}
|
|
|
|
if err != nil {
|
|
app.Log("Err: %s", err)
|
|
app.HttpRespForbidden(w, req, "Server error")
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
|
|
|
app.HttpRespText(w, data)
|
|
|
|
return
|
|
|
|
case "redirect":
|
|
http.Redirect(w, req, locTarg["targ"].(string), int(locTarg["code"].(float64)))
|
|
|
|
return
|
|
|
|
// case "ws":
|
|
}
|
|
|
|
app.HttpRespForbidden(w, req, "Invalid type")
|
|
}
|