Skip to content

Commit

Permalink
use slog
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishek9686 committed Aug 29, 2023
1 parent 64370f2 commit a986db6
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 15 deletions.
7 changes: 4 additions & 3 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"golang.org/x/crypto/bcrypt"
"golang.org/x/exp/slog"
"golang.org/x/oauth2"

"github.com/gorilla/websocket"
Expand Down Expand Up @@ -240,7 +241,7 @@ func HandleHeadlessSSO(w http.ResponseWriter, r *http.Request) {
func addUser(email string) error {
var hasSuperAdmin, err = logic.HasSuperAdmin()
if err != nil {
logger.Log(1, "error checking for existence of admin user during OAuth login for", email, "; user not added")
slog.Error("error checking for existence of admin user during OAuth login for", "email", email, "error", err)
return err
} // generate random password to adapt to current model
var newPass, fetchErr = fetchPassValue("")
Expand All @@ -253,9 +254,9 @@ func addUser(email string) error {
}
if !hasSuperAdmin { // must be first attempt, create a superadmin
if err = logic.CreateSuperAdmin(&newUser); err != nil {
logger.Log(1, "error creating super admin from user,", email, "; user not added")
slog.Error("error creating super admin from user", "email", email, "error", err)
} else {
logger.Log(1, "superadmin created from user,", email, "; was first user added")
slog.Info("superadmin created from user", "email", email)
}
} else { // otherwise add to db as admin..?
// TODO: add ability to add users with preemptive permissions
Expand Down
3 changes: 1 addition & 2 deletions controllers/ext_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ func getAllExtClients(w http.ResponseWriter, r *http.Request) {
networksSlice := []string{}
marshalErr := json.Unmarshal([]byte(headerNetworks), &networksSlice)
if marshalErr != nil {
logger.Log(0, "error unmarshalling networks: ",
marshalErr.Error())
slog.Error("error unmarshalling networks", "error", marshalErr.Error())
logic.ReturnErrorResponse(w, r, logic.FormatError(marshalErr, "internal"))
return
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func getNetworks(w http.ResponseWriter, r *http.Request) {

allnetworks, err := logic.GetNetworks()
if err != nil && !database.IsEmptyRecord(err) {
logger.Log(0, r.Header.Get("user"), "failed to fetch networks: ", err.Error())
slog.Error("failed to fetch networks", "error", err.Error())
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
return
}
Expand Down
14 changes: 5 additions & 9 deletions controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,7 @@ func createSuperAdmin(w http.ResponseWriter, r *http.Request) {

err := json.NewDecoder(r.Body).Decode(&u)
if err != nil {

logger.Log(0, u.UserName, "error decoding request body: ",
err.Error())
slog.Error("error decoding request body", "error", err.Error())
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
return
}
Expand All @@ -222,19 +220,17 @@ func createSuperAdmin(w http.ResponseWriter, r *http.Request) {

err = logic.CreateSuperAdmin(&u)
if err != nil {
logger.Log(0, u.UserName, "failed to create admin: ",
err.Error())
slog.Error("failed to create admin", "error", err.Error())
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
return
}

logger.Log(1, u.UserName, "was made a super admin")
json.NewEncoder(w).Encode(logic.ToReturnUser(u))
}

// swagger:route POST /api/users/adm/transfersuperadmin user transferSuperAdmin
//
// Transfers suoeradmin role to an admin user.
// Transfers superadmin role to an admin user.
//
// Schemes: https
//
Expand Down Expand Up @@ -330,11 +326,11 @@ func createUser(w http.ResponseWriter, r *http.Request) {

err = logic.CreateUser(&user)
if err != nil {
logger.Log(0, user.UserName, "error creating new user: ", err.Error())
slog.Error("error creating new user: ", "user", user.UserName, "error", err.Error())
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
return
}
logger.Log(1, user.UserName, "was created")
slog.Info("user was created", "username", user.UserName)
json.NewEncoder(w).Encode(logic.ToReturnUser(user))
}

Expand Down

0 comments on commit a986db6

Please sign in to comment.