diff --git a/app/method.go b/app/method.go index 9177b09..2da368e 100644 --- a/app/method.go +++ b/app/method.go @@ -2,12 +2,13 @@ package app import ( "encoding/json" - "fmt" "reflect" "regexp" "strings" "unsafe" + "golang.org/x/exp/slog" + "github.com/osuAkatsuki/akatsuki-api/common" "github.com/valyala/fasthttp" ) @@ -102,7 +103,7 @@ var callbackJSONP = regexp.MustCompile(`^[a-zA-Z_\$][a-zA-Z0-9_\$]*$`) func mkjson(c *fasthttp.RequestCtx, data interface{}) { exported, err := json.MarshalIndent(data, "", "\t") if err != nil { - fmt.Println(err) + slog.Error("Error marshalling JSON", "error", err.Error()) exported = []byte(`{ "code": 500, "message": "something has gone really really really really really really wrong." }`) } cb := string(c.URI().QueryArgs().Peek("callback")) diff --git a/app/tokens.go b/app/tokens.go index 3f62efa..a802f9c 100644 --- a/app/tokens.go +++ b/app/tokens.go @@ -5,6 +5,7 @@ import ( "crypto/sha256" "database/sql" "fmt" + "golang.org/x/exp/slog" "time" "github.com/jmoiron/sqlx" @@ -81,7 +82,7 @@ func tokenUpdater(db *sqlx.DB) { q = db.Rebind(q) _, err := db.Exec(q, a...) if err != nil { - fmt.Println(err) + slog.Error("Error updating tokens", "error", err.Error()) } } } diff --git a/app/v1/manage_user.go b/app/v1/manage_user.go index ba5799b..b06e108 100644 --- a/app/v1/manage_user.go +++ b/app/v1/manage_user.go @@ -4,6 +4,7 @@ import ( "database/sql" "encoding/json" "fmt" + "golang.org/x/exp/slog" "strings" "time" @@ -67,7 +68,7 @@ type userEditData struct { func UserEditPOST(md common.MethodData) common.CodeMessager { var data userEditData if err := md.Unmarshal(&data); err != nil { - fmt.Println(err) + slog.Error("Error unmarshalling", "error", err.Error()) return ErrBadJSON } diff --git a/app/v1/meta_linux.go b/app/v1/meta_linux.go index e6a807d..c95af7d 100644 --- a/app/v1/meta_linux.go +++ b/app/v1/meta_linux.go @@ -1,3 +1,4 @@ +//go:build !windows // +build !windows // TODO: Make all these methods POST @@ -7,12 +8,13 @@ package v1 import ( "io" "io/ioutil" - "log" "os" "os/exec" "syscall" "time" + "golang.org/x/exp/slog" + "github.com/osuAkatsuki/akatsuki-api/common" ) @@ -67,7 +69,7 @@ func MetaUpdateGET(md common.MethodData) common.CodeMessager { proc, err := os.FindProcess(syscall.Getpid()) if err != nil { - log.Println(err) + slog.Error("Couldn't find process", "error", err.Error()) return } proc.Signal(syscall.SIGUSR2) @@ -80,26 +82,26 @@ func execCommand(command string, args ...string) bool { cmd.Env = os.Environ() stdout, err := cmd.StdoutPipe() if err != nil { - log.Println(err) + slog.Error("Error getting stdout pipe", "error", err.Error()) return false } stderr, err := cmd.StderrPipe() if err != nil { - log.Println(err) + slog.Error("Error getting stderr pipe", "error", err.Error()) return false } if err := cmd.Start(); err != nil { - log.Println(err) + slog.Error("Error starting command", "error", err.Error()) return false } data, err := ioutil.ReadAll(stderr) if err != nil { - log.Println(err) + slog.Error("Error reading stderr", "error", err.Error()) return false } // Bob. We got a problem. if len(data) != 0 { - log.Println(string(data)) + slog.Error("Error running command", "error", string(data)) } io.Copy(os.Stdout, stdout) cmd.Wait() diff --git a/app/v1/rap.go b/app/v1/rap.go index f65f987..84cbbd1 100644 --- a/app/v1/rap.go +++ b/app/v1/rap.go @@ -1,9 +1,10 @@ package v1 import ( - "fmt" "time" + "golang.org/x/exp/slog" + "github.com/osuAkatsuki/akatsuki-api/common" ) @@ -31,7 +32,7 @@ func RAPLogPOST(md common.MethodData) common.CodeMessager { var d rapLogData if err := md.Unmarshal(&d); err != nil { - fmt.Println(err) + slog.Error("Error unmarshalling rap log data", "error", err.Error()) return ErrBadJSON } diff --git a/app/v1/token.go b/app/v1/token.go index 53ef201..7f7ce39 100644 --- a/app/v1/token.go +++ b/app/v1/token.go @@ -11,6 +11,7 @@ import ( "github.com/jmoiron/sqlx" "github.com/osuAkatsuki/akatsuki-api/common" + "golang.org/x/exp/slog" ) // TokenSelfDeletePOST deletes the token the user is connecting with. @@ -185,7 +186,7 @@ FROM tokens LEFT JOIN users ON users.id = tokens.user `+wc, params...) if err != nil { - fmt.Println(err) + slog.Error("Error fetching data", "error", err.Error()) return } for rows.Next() { @@ -198,7 +199,7 @@ LEFT JOIN users ON users.id = tokens.user ) err := rows.Scan(&id, &privsRaw, &privilegesRaw) if err != nil { - fmt.Println(err) + slog.Error("Error copying data", "error", err.Error()) continue } privileges := common.UserPrivileges(privilegesRaw) @@ -207,7 +208,7 @@ LEFT JOIN users ON users.id = tokens.user if newPrivs != privs { _, err := db.Exec("UPDATE tokens SET privileges = ? WHERE id = ? LIMIT 1", uint64(newPrivs), id) if err != nil { - fmt.Println(err) + slog.Error("Error updating tokens table", "error", err.Error()) continue } } diff --git a/app/v1/user.go b/app/v1/user.go index f9f8d29..4a7c634 100644 --- a/app/v1/user.go +++ b/app/v1/user.go @@ -8,6 +8,8 @@ import ( "strings" "unicode" + "golang.org/x/exp/slog" + "github.com/jmoiron/sqlx" "github.com/osuAkatsuki/akatsuki-api/common" "github.com/osuAkatsuki/akatsuki-api/externals" @@ -250,7 +252,7 @@ SELECT users_stats.ranked_score_mania, users_stats.total_score_mania, users_stats.playcount_mania, users_stats.playtime_mania, users_stats.replays_watched_mania, users_stats.total_hits_mania, users_stats.avg_accuracy_mania, users_stats.pp_mania, users_stats.max_combo_mania, - + rx_stats.ranked_score_std, rx_stats.total_score_std, rx_stats.playcount_std, users_stats.playtime_std, rx_stats.replays_watched_std, rx_stats.total_hits_std, rx_stats.avg_accuracy_std, rx_stats.pp_std, rx_stats.max_combo_std, @@ -540,7 +542,7 @@ func UserUnweightedPerformanceGET(md common.MethodData) common.CodeMessager { err := md.DB.QueryRow("SELECT SUM(pp) FROM scores"+tab+" WHERE userid = ? AND completed = 3 AND mode = ?", id, mode).Scan(&r.performance) if err != nil { if err == sql.ErrNoRows { - fmt.Println("User", id, "has no scores in scores"+tab, "???") + slog.Error("User has no scores", "user_id", id, "table", "scores"+tab) return r } return Err500 diff --git a/app/v1/user_achievements.go b/app/v1/user_achievements.go index bdccfff..679d8ab 100644 --- a/app/v1/user_achievements.go +++ b/app/v1/user_achievements.go @@ -2,9 +2,10 @@ package v1 import ( "database/sql" - "fmt" "time" + "golang.org/x/exp/slog" + "github.com/jmoiron/sqlx" "github.com/osuAkatsuki/akatsuki-api/common" ) @@ -25,7 +26,7 @@ func LoadAchievementsEvery(db *sqlx.DB, d time.Duration) { err := db.Select(&achievs, "SELECT id, name, description, icon FROM achievements ORDER BY id ASC") if err != nil { - fmt.Println("LoadAchievements error", err) + slog.Error("LoadAchievements error", "error", err.Error()) common.GenericError(err) } time.Sleep(d) diff --git a/app/websockets/multi.go b/app/websockets/multi.go index 6a44d50..59c4c9d 100644 --- a/app/websockets/multi.go +++ b/app/websockets/multi.go @@ -2,8 +2,9 @@ package websockets import ( "encoding/json" - "fmt" "sync" + + "golang.org/x/exp/slog" ) // SubscribeMultiMatches subscribes to receiving information from completed @@ -32,12 +33,12 @@ var multiSubscriptionsMtx = new(sync.RWMutex) func matchRetriever() { ps, err := red.Subscribe("api:mp_complete_match") if err != nil { - fmt.Println(err) + slog.Error("Error subscribing to api:mp_complete_match", "error", err.Error()) } for { msg, err := ps.ReceiveMessage() if err != nil { - fmt.Println(err.Error()) + slog.Error("Error receiving message from api:mp_complete_match", "error", err.Error()) return } go handleNewMultiGame(msg.Payload) diff --git a/app/websockets/scores.go b/app/websockets/scores.go index 71c45fa..aa2528f 100644 --- a/app/websockets/scores.go +++ b/app/websockets/scores.go @@ -2,12 +2,12 @@ package websockets import ( "encoding/json" - "fmt" "strings" "sync" - "github.com/osuAkatsuki/akatsuki-api/app/v1" + v1 "github.com/osuAkatsuki/akatsuki-api/app/v1" "github.com/osuAkatsuki/akatsuki-api/common" + "golang.org/x/exp/slog" "gopkg.in/thehowl/go-osuapi.v1" "zxq.co/x/getrank" ) @@ -22,6 +22,7 @@ func SubscribeScores(c *conn, message incomingMessage) { var ssu []subscribeScoresUser err := json.Unmarshal(message.Data, &ssu) if err != nil { + slog.Error("Error unmarshalling subscribe scores user", "error", err.Error()) c.WriteJSON(TypeInvalidMessage, err.Error()) return } @@ -58,12 +59,12 @@ var scoreSubscriptionsMtx = new(sync.RWMutex) func scoreRetriever() { ps, err := red.Subscribe("api:score_submission") if err != nil { - fmt.Println(err) + slog.Error("Error subscribing to api:score_submission", "error", err.Error()) } for { msg, err := ps.ReceiveMessage() if err != nil { - fmt.Println(err.Error()) + slog.Error("Error receiving message from api:score_submission", "error", err.Error()) return } go handleNewScore(msg.Payload) @@ -100,7 +101,7 @@ FROM scores s INNER JOIN users u ON s.userid = u.id WHERE s.id = ?`, id) if err != nil { - fmt.Println(err) + slog.Error("Error fetching score", "error", err.Error()) return } s.Rank = strings.ToUpper(getrank.GetRank( @@ -167,7 +168,7 @@ func catchPanic() { case error: common.WSErr(r) default: - fmt.Println("PANIC", r) + slog.Error("PANIC", "error", r) } } } diff --git a/common/method_data.go b/common/method_data.go index faf3054..7361ce4 100644 --- a/common/method_data.go +++ b/common/method_data.go @@ -2,7 +2,6 @@ package common import ( "encoding/json" - "fmt" "runtime" "strconv" "strings" @@ -10,6 +9,7 @@ import ( "github.com/getsentry/raven-go" "github.com/jmoiron/sqlx" "github.com/valyala/fasthttp" + "golang.org/x/exp/slog" "gopkg.in/redis.v5" ) @@ -81,9 +81,10 @@ func GenericError(err error) { func _err(err error, tags map[string]string, user *raven.User, c *fasthttp.RequestCtx) { _, file, no, ok := runtime.Caller(2) if ok { - fmt.Println("ERROR in", file, "at line", no) + slog.Error("An error occurred", "filename", file, "line", no, "error", err.Error()) + } else { + slog.Error("An error occurred", "error", err.Error()) } - fmt.Println(err) } func generateRavenHTTP(ctx *fasthttp.RequestCtx) *raven.Http { diff --git a/go.mod b/go.mod index 7c630f0..9bebf91 100644 --- a/go.mod +++ b/go.mod @@ -33,6 +33,7 @@ require ( github.com/onsi/gomega v1.13.0 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect + golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa golang.org/x/net v0.10.0 // indirect - golang.org/x/sys v0.8.0 // indirect + golang.org/x/sys v0.14.0 // indirect ) diff --git a/go.sum b/go.sum index 881066f..e9f9ec5 100644 --- a/go.sum +++ b/go.sum @@ -84,6 +84,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa h1:FRnLl4eNAQl8hwxVVC17teOw8kdjVDVAiFMtgUdTSRQ= +golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa/go.mod h1:zk2irFbV9DP96SEBUUAy67IdHUaZuSnrz1n472HUCLE= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= @@ -117,6 +119,7 @@ golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/limit/limit.go b/limit/limit.go index fe5d538..93b73f8 100644 --- a/limit/limit.go +++ b/limit/limit.go @@ -1,9 +1,10 @@ package limit import ( - "fmt" "sync" "time" + + "golang.org/x/exp/slog" ) // Request is a Request with DefaultLimiter. @@ -82,7 +83,7 @@ func (s *RateLimiter) filler(el string, perMinute int) { defer func() { r := recover() if r != nil { - fmt.Println(r) + slog.Error("PANIC", "error", r) } }() diff --git a/main.go b/main.go index 6b8926d..3446e14 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,8 @@ import ( "log" "syscall" + "golang.org/x/exp/slog" + "github.com/osuAkatsuki/akatsuki-api/app" "github.com/osuAkatsuki/akatsuki-api/beatmapget" "github.com/osuAkatsuki/akatsuki-api/common" @@ -23,9 +25,7 @@ func init() { } func main() { - - fmt.Print("Akatsuki API") - fmt.Println() + slog.Info("Akatsuki API") settings := common.LoadSettings() @@ -39,7 +39,7 @@ func main() { db, err := sqlx.Open(settings.DB_SCHEME, dns) if err != nil { - log.Fatalln(err) + slog.Error("Error opening DB connection", "error", err.Error()) } db.MapperFunc(func(s string) string { @@ -56,7 +56,7 @@ func main() { err = fasthttp.ListenAndServe(fmt.Sprintf(":%d", settings.APP_PORT), engine.Handler) if err != nil { - log.Fatalln(err) + slog.Error("Unable to start server", "error", err.Error()) } }