Create server structure
This commit is contained in:
parent
a0b0332cd4
commit
09a9f9efb7
|
@ -5,25 +5,23 @@ Copyright © 2024 Raul <raul@bulgariu.xyz>
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// serverCmd represents the server command
|
// serverCmd represents the server command
|
||||||
var serverCmd = &cobra.Command{
|
var serverCmd = &cobra.Command{
|
||||||
Use: "server",
|
Use: "server",
|
||||||
Short: "A brief description of your command",
|
Short: "Tiny chat server",
|
||||||
Long: `A longer description that spans multiple lines and likely contains examples
|
Long: `Refactored mini-chat server designed to be connected to
|
||||||
and usage of using your command. For example:
|
using a proper interface this time, not using netcat.
|
||||||
|
|
||||||
Cobra is a CLI library for Go that empowers applications.
|
Example:
|
||||||
This application is a tool to generate the needed files
|
./tiny-chat server --port 1337`,
|
||||||
to quickly create a Cobra application.`,
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
|
||||||
if err := setParameters(cmd); err != nil {
|
if err := setServerParameters(cmd); err != nil {
|
||||||
log.Fatalln("Error: Port could not be read!")
|
log.Fatalf("Error happened trying to set parameters: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
Server()
|
Server()
|
||||||
|
@ -45,13 +43,13 @@ func init() {
|
||||||
// serverCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
// serverCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||||
}
|
}
|
||||||
|
|
||||||
func setParameters(cmd *cobra.Command) error {
|
func setServerParameters(cmd *cobra.Command) error {
|
||||||
parameterPort, err := cmd.Flags().GetString("port")
|
parameterPort, err := cmd.Flags().GetString("port")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if parameterPort != "" {
|
if parameterPort != "" {
|
||||||
port = parameterPort
|
listenPort = parameterPort
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,14 +4,15 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
port string = "1302"
|
listenPort string = "1302"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Updater interface {
|
type Creator interface {
|
||||||
UpdateUser()
|
CreateUser()
|
||||||
}
|
}
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
|
@ -19,20 +20,29 @@ type User struct {
|
||||||
IP string
|
IP string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u User) UpdateUser(usr string, ip string) User {
|
type Message struct {
|
||||||
newU := new(User)
|
Contents string
|
||||||
newU.Username = usr
|
Date time.Time
|
||||||
newU.IP = ip
|
}
|
||||||
return *newU
|
|
||||||
|
func (u User) CreateUser(usr string, ip string) User {
|
||||||
|
u.Username = usr
|
||||||
|
u.IP = ip
|
||||||
|
return u
|
||||||
}
|
}
|
||||||
|
|
||||||
func Server() {
|
func Server() {
|
||||||
ln, err := net.Listen("tcp", ":"+port)
|
ln, err := net.Listen("tcp", ":"+listenPort)
|
||||||
checkErr(err)
|
if err != nil {
|
||||||
fmt.Printf("Listening on port %v...\n", port)
|
log.Fatalf("Error happened trying to listen on port: %v\n", err)
|
||||||
|
}
|
||||||
|
defer ln.Close()
|
||||||
|
fmt.Printf("Listening on port %v...\n", listenPort)
|
||||||
for {
|
for {
|
||||||
conn, err := ln.Accept()
|
conn, err := ln.Accept()
|
||||||
checkErr(err)
|
if err != nil {
|
||||||
|
log.Fatalf("Error happened trying to accept connection: %v\n", err)
|
||||||
|
}
|
||||||
go handleConn(conn)
|
go handleConn(conn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,12 +52,17 @@ func getUserInput() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleConn(conn net.Conn) {
|
func handleConn(conn net.Conn) {
|
||||||
fmt.Println("Received connection")
|
defer conn.Close()
|
||||||
|
newUserTemplate := new(User)
|
||||||
|
newUser := newUserTemplate.CreateUser("Tom", getIP(conn))
|
||||||
|
fmt.Printf("Received connection from %v at %v\n", newUser.Username, newUser.IP)
|
||||||
fmt.Fprintln(conn, "Hello buddy!")
|
fmt.Fprintln(conn, "Hello buddy!")
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkErr(err error) {
|
func getIP(conn net.Conn) (IP string) {
|
||||||
if err != nil {
|
if addr, ok := conn.RemoteAddr().(*net.TCPAddr); ok {
|
||||||
log.Fatalf("Error: %v\n", err)
|
IP = fmt.Sprintf("%v", addr.IP)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return IP
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue