Feat: done tcp

This commit is contained in:
2025-11-10 10:02:55 +08:00
parent 78d89595a1
commit 9a39bcda40
17 changed files with 689 additions and 0 deletions

40
cmds/server.go Normal file
View 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")
},
}