@@ -5,22 +5,60 @@ import (
55 "fmt"
66 "log"
77 "net/http"
8+ "os"
9+ "strconv"
810
9- sample "github.com/friendsofgo/gopherapi/cmd/sample-data"
10- "github.com/friendsofgo/gopherapi/internal/container"
11+ "github.com/friendsofgo/gopherapi/cmd/sample-data"
1112 gopher "github.com/friendsofgo/gopherapi/pkg"
13+ "github.com/friendsofgo/gopherapi/pkg/adding"
14+ "github.com/friendsofgo/gopherapi/pkg/fetching"
15+ "github.com/friendsofgo/gopherapi/pkg/log/logrus"
16+ "github.com/friendsofgo/gopherapi/pkg/modifying"
17+ "github.com/friendsofgo/gopherapi/pkg/removing"
18+ "github.com/friendsofgo/gopherapi/pkg/server"
19+ "github.com/friendsofgo/gopherapi/pkg/storage/inmem"
20+
21+ _ "github.com/joho/godotenv/autoload"
1222)
1323
1424func main () {
25+
26+ var (
27+ hostName , _ = os .Hostname ()
28+ defaultServerID = fmt .Sprintf ("%s-%s" , os .Getenv ("GOPHERAPI_NAME" ), hostName )
29+ defaultHost = os .Getenv ("GOPHERAPI_SERVER_HOST" )
30+ defaultPort , _ = strconv .Atoi (os .Getenv ("GOPHERAPI_SERVER_PORT" ))
31+ )
32+
33+ host := flag .String ("host" , defaultHost , "define host of the server" )
34+ port := flag .Int ("port" , defaultPort , "define port of the server" )
35+ serverID := flag .String ("server-id" , defaultServerID , "define server identifier" )
1536 withData := flag .Bool ("withData" , false , "initialize the api with some gophers" )
1637 flag .Parse ()
1738
1839 var gophers map [string ]gopher.Gopher
1940 if * withData {
2041 gophers = sample .Gophers
2142 }
22- s := container .InitializeServer (gophers )
2343
24- fmt .Println ("The gopher server is on tap now: http://localhost:8080" )
25- log .Fatal (http .ListenAndServe (":8080" , s .Router ()))
44+ logger := logrus .NewLogger ()
45+
46+ repo := inmem .NewRepository (gophers )
47+ fetchingService := fetching .NewService (repo , logger )
48+ addingService := adding .NewService (repo )
49+ modifyingService := modifying .NewService (repo )
50+ removingService := removing .NewService (repo )
51+
52+ httpAddr := fmt .Sprintf ("%s:%d" , * host , * port )
53+
54+ s := server .New (
55+ * serverID ,
56+ fetchingService ,
57+ addingService ,
58+ modifyingService ,
59+ removingService ,
60+ )
61+
62+ fmt .Println ("The gopher server is on tap now:" , httpAddr )
63+ log .Fatal (http .ListenAndServe (httpAddr , s .Router ()))
2664}
0 commit comments