summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorGrail Finder <wohilas@gmail.com>2025-03-29 11:12:53 +0300
committerGrail Finder <wohilas@gmail.com>2025-03-29 11:12:53 +0300
commit3921db6166e2da895257496bb76dd115556699d3 (patch)
tree1be4f739121761085f69cb7706c60dbbe98a93e9 /cmd
init
Diffstat (limited to 'cmd')
-rw-r--r--cmd/root.go46
-rw-r--r--cmd/start.go47
2 files changed, 93 insertions, 0 deletions
diff --git a/cmd/root.go b/cmd/root.go
new file mode 100644
index 0000000..1048bf2
--- /dev/null
+++ b/cmd/root.go
@@ -0,0 +1,46 @@
+package cmd
+
+import (
+ "log"
+
+ "github.com/spf13/cobra"
+ "github.com/spf13/viper"
+)
+
+// LogLevel Flag
+var LogLevel = "info"
+var LogFormat = "json"
+var cfgFile string
+var rootCmd = &cobra.Command{
+ Use: "demoon",
+ Short: "demoon",
+}
+
+func init() {
+ cobra.OnInitialize(initConfig)
+ rootCmd.PersistentFlags().StringVar(&cfgFile,
+ "config", "", "config file (default is ./config.yml)")
+ viper.SetConfigName("config")
+ viper.AddConfigPath(".") // First try to load the config from the current directory
+ viper.AddConfigPath("$HOME") // Then try to load it from the HOME directory
+ viper.AddConfigPath("/etc/demoon/") // As a last resort try to load it from the /etc/
+}
+
+func initConfig() {
+ if cfgFile != "" {
+ // Use config file from the flag.
+ viper.SetConfigFile(cfgFile)
+ }
+ viper.SetEnvPrefix("CFG")
+ viper.AutomaticEnv()
+ if err := viper.ReadInConfig(); err != nil {
+ log.Fatal("Can't read configuration file", "error", err)
+ }
+}
+
+// Execute the commands
+func Execute() {
+ if err := rootCmd.Execute(); err != nil {
+ log.Fatal("failed to execute", "error", err)
+ }
+}
diff --git a/cmd/start.go b/cmd/start.go
new file mode 100644
index 0000000..af46d79
--- /dev/null
+++ b/cmd/start.go
@@ -0,0 +1,47 @@
+package cmd
+
+import (
+ "demoon/config"
+ "demoon/internal/crons"
+ "demoon/internal/database/repos"
+ database "demoon/internal/database/sql"
+ "demoon/internal/server"
+ "context"
+ "os"
+
+ "log/slog"
+
+ "github.com/spf13/cobra"
+ "github.com/spf13/viper"
+)
+
+func init() {
+ rootCmd.AddCommand(startCmd)
+}
+
+var startCmd = &cobra.Command{
+ Use: "start",
+ Short: "Start data server",
+ Long: `Start data server`,
+ Run: func(cmd *cobra.Command, args []string) {
+ log := slog.New(slog.NewJSONHandler(os.Stdout,
+ &slog.HandlerOptions{Level: slog.LevelDebug, AddSource: true}))
+ // load server configuration from server
+ if viper.ConfigFileUsed() != "" {
+ log.Debug("Configuration file loaded", "section", "init",
+ "path", viper.ConfigFileUsed())
+ }
+ cfg := config.LoadConfig(viper.GetViper())
+ db, err := database.Init(cfg.DBURI)
+ if err != nil {
+ log.Error("failed to connect to db", "error", err)
+ return
+ }
+ repo := repos.NewProvider(db.Conn)
+ srv := server.NewServer(cfg, log, repo)
+ cron := crons.NewCron(context.Background(), repo)
+ cron.StartCronJobs()
+ // listen for new messages
+ srv.Listen()
+ },
+}