Refactoring main into a server method
This commit is contained in:
parent
9b8939d06d
commit
62d871b44c
2 changed files with 35 additions and 33 deletions
|
@ -6,6 +6,8 @@ import (
|
|||
"io"
|
||||
"log"
|
||||
"net"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// DataBufferSize the maximum size of the data buffer.
|
||||
|
@ -83,3 +85,35 @@ func HandleConnection(client Client) {
|
|||
// Client has left.
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,41 +1,9 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
|
||||
"github.com/metonimie/simpleFTP/server/server"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func main() {
|
||||
server.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("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)
|
||||
}
|
||||
server.StartFtpServer()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue