perf(web): improve logging
This commit is contained in:
parent
515beac231
commit
0c03d3a791
@ -41,6 +41,7 @@ func TokenGenerate(seed []byte) {
|
||||
value += int64(char) << (i * 8)
|
||||
}
|
||||
TOKEN = fmt.Sprintf("%x", sha256.Sum256([]byte(strconv.FormatInt(value, 16))))
|
||||
defer log.Println("Token generated")
|
||||
}
|
||||
|
||||
func Auth(handler http.HandlerFunc) http.HandlerFunc {
|
||||
@ -51,6 +52,7 @@ func Auth(handler http.HandlerFunc) http.HandlerFunc {
|
||||
handler.ServeHTTP(w, r)
|
||||
} else {
|
||||
http.Redirect(w, r, "/auth", http.StatusSeeOther)
|
||||
defer log.Println("Unauthorized request, redirecting to /auth")
|
||||
}
|
||||
}()
|
||||
token, err := r.Cookie("token")
|
||||
@ -68,6 +70,7 @@ func HandlerAuth(w http.ResponseWriter, r *http.Request) {
|
||||
var hash [sha256.Size]byte
|
||||
var passlen = sha256.Size
|
||||
var err error
|
||||
log.Println("Got authorization request")
|
||||
passfile, err := os.Open("/etc/tanabata/.htpasswd")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open password file: %s\n", err)
|
||||
@ -86,6 +89,7 @@ func HandlerAuth(w http.ResponseWriter, r *http.Request) {
|
||||
_, err = r.Body.Read(buffer)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
defer log.Println("Bad authorization request")
|
||||
return
|
||||
}
|
||||
for i := 0; i < sha256.Size; i++ {
|
||||
@ -97,7 +101,7 @@ func HandlerAuth(w http.ResponseWriter, r *http.Request) {
|
||||
hash = sha256.Sum256(buffer[:passlen])
|
||||
TokenGenerate(buffer)
|
||||
if bytes.Equal(hash[:], passhash) {
|
||||
TokenGenerate(buffer)
|
||||
log.Println("Password valid")
|
||||
response.Status = true
|
||||
response.Token = TOKEN
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
@ -105,15 +109,17 @@ func HandlerAuth(w http.ResponseWriter, r *http.Request) {
|
||||
Value: TOKEN,
|
||||
Expires: time.Now().Add(TOKEN_VALIDTIME * time.Second),
|
||||
})
|
||||
} else {
|
||||
log.Println("Password invalid")
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
jsonData, err := json.Marshal(response)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
log.Fatalf("Failed to marshal json: %s\n", err)
|
||||
}
|
||||
_, err = w.Write(jsonData)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
log.Fatalf("Failed to send response: %s\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -121,24 +127,28 @@ func HandlerTDBMS(w http.ResponseWriter, r *http.Request) {
|
||||
var request JSON
|
||||
var response []byte
|
||||
var err error
|
||||
log.Println("Got TDB request")
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 1048576)
|
||||
json_decoder := json.NewDecoder(r.Body)
|
||||
json_decoder.DisallowUnknownFields()
|
||||
err = json_decoder.Decode(&request)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
defer log.Println("Bad TDB request")
|
||||
return
|
||||
}
|
||||
response = tdbms.Query(request.TRDB, request.TRC, request.TRB)
|
||||
if response == nil {
|
||||
http.Error(w, "Failed to execute request", http.StatusInternalServerError)
|
||||
defer log.Println("Failed to execute request")
|
||||
return
|
||||
}
|
||||
if request.TRDB == "$TFM" && (request.TRC == 0b10000 || request.TRC == 0b101000) {
|
||||
var json_response JSON
|
||||
err = json.Unmarshal(response, &json_response)
|
||||
if err != nil {
|
||||
http.Error(w, "TDBMS error", http.StatusInternalServerError)
|
||||
http.Error(w, "Bad TDBMS response", http.StatusInternalServerError)
|
||||
defer log.Println("Bad TDBMS response")
|
||||
return
|
||||
}
|
||||
for index, element := range json_response.Data {
|
||||
@ -151,12 +161,17 @@ func HandlerTDBMS(w http.ResponseWriter, r *http.Request) {
|
||||
json_response.Data[index]["path"] = path
|
||||
}
|
||||
response, err = json.Marshal(json_response)
|
||||
if err != nil {
|
||||
log.Printf("Failed to marshal json: %s\n", err)
|
||||
}
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
_, err = w.Write(response)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
defer log.Printf("Failed to send TDBMS response: %s\n", err)
|
||||
return
|
||||
}
|
||||
defer log.Println("TDBMS response sent")
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user