Feat: done tcp
This commit is contained in:
40
cmds/server.go
Normal file
40
cmds/server.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package cmds
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"inp2025/middlewares"
|
||||
"inp2025/tcp"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func pongHandler(w tcp.ResponseWriter, req *tcp.Request) error {
|
||||
w.WriteHeader(tcp.StatusOK)
|
||||
_, err := io.WriteString(w, string(req.Body))
|
||||
return err
|
||||
}
|
||||
|
||||
func tickHandler(w tcp.ResponseWriter, req *tcp.Request) error {
|
||||
zap.L().Info("Run tickHandler")
|
||||
for {
|
||||
w.SocketSendString(fmt.Sprintf("time: %s", time.Now().String()))
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var serverCmd = &cobra.Command{
|
||||
Use: "server",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
router := tcp.NewRouter()
|
||||
router.Use(middlewares.ErrorHandler)
|
||||
router.Use(middlewares.AccessLog)
|
||||
router.Register(tcp.MethodSOCKET, "/", tickHandler)
|
||||
router.Register(tcp.MethodPOST, "/", pongHandler)
|
||||
|
||||
router.Listen(":8080")
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user