dummy/src/main.go

67 lines
1.0 KiB
Go
Raw Normal View History

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