dummy/src/main.go

67 lines
1.0 KiB
Go

package main
import (
"bufio"
"fmt"
"io"
"log"
"net"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
log.Println("\033[32mStarting Server\033[0m")
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM)
ln, _ := net.Listen("tcp", ":8081")
//cleanup
defer func() {
ln.Close()
}()
// capture sigterm
go func() {
sig := <-sigs
if sig == syscall.SIGTERM {
fmt.Println("\033[31mStopping server\033[0m")
os.Exit(0)
}
}()
//accepts command from cmd.go and sends them to the server
go func() {
//accept connections from clients
for {
conn, err := ln.Accept()
if err != nil {
continue
}
go handleClient(conn)
}
}()
//copy stdin
go func() {
io.Copy(os.Stdout, os.Stdin)
}()
i := 0
for {
fmt.Printf("Hello %d \n", i)
i += 1
time.Sleep(time.Second)
}
}
//take commands from cmd.go and send it to sdtin of the server
func handleClient(conn net.Conn) {
defer conn.Close()
cmd, _ := bufio.NewReader(conn).ReadString('\n')
fmt.Println(cmd)
}