app. The structure should look like that:
appfolder and execute the following command:
ReadEnvwill read from the
Envtype of a system's environment variable (see
main.goin the end of the page).
environment/environment.gofile and put the following contents:
SQLite. The first one for "production" use and the other for "development".
database/database.gofile and copy-paste the following:
DBinstances. Create a
database/mysql.gofile which looks like the following one:
DBdepends on the `Environment.
GreetServiceinterface contains a single method of
Say(input string) (output string, err error). Create a
./service/greet_service.gofile and write the following code:
greeterwill be used on "production" and the
greeterWithLoggingon "development". The
GreetServicedepends on the
model/request.gofile and copy-paste the following code:
/greet?name=kataras) and will reply back with a JSON message.
GreetControllerfor our mini web-application. Create a file at
controller/greet_controller.gowhich looks like that:
GreetControllerdepends on the
GreetService. It serves the
GET: /greetindex path through its
Getmethod accepts a
model.Requestwhich contains a single field name of
Namewhich will be extracted from the
URL Query Parameter 'name'(because it's a
GETrequst and its
url:"name"struct field). And it will respond with a
model.Response(JSON) or an error.
main.gofile. Copy-paste the following code:
mvc.Application.Registermethod registers one more dependencies, dependencies can depend on previously registered dependencies too. Thats the reason we pass, first, the
Environment(DEV), then the
NewDBwhich depends on that
Environment, following by the
NewGreetServicefunction which depends on both the
mvc.Application.Handleregisters a new controller, which depends on the
GreetService, for the targeted sub-router of
environment.PROD, restart the application and refresh. You will see that a new database (
sqlite) and another service of (
greeterWithLogging) will be binded to the
GreetController. With a single change you achieve to completety change the result.