@@ -5,22 +5,58 @@ 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/modifying"
16+ "github.com/friendsofgo/gopherapi/pkg/removing"
17+ "github.com/friendsofgo/gopherapi/pkg/server"
18+ "github.com/friendsofgo/gopherapi/pkg/storage/inmem"
19+
20+ _ "github.com/joho/godotenv/autoload"
1221)
1322
1423func main () {
24+
25+ var (
26+ hostName , _ = os .Hostname ()
27+ defaultServerName = fmt .Sprintf ("%s-%s" , os .Getenv ("GOPHERAPI_NAME" ), hostName )
28+ defaultHost = os .Getenv ("GOPHERAPI_SERVER_HOST" )
29+ defaultPort , _ = strconv .Atoi (os .Getenv ("GOPHERAPI_SERVER_PORT" ))
30+ )
31+
32+ host := flag .String ("host" , defaultHost , "define host of the server" )
33+ port := flag .Int ("port" , defaultPort , "define port of the server" )
34+ serverName := flag .String ("server-name" , defaultServerName , "define name of the server" )
1535 withData := flag .Bool ("withData" , false , "initialize the api with some gophers" )
1636 flag .Parse ()
1737
1838 var gophers map [string ]gopher.Gopher
1939 if * withData {
2040 gophers = sample .Gophers
2141 }
22- s := container .InitializeServer (gophers )
2342
24- fmt .Println ("The gopher server is on tap now: http://localhost:8080" )
25- log .Fatal (http .ListenAndServe (":8080" , s .Router ()))
43+ repo := inmem .NewRepository (gophers )
44+ fetchingService := fetching .NewService (repo )
45+ addingService := adding .NewService (repo )
46+ modifyingService := modifying .NewService (repo )
47+ removingService := removing .NewService (repo )
48+
49+ httpAddr := fmt .Sprintf ("%s:%d" , * host , * port )
50+
51+ s := server .New (
52+ * serverName ,
53+ httpAddr ,
54+ fetchingService ,
55+ addingService ,
56+ modifyingService ,
57+ removingService ,
58+ )
59+
60+ fmt .Println ("The gopher server is on tap now:" , httpAddr )
61+ log .Fatal (http .ListenAndServe (httpAddr , s .Router ()))
2662}
0 commit comments