mirror of
https://github.com/donl/gPanel.git
synced 2026-06-30 06:12:06 -06:00
59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
// Package logs is a child of package api to handle api calls concerning log files
|
|
package log
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/Ennovar/gPanel/pkg/file"
|
|
)
|
|
|
|
// Read function is accessed from api/logs/read and will attempt to read
|
|
// a given log based off of the request data.
|
|
func Read(res http.ResponseWriter, req *http.Request) bool {
|
|
if req.Method != "POST" {
|
|
http.Error(res, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
|
return false
|
|
}
|
|
|
|
var readLogRequestData struct {
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
err := json.NewDecoder(req.Body).Decode(&readLogRequestData)
|
|
if err != nil {
|
|
http.Error(res, err.Error(), http.StatusBadRequest)
|
|
return false
|
|
}
|
|
|
|
valid := false
|
|
for _, log := range file.KNOWN_LOGS {
|
|
if log == readLogRequestData.Name {
|
|
valid = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if !valid {
|
|
http.Error(res, "Attempting to read from an unknown log file.", http.StatusBadRequest)
|
|
return false
|
|
}
|
|
|
|
handle, err := file.Open(readLogRequestData.Name, true, true)
|
|
if err != nil {
|
|
http.Error(res, err.Error(), http.StatusBadRequest)
|
|
return false
|
|
}
|
|
defer handle.Close(false)
|
|
|
|
data, err := handle.Read()
|
|
if err != nil {
|
|
http.Error(res, err.Error(), http.StatusBadRequest)
|
|
return false
|
|
}
|
|
|
|
res.WriteHeader(http.StatusOK)
|
|
res.Write(data)
|
|
|
|
return true
|
|
}
|