mirror of
https://github.com/hanzozt/sdk-golang.git
synced 2026-10-30 14:46:37 +00:00
- Repo names: ziti-* → zt-* - Go modules: github.com/hanzozt/ziti → github.com/hanzozt/zt - Docker images: hanzozt/ziti-* → hanzozt/zt-* - C/Java/JS symbols: ziti_* → zt_* - File and directory names updated
61 lines
1.1 KiB
Go
61 lines
1.1 KiB
Go
package cmd
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"github.com/hanzozt/sdk-golang/zt"
|
|
"net"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
func Server(ztCfg *zt.Config, serviceName string) {
|
|
ctx, err := zt.NewContext(ztCfg)
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
listener, err := ctx.Listen(serviceName)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
serve(listener)
|
|
|
|
sig := make(chan os.Signal)
|
|
s := <-sig
|
|
log.Infof("received %s: shutting down...", s)
|
|
}
|
|
|
|
func serve(listener net.Listener) {
|
|
log.Infof("ready to accept connections")
|
|
for {
|
|
conn, _ := listener.Accept()
|
|
log.Infof("new connection accepted")
|
|
go accept(conn)
|
|
}
|
|
}
|
|
|
|
func accept(conn net.Conn) {
|
|
if conn == nil {
|
|
panic("connection is nil!")
|
|
}
|
|
writer := bufio.NewWriter(conn)
|
|
reader := bufio.NewReader(conn)
|
|
rw := bufio.NewReadWriter(reader, writer)
|
|
//line delimited
|
|
for {
|
|
line, err := rw.ReadString('\n')
|
|
if err != nil {
|
|
log.Error(err)
|
|
break
|
|
}
|
|
log.Info("about to read a string :")
|
|
log.Infof(" read : %s", strings.TrimSpace(line))
|
|
resp := fmt.Sprintf("you sent me: %s", line)
|
|
_, _ = rw.WriteString(resp)
|
|
_ = rw.Flush()
|
|
log.Infof(" responding with : %s", strings.TrimSpace(resp))
|
|
}
|
|
}
|