You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.8 KiB
Go
92 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/noahlann/nnet/pkg/nnet"
|
|
)
|
|
|
|
func main() {
|
|
var (
|
|
addr = flag.String("addr", "tcp://:8080", "Server address")
|
|
protocol = flag.String("protocol", "", "Transport protocol (tcp, udp, websocket, unix, serial). If empty, auto-detect from addr")
|
|
multicore = flag.Bool("multicore", true, "Enable multicore")
|
|
readBuf = flag.Int("read-buf", 4096, "Read buffer size")
|
|
writeBuf = flag.Int("write-buf", 4096, "Write buffer size")
|
|
maxConns = flag.Int("max-conns", 10000, "Max connections")
|
|
// codec
|
|
defaultCodec = flag.String("default-codec", "binary", "Default codec (binary, plain, json, msgpack, protobuf)")
|
|
enableProtoEnc = flag.Bool("enable-proto-encode", false, "Enable protocol-level encode before codec")
|
|
// metrics
|
|
metricsEnable = flag.Bool("metrics-enable", false, "Enable metrics endpoint")
|
|
metricsPort = flag.Int("metrics-port", 0, "Metrics HTTP port (0 disables separate port)")
|
|
metricsPath = flag.String("metrics-path", "/metrics", "Metrics HTTP path")
|
|
// session
|
|
sessionStorage = flag.String("session-storage", "memory", "Session storage (memory|file|redis)")
|
|
sessionPath = flag.String("session-path", "./sessions", "Session path for file storage")
|
|
sessionExpiration = flag.Duration("session-expiration", 24*time.Hour, "Session expiration duration")
|
|
)
|
|
flag.Parse()
|
|
|
|
// 创建服务器配置
|
|
cfg := &nnet.Config{
|
|
Addr: *addr,
|
|
TransportProtocol: *protocol, // 如果为空,会根据地址前缀自动识别
|
|
Multicore: *multicore,
|
|
ReadBufferSize: *readBuf,
|
|
WriteBufferSize: *writeBuf,
|
|
MaxConnections: *maxConns,
|
|
Metrics: &nnet.MetricsConfig{
|
|
Enabled: *metricsEnable,
|
|
Path: *metricsPath,
|
|
Port: *metricsPort,
|
|
},
|
|
Session: &nnet.SessionConfig{
|
|
Storage: *sessionStorage,
|
|
Expiration: *sessionExpiration,
|
|
Path: *sessionPath,
|
|
},
|
|
Codec: &nnet.CodecConfig{
|
|
DefaultCodec: *defaultCodec,
|
|
EnableProtocolEncode: *enableProtoEnc,
|
|
},
|
|
}
|
|
|
|
// 创建服务器
|
|
server, err := nnet.NewServer(cfg)
|
|
if err != nil {
|
|
fmt.Printf("Failed to create server: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// 注册路由
|
|
server.Router().RegisterString("/hello", func(ctx nnet.Context) error {
|
|
return ctx.Response().WriteString("Hello, World!")
|
|
})
|
|
|
|
server.Router().RegisterString("/ping", func(ctx nnet.Context) error {
|
|
return ctx.Response().WriteString("pong")
|
|
})
|
|
|
|
// 启动服务器
|
|
if err := server.Start(); err != nil {
|
|
fmt.Printf("Failed to start server: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
defer server.Stop()
|
|
|
|
fmt.Printf("Server started on %s\n", *addr)
|
|
|
|
// 等待中断信号
|
|
sigChan := make(chan os.Signal, 1)
|
|
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
|
<-sigChan
|
|
|
|
fmt.Println("Server stopped")
|
|
}
|