-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from fabiante/feat/refactor-cli
- Loading branch information
Showing
6 changed files
with
75 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,4 @@ EXPOSE 8060 | |
ENV GIN_MODE=release | ||
|
||
ENTRYPOINT ["/persurl"] | ||
CMD ["run"] |
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,5 @@ | ||
package cmds | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
var Root = &cobra.Command{} |
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,55 @@ | ||
package cmds | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/fabiante/persurl/api" | ||
"github.com/fabiante/persurl/db" | ||
"github.com/gin-gonic/gin" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
cmd := &cobra.Command{ | ||
Use: "run", | ||
Short: "Run the application", | ||
} | ||
|
||
cmd.Run = func(cmd *cobra.Command, args []string) { | ||
dataDir := envDataDir() | ||
|
||
dbFile := envDbFile(dataDir) | ||
|
||
_, database, err := db.SetupAndMigrateDB(dbFile) | ||
if err != nil { | ||
log.Fatalf("setting up database failed: %s", err) | ||
} | ||
|
||
engine := gin.Default() | ||
service := db.NewDatabase(database) | ||
server := api.NewServer(service) | ||
api.SetupRouting(engine, server) | ||
if err := engine.Run(":8060"); err != nil { | ||
log.Fatalf("running api failed: %s", err) | ||
} | ||
} | ||
|
||
Root.AddCommand(cmd) | ||
} | ||
|
||
func envDataDir() string { | ||
dataDir := os.Getenv("PERSURL_DATA_DIR") | ||
if dataDir == "" { | ||
dataDir = "." | ||
} | ||
log.Printf("using data dir: %s", dataDir) | ||
return dataDir | ||
} | ||
|
||
func envDbFile(dataDir string) string { | ||
dbFile := fmt.Sprintf("%s/prod.sqlite", dataDir) | ||
log.Printf("using database file: %s", dbFile) | ||
return dbFile | ||
} |
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,47 +1,13 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/fabiante/persurl/api" | ||
"github.com/fabiante/persurl/db" | ||
"github.com/gin-gonic/gin" | ||
"github.com/fabiante/persurl/cli/cmds" | ||
) | ||
|
||
// main is a crude CLI entrypoint for the application. Will be replaced | ||
// with a proper CLI supporting multiple commands later. | ||
func main() { | ||
dataDir := envDataDir() | ||
|
||
dbFile := envDbFile(dataDir) | ||
|
||
_, database, err := db.SetupAndMigrateDB(dbFile) | ||
if err != nil { | ||
log.Fatalf("setting up database failed: %s", err) | ||
if err := cmds.Root.Execute(); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
engine := gin.Default() | ||
service := db.NewDatabase(database) | ||
server := api.NewServer(service) | ||
api.SetupRouting(engine, server) | ||
if err := engine.Run(":8060"); err != nil { | ||
log.Fatalf("running api failed: %s", err) | ||
} | ||
} | ||
|
||
func envDataDir() string { | ||
dataDir := os.Getenv("PERSURL_DATA_DIR") | ||
if dataDir == "" { | ||
dataDir = "." | ||
} | ||
log.Printf("using data dir: %s", dataDir) | ||
return dataDir | ||
} | ||
|
||
func envDbFile(dataDir string) string { | ||
dbFile := fmt.Sprintf("%s/prod.sqlite", dataDir) | ||
log.Printf("using database file: %s", dbFile) | ||
return dbFile | ||
} |
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