-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5609e5c
commit dc05356
Showing
7 changed files
with
113 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,30 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/labasubagia/realworld-backend/internal/core/util" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type Command struct { | ||
config util.Config | ||
rootCmd *cobra.Command | ||
} | ||
var config util.Config | ||
|
||
func (c *Command) AddCommand(fnCommands ...func(config util.Config) *cobra.Command) { | ||
commands := []*cobra.Command{} | ||
for _, fn := range fnCommands { | ||
commands = append(commands, fn(c.config)) | ||
} | ||
c.rootCmd.AddCommand(commands...) | ||
var rootCmd = &cobra.Command{ | ||
Use: "realworld", | ||
Short: "Realworld backend app", | ||
Long: "Realworld is an app about article similar medium.com and dev.to", | ||
Run: func(cmd *cobra.Command, args []string) {}, | ||
} | ||
|
||
func (c *Command) Execute() error { | ||
return c.rootCmd.Execute() | ||
} | ||
func init() { | ||
var err error | ||
|
||
func NewCommand(config util.Config) *Command { | ||
command := &Command{ | ||
config: config, | ||
rootCmd: &cobra.Command{ | ||
Use: "app", | ||
Short: "App about article", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Println("see help") | ||
}, | ||
}, | ||
config, err = util.LoadConfig(".env") | ||
if err != nil { | ||
log.Fatal("failed to load env config", err) | ||
} | ||
command.AddCommand(startCmd) | ||
return command | ||
} | ||
|
||
func Execute() error { | ||
return rootCmd.Execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/labasubagia/realworld-backend/internal/adapter/handler/restful" | ||
"github.com/labasubagia/realworld-backend/internal/adapter/repository" | ||
"github.com/labasubagia/realworld-backend/internal/core/service" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
dbOpts := []string{} | ||
for option := range repository.RepoFnMap { | ||
dbOpts = append(dbOpts, option) | ||
} | ||
dbOptStr := strings.Join(dbOpts, ",") | ||
|
||
rootCmd.AddCommand(serverCmd) | ||
serverCmd.Flags().StringP("database", "d", repository.DefaultRepoKey, fmt.Sprintf("select database in (%s)", dbOptStr)) | ||
} | ||
|
||
var serverCmd = &cobra.Command{ | ||
Use: "server", | ||
Short: "run http server", | ||
Long: "Run gin server restful API", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
dbType := cmd.Flag("database").Value.String() | ||
dbType = strings.ToLower(dbType) | ||
newRepo, exist := repository.RepoFnMap[dbType] | ||
if !exist { | ||
log.Fatal("invalid database", dbType) | ||
} | ||
|
||
repo, err := newRepo(config) | ||
if err != nil { | ||
log.Fatal("failed to load repository", err) | ||
} | ||
service, err := service.NewService(config, repo) | ||
if err != nil { | ||
log.Fatal("failed to load service", err) | ||
} | ||
server := restful.NewServer(config, service) | ||
|
||
if server.Start(); err != nil { | ||
log.Fatal("failed to load server", err) | ||
} | ||
}, | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters