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