Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(handlers): migrate from fmt to structured logging #2268

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions handlers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (ah *authHandler) CreateConnectionCode(w http.ResponseWriter, r *http.Reque

body, err := io.ReadAll(r.Body)
if err != nil {
fmt.Println("ReadAll Error", err)
utils.Log.Error("ReadAll Error: %v", err)
w.WriteHeader(http.StatusBadRequest)
return
}
Expand All @@ -72,7 +72,7 @@ func (ah *authHandler) CreateConnectionCode(w http.ResponseWriter, r *http.Reque
err = json.Unmarshal(body, &codeBody)

if err != nil {
fmt.Println("Could not unmarshal connection code body")
utils.Log.Error("Could not unmarshal connection code body")
w.WriteHeader(http.StatusNotAcceptable)
return
}
Expand Down Expand Up @@ -110,7 +110,7 @@ func (ah *authHandler) CreateConnectionCode(w http.ResponseWriter, r *http.Reque
_, err = ah.db.CreateConnectionCode(codeArr)

if err != nil {
fmt.Println("[auth] => ERR create connection code", err)
utils.Log.Error("[auth] => ERR create connection code: %v", err)
w.WriteHeader(http.StatusBadRequest)
return
}
Expand Down Expand Up @@ -149,7 +149,7 @@ func MakeConnectionCodeRequest(inviter_pubkey string, inviter_route_hint string,
err = json.Unmarshal(body, &inviteReponse)

if err != nil {
fmt.Println("Could not get connection code")
utils.Log.Error("Could not get connection code")
return ""
}

Expand Down Expand Up @@ -202,7 +202,7 @@ func ReceiveLnAuthData(w http.ResponseWriter, r *http.Request) {

exVerify, err := auth.VerifyDerSig(sig, k1, userKey)
if err != nil || !exVerify {
fmt.Println("[auth] Error signing signature")
utils.Log.Error("[auth] Error signing signature")
w.WriteHeader(http.StatusUnauthorized)
json.NewEncoder(w).Encode(err.Error())
return
Expand All @@ -221,7 +221,7 @@ func ReceiveLnAuthData(w http.ResponseWriter, r *http.Request) {
tokenString, err := auth.EncodeJwt(userKey)

if err != nil {
fmt.Println("[auth] error creating LNAUTH JWT")
utils.Log.Error("[auth] error creating LNAUTH JWT")
w.WriteHeader(http.StatusNotAcceptable)
json.NewEncoder(w).Encode(err.Error())
return
Expand All @@ -245,7 +245,7 @@ func ReceiveLnAuthData(w http.ResponseWriter, r *http.Request) {
socket.Conn.WriteJSON(socketMsg)
db.Store.DeleteCache(k1[0:20])
} else {
fmt.Println("[auth] Socket Error", err)
utils.Log.Error("[auth] Socket Error: %v", err)
}

responseMsg["status"] = "OK"
Expand All @@ -265,7 +265,7 @@ func (ah *authHandler) RefreshToken(w http.ResponseWriter, r *http.Request) {
claims, err := ah.decodeJwt(token)

if err != nil {
fmt.Println("[auth] Failed to parse JWT")
utils.Log.Error("[auth] Failed to parse JWT")
w.WriteHeader(http.StatusUnauthorized)
json.NewEncoder(w).Encode(err.Error())
return
Expand All @@ -280,7 +280,7 @@ func (ah *authHandler) RefreshToken(w http.ResponseWriter, r *http.Request) {
tokenString, err := ah.encodeJwt(pubkey)

if err != nil {
fmt.Println("[auth] error creating refresh JWT")
utils.Log.Error("[auth] error creating refresh JWT")
w.WriteHeader(http.StatusNotAcceptable)
json.NewEncoder(w).Encode(err.Error())
return
Expand Down
12 changes: 6 additions & 6 deletions handlers/bots.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package handlers

import (
"encoding/json"
"fmt"
"io"
"net/http"
"regexp"
Expand All @@ -13,6 +12,7 @@ import (
"github.com/go-chi/chi"
"github.com/stakwork/sphinx-tribes/auth"
"github.com/stakwork/sphinx-tribes/db"
"github.com/stakwork/sphinx-tribes/utils"
)

type botHandler struct {
Expand All @@ -36,7 +36,7 @@ func (bt *botHandler) CreateOrEditBot(w http.ResponseWriter, r *http.Request) {
r.Body.Close()
err = json.Unmarshal(body, &bot)
if err != nil {
fmt.Println(err)
utils.Log.Error("%v", err)
w.WriteHeader(http.StatusNotAcceptable)
return
}
Expand All @@ -50,7 +50,7 @@ func (bt *botHandler) CreateOrEditBot(w http.ResponseWriter, r *http.Request) {

extractedPubkey, err := bt.verifyTribeUUID(bot.UUID, false)
if err != nil {
fmt.Println(err)
utils.Log.Error("%v", err)
w.WriteHeader(http.StatusUnauthorized)
return
}
Expand All @@ -70,7 +70,7 @@ func (bt *botHandler) CreateOrEditBot(w http.ResponseWriter, r *http.Request) {

_, err = bt.db.CreateOrEditBot(bot)
if err != nil {
fmt.Println("=> ERR createOrEditBot", err)
utils.Log.Error("=> ERR createOrEditBot: %v", err)
w.WriteHeader(http.StatusBadRequest)
return
}
Expand Down Expand Up @@ -127,7 +127,7 @@ func (bt *botHandler) DeleteBot(w http.ResponseWriter, r *http.Request) {

uuid := chi.URLParam(r, "uuid")

fmt.Println("uuid: ", uuid)
utils.Log.Info("uuid: %s", uuid)

if uuid == "" {
w.WriteHeader(http.StatusUnauthorized)
Expand All @@ -136,7 +136,7 @@ func (bt *botHandler) DeleteBot(w http.ResponseWriter, r *http.Request) {

extractedPubkey, err := bt.verifyTribeUUID(uuid, false)
if err != nil {
fmt.Println(err)
utils.Log.Error("%v", err)
w.WriteHeader(http.StatusUnauthorized)
return
}
Expand Down
16 changes: 8 additions & 8 deletions handlers/bounties.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package handlers

import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
Expand All @@ -11,6 +10,7 @@ import (

"github.com/lib/pq"
"github.com/stakwork/sphinx-tribes/db"
"github.com/stakwork/sphinx-tribes/utils"
)

func GetWantedsHeader(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -54,7 +54,7 @@ func DeleteBountyAssignee(w http.ResponseWriter, r *http.Request) {
err = json.Unmarshal(body, &invoice)

if err != nil {
fmt.Println(err)
utils.Log.Error("%v", err)
w.WriteHeader(http.StatusNotAcceptable)
return
}
Expand Down Expand Up @@ -91,17 +91,17 @@ func MigrateBounties(w http.ResponseWriter, r *http.Request) {
peeps := db.DB.GetAllPeople()

for indexPeep, peep := range peeps {
fmt.Println("peep: ", indexPeep)
utils.Log.Info("peep: %d", indexPeep)
bounties, ok := peep.Extras["wanted"].([]interface{})

if !ok {
fmt.Println("Wanted not there")
utils.Log.Info("Wanted not there")
continue
}

for index, bounty := range bounties {

fmt.Println("looping bounties: ", index)
utils.Log.Info("looping bounties: %d", index)
migrateBounty := bounty.(map[string]interface{})

migrateBountyFinal := db.Bounty{}
Expand Down Expand Up @@ -156,8 +156,8 @@ func MigrateBounties(w http.ResponseWriter, r *http.Request) {
if !ok7 {
migrateBountyFinal.Created = 0
} else {
fmt.Println(reflect.TypeOf(CreatedInt64))
fmt.Println("Timestamp:", CreatedInt64)
utils.Log.Info("Type: %v", reflect.TypeOf(CreatedInt64))
utils.Log.Info("Timestamp: %d", CreatedInt64)
migrateBountyFinal.Created = CreatedInt64
}

Expand Down Expand Up @@ -237,7 +237,7 @@ func MigrateBounties(w http.ResponseWriter, r *http.Request) {
} else {
migrateBountyFinal.EstimatedCompletionDate = EstimatedCompletionDate
}
fmt.Println("Bounty about to be added ")
utils.Log.Info("Bounty about to be added ")
db.DB.AddBounty(migrateBountyFinal)
//Migrate the bounties here
}
Expand Down
Loading
Loading