golang-exercises/query-cpu/cmd/server.go

100 lines
2.4 KiB
Go

/*
Copyright © 2024 Raul
*/
package cmd
import (
"bufio"
"fmt"
"log"
"net"
"os"
"strings"
"time"
"github.com/spf13/cobra"
)
// serverCmd represents the server command
var serverCmd = &cobra.Command{
Use: "server",
Short: "Start the server (default port: 8080)",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
server()
},
}
func init() {
rootCmd.AddCommand(serverCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// serverCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// serverCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
func server() {
ln, err := net.Listen("tcp", ":8080")
catchErr(err)
fmt.Println("Listening on port 8080...")
for {
conn, err := ln.Accept()
catchErr(err)
go handleConn(conn)
}
}
func handleConn(conn net.Conn) {
defer conn.Close()
message, err := bufio.NewReader(conn).ReadString('\n')
message = strings.TrimRight(message, "\n")
catchErr(err)
conn_IP := getIP(conn)
t := time.Now()
date := fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
final_message := fmt.Sprintf("[%v] Received connection from %v with message: %v\n", date, conn_IP, message)
writer(final_message)
reply_message := fmt.Sprintf("Your message was sent successfully\n")
conn.Write([]byte(reply_message))
fmt.Print(final_message)
}
func getIP(conn net.Conn) (IP string) {
if addr, ok := conn.RemoteAddr().(*net.TCPAddr); ok {
IP = fmt.Sprintf("%v", addr)
}
return IP
}
func catchErr(err error) (errHappened bool) {
errHappened = false
if err != nil {
errHappened = true
log.Fatalf("Error: %v\n", err)
os.Exit(2)
}
return errHappened
}
func writer(logstring string) {
file, err := os.OpenFile("./connections.log", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0640)
if err != nil {
log.Fatalf("Error occurred: %v\n", err)
}
defer file.Close()
file.WriteString(logstring)
}