mirror of
https://github.com/mochi-mqtt/server.git
synced 2025-09-27 04:26:23 +08:00

* Begin adding new slog calls * Fixed unit tests * Add leveler example * Add debug log level to Redis example * Change location of server.Close() and add logs to example/hooks * Begin removing references to zerolog * Removed final references to zerolog * Change where server.Close() occurs in main * Change to 1.21 to remove x dependency * Add slog * Update references to 1.21 * Begin change of LogAttrs to standard logging interface * Change the rest of LogAttrs to default * Fix bad log * Update badger.go Changing "data" to "key" or "id" here might be more appropriate. * Update badger.go Changing "data" to "key" or "id" here might be more appropriate. * Update server.go Not checking if err is equal to nil * Update server.go printing information for ID or error is missing. * Change references of err.Error() to err in slog * Remove missed removal of Error() references for logging --------- Co-authored-by: Derek Duncan <dduncan@atlassian.com> Co-authored-by: Derek Duncan <derekduncan@gmail.com> Co-authored-by: JB <28275108+mochi-co@users.noreply.github.com> Co-authored-by: werbenhu <werben@qq.com>
69 lines
1.4 KiB
Go
69 lines
1.4 KiB
Go
// SPDX-License-Identifier: MIT
|
|
// SPDX-FileCopyrightText: 2022 mochi-mqtt, mochi-co
|
|
// SPDX-FileContributor: mochi-co
|
|
|
|
package main
|
|
|
|
import (
|
|
"log"
|
|
"log/slog"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
mqtt "github.com/mochi-mqtt/server/v2"
|
|
"github.com/mochi-mqtt/server/v2/hooks/auth"
|
|
"github.com/mochi-mqtt/server/v2/hooks/storage/redis"
|
|
"github.com/mochi-mqtt/server/v2/listeners"
|
|
|
|
rv8 "github.com/go-redis/redis/v8"
|
|
)
|
|
|
|
func main() {
|
|
sigs := make(chan os.Signal, 1)
|
|
done := make(chan bool, 1)
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
|
go func() {
|
|
<-sigs
|
|
done <- true
|
|
}()
|
|
|
|
server := mqtt.New(nil)
|
|
_ = server.AddHook(new(auth.AllowHook), nil)
|
|
|
|
level := new(slog.LevelVar)
|
|
server.Log = slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
|
Level: level,
|
|
}))
|
|
level.Set(slog.LevelDebug)
|
|
|
|
err := server.AddHook(new(redis.Hook), &redis.Options{
|
|
Options: &rv8.Options{
|
|
Addr: "localhost:6379", // default redis address
|
|
Password: "", // your password
|
|
DB: 0, // your redis db
|
|
},
|
|
})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
tcp := listeners.NewTCP("t1", ":1883", nil)
|
|
err = server.AddListener(tcp)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
go func() {
|
|
err := server.Serve()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}()
|
|
|
|
<-done
|
|
server.Log.Warn("caught signal, stopping...")
|
|
server.Close()
|
|
server.Log.Info("main.go finished")
|
|
}
|