Refactoring main into a server method

This commit is contained in:
Denis-Cosmin Nutiu 2017-11-26 21:57:57 +02:00
parent 9b8939d06d
commit 62d871b44c
2 changed files with 35 additions and 33 deletions

View file

@ -6,6 +6,8 @@ import (
"io" "io"
"log" "log"
"net" "net"
"github.com/spf13/viper"
) )
// DataBufferSize the maximum size of the data buffer. // DataBufferSize the maximum size of the data buffer.
@ -83,3 +85,35 @@ func HandleConnection(client Client) {
// Client has left. // Client has left.
log.Println(client.Connection().RemoteAddr(), "has disconnected.") log.Println(client.Connection().RemoteAddr(), "has disconnected.")
} }
func StartFtpServer() {
InitializedConfiguration()
Addr := viper.GetString("address")
Port := viper.GetString("port")
DirDepth := viper.GetInt("maxDirDepth")
// Start the server
listener, err := net.Listen("tcp", Addr+":"+Port)
if err != nil {
log.Fatal(err)
}
log.Println("Hello world!")
log.Println("Ftp server running on:", Addr, "port", Port)
for {
conn, err := listener.Accept()
if err != nil {
log.Print(err)
continue
}
client := FTPClient{}
client.SetStack(MakeStringStack(DirDepth))
client.SetConnection(conn)
go HandleConnection(&client)
}
}

View file

@ -1,41 +1,9 @@
package main package main
import ( import (
"log"
"net"
"github.com/metonimie/simpleFTP/server/server" "github.com/metonimie/simpleFTP/server/server"
"github.com/spf13/viper"
) )
func main() { func main() {
server.InitializedConfiguration() server.StartFtpServer()
Addr := viper.GetString("address")
Port := viper.GetString("port")
DirDepth := viper.GetInt("maxDirDepth")
// Start the server
listener, err := net.Listen("tcp", Addr+":"+Port)
if err != nil {
log.Fatal(err)
}
log.Println("Hello world!")
log.Println("Running on:", Addr, "port", Port)
for {
conn, err := listener.Accept()
if err != nil {
log.Print(err)
continue
}
client := server.FTPClient{}
client.SetStack(server.MakeStringStack(DirDepth))
client.SetConnection(conn)
go server.HandleConnection(&client)
}
} }