|
@@ -1,12 +1,9 @@
|
|
package main
|
|
package main
|
|
|
|
|
|
import (
|
|
import (
|
|
- "database/sql"
|
|
|
|
- "fmt"
|
|
|
|
|
|
+ "log"
|
|
|
|
|
|
"code.osinet.fr/fgm/kurz/api"
|
|
"code.osinet.fr/fgm/kurz/api"
|
|
- "code.osinet.fr/fgm/kurz/domain"
|
|
|
|
- "code.osinet.fr/fgm/kurz/infrastructure"
|
|
|
|
_ "code.osinet.fr/fgm/kurz/migrations"
|
|
_ "code.osinet.fr/fgm/kurz/migrations"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
"github.com/spf13/viper"
|
|
@@ -24,27 +21,12 @@ func init() {
|
|
cmdServe.AddCommand(cmdServeAPI)
|
|
cmdServe.AddCommand(cmdServeAPI)
|
|
}
|
|
}
|
|
|
|
|
|
-func registerInfrastructure() *sql.DB {
|
|
|
|
- dbDriver, dbDsn := infrastructure.ParseDbCred()
|
|
|
|
- db, err := infrastructure.DbDial(dbDriver, dbDsn)
|
|
|
|
- if err != nil {
|
|
|
|
- panic(err)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- domain.RegisterRepositories(
|
|
|
|
- infrastructure.MySQLShortURLRepository{DB: db},
|
|
|
|
- infrastructure.MySQLTargetURLRepository{DB: db},
|
|
|
|
- )
|
|
|
|
-
|
|
|
|
- return db
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
// Set up infrastructure and listen on specified address.
|
|
// Set up infrastructure and listen on specified address.
|
|
func serveAPIHandler(_ *cobra.Command, args []string) {
|
|
func serveAPIHandler(_ *cobra.Command, args []string) {
|
|
- db := registerInfrastructure()
|
|
|
|
|
|
+ db = ensureInfrastructure(db)
|
|
defer db.Close()
|
|
defer db.Close()
|
|
|
|
|
|
address := viper.Get("api.address").(string)
|
|
address := viper.Get("api.address").(string)
|
|
err := api.ListenAndServe(address)
|
|
err := api.ListenAndServe(address)
|
|
- fmt.Println(err)
|
|
|
|
|
|
+ log.Fatal(err)
|
|
}
|
|
}
|