Files
dagu/cmd/server.go
T
2024-08-28 19:31:36 +09:00

70 lines
2.2 KiB
Go

// Copyright (C) 2024 The Dagu Authors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package cmd
import (
"log"
"github.com/dagu-org/dagu/internal/config"
"github.com/dagu-org/dagu/internal/frontend"
"github.com/dagu-org/dagu/internal/logger"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func serverCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "server",
Short: "Start the server",
Long: `dagu server [--dags=<DAGs dir>] [--host=<host>] [--port=<port>]`,
PreRun: func(cmd *cobra.Command, _ []string) {
_ = viper.BindPFlag("port", cmd.Flags().Lookup("port"))
_ = viper.BindPFlag("host", cmd.Flags().Lookup("host"))
_ = viper.BindPFlag("dags", cmd.Flags().Lookup("dags"))
},
Run: func(cmd *cobra.Command, _ []string) {
cfg, err := config.Load()
if err != nil {
log.Fatalf("Configuration load failed: %v", err)
}
logger := logger.NewLogger(logger.NewLoggerArgs{
Debug: cfg.Debug,
Format: cfg.LogFormat,
})
logger.Info("Server initialization", "host", cfg.Host, "port", cfg.Port)
dataStore := newDataStores(cfg)
cli := newClient(cfg, dataStore, logger)
server := frontend.New(cfg, logger, cli)
if err := server.Serve(cmd.Context()); err != nil {
logger.Fatal("Server initialization failed", "error", err)
}
},
}
bindServerCommandFlags(cmd)
return cmd
}
func bindServerCommandFlags(cmd *cobra.Command) {
cmd.Flags().StringP(
"dags", "d", "", "location of DAG files (default is $HOME/.config/dagu/dags)",
)
cmd.Flags().StringP("host", "s", "", "server host (default is localhost)")
cmd.Flags().StringP("port", "p", "", "server port (default is 8080)")
}