2020-10-23 03:54:50 +13:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2021-03-17 03:24:58 +13:00
|
|
|
"strings"
|
2020-10-23 03:54:50 +13:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/diode"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2020-10-29 07:15:48 +13:00
|
|
|
"demodesk/neko"
|
2022-01-14 12:22:23 +13:00
|
|
|
"demodesk/neko/internal/config"
|
|
|
|
"demodesk/neko/modules"
|
2020-10-23 03:54:50 +13:00
|
|
|
)
|
|
|
|
|
|
|
|
func Execute() error {
|
|
|
|
return root.Execute()
|
|
|
|
}
|
|
|
|
|
|
|
|
var root = &cobra.Command{
|
|
|
|
Use: "neko",
|
|
|
|
Short: "neko streaming server",
|
|
|
|
Long: `neko streaming server`,
|
|
|
|
Version: neko.Service.Version.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2022-01-14 12:22:23 +13:00
|
|
|
configs := append([]config.Config{
|
|
|
|
neko.Service.Configs.Root,
|
|
|
|
neko.Service.Configs.Desktop,
|
|
|
|
neko.Service.Configs.Capture,
|
|
|
|
neko.Service.Configs.WebRTC,
|
|
|
|
neko.Service.Configs.Member,
|
|
|
|
neko.Service.Configs.Session,
|
|
|
|
neko.Service.Configs.Server,
|
|
|
|
}, modules.Configs()...)
|
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
cobra.OnInitialize(func() {
|
|
|
|
//////
|
|
|
|
// logs
|
|
|
|
//////
|
|
|
|
console := zerolog.ConsoleWriter{Out: os.Stdout}
|
2021-09-27 08:29:56 +13:00
|
|
|
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
|
|
|
|
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2021-09-27 08:29:56 +13:00
|
|
|
logs := viper.GetBool("logs")
|
|
|
|
if !logs {
|
2020-10-23 03:54:50 +13:00
|
|
|
log.Logger = log.Output(console)
|
|
|
|
} else {
|
2021-09-27 08:29:56 +13:00
|
|
|
logsPath := filepath.Join(".", "logs")
|
2020-10-23 03:54:50 +13:00
|
|
|
if runtime.GOOS == "linux" {
|
2021-09-27 08:29:56 +13:00
|
|
|
logsPath = "/var/log/neko"
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-09-27 08:29:56 +13:00
|
|
|
if _, err := os.Stat(logsPath); os.IsNotExist(err) {
|
|
|
|
_ = os.Mkdir(logsPath, os.ModePerm)
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-09-27 08:29:56 +13:00
|
|
|
latest := filepath.Join(logsPath, "neko-latest.log")
|
|
|
|
if _, err := os.Stat(latest); err == nil {
|
|
|
|
err = os.Rename(latest, filepath.Join(logsPath, "neko."+time.Now().Format("2006-01-02T15-04-05Z07-00")+".log"))
|
2020-10-23 03:54:50 +13:00
|
|
|
if err != nil {
|
|
|
|
log.Panic().Err(err).Msg("failed to rotate log file")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
logf, err := os.OpenFile(latest, os.O_RDWR|os.O_CREATE, 0666)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic().Err(err).Msg("failed to create log file")
|
|
|
|
}
|
|
|
|
|
|
|
|
logger := diode.NewWriter(logf, 1000, 10*time.Millisecond, func(missed int) {
|
|
|
|
fmt.Printf("logger dropped %d messages", missed)
|
|
|
|
})
|
|
|
|
|
|
|
|
log.Logger = log.Output(io.MultiWriter(console, logger))
|
|
|
|
}
|
|
|
|
|
|
|
|
//////
|
|
|
|
// configs
|
|
|
|
//////
|
2021-03-19 07:37:37 +13:00
|
|
|
config := viper.GetString("config") // Use config file from the flag.
|
|
|
|
if config == "" {
|
|
|
|
config = os.Getenv("NEKO_CONFIG") // Use config file from the environment variable.
|
|
|
|
}
|
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
if config != "" {
|
2021-03-19 07:37:37 +13:00
|
|
|
viper.SetConfigFile(config)
|
2020-10-23 03:54:50 +13:00
|
|
|
} else {
|
|
|
|
if runtime.GOOS == "linux" {
|
|
|
|
viper.AddConfigPath("/etc/neko/")
|
|
|
|
}
|
|
|
|
|
|
|
|
viper.AddConfigPath(".")
|
|
|
|
viper.SetConfigName("neko")
|
|
|
|
}
|
|
|
|
|
|
|
|
viper.SetEnvPrefix("NEKO")
|
2021-03-17 03:24:58 +13:00
|
|
|
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
2020-10-23 03:54:50 +13:00
|
|
|
viper.AutomaticEnv() // read in environment variables that match
|
|
|
|
|
2021-09-27 08:29:56 +13:00
|
|
|
err := viper.ReadInConfig()
|
|
|
|
if err != nil && config != "" {
|
|
|
|
log.Err(err)
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2021-09-27 08:29:56 +13:00
|
|
|
// get full config file path
|
|
|
|
config = viper.ConfigFileUsed()
|
|
|
|
|
2021-03-19 07:37:37 +13:00
|
|
|
//////
|
|
|
|
// debug
|
|
|
|
//////
|
|
|
|
debug := viper.GetBool("debug")
|
|
|
|
if debug {
|
2021-01-18 07:10:59 +13:00
|
|
|
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
|
|
|
}
|
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
logger := log.With().
|
2021-03-19 07:37:37 +13:00
|
|
|
Bool("debug", debug).
|
2021-09-27 08:29:56 +13:00
|
|
|
Bool("logs", logs).
|
|
|
|
Str("config", config).
|
2020-10-23 03:54:50 +13:00
|
|
|
Logger()
|
|
|
|
|
2021-09-27 08:29:56 +13:00
|
|
|
if config == "" {
|
2020-10-23 03:54:50 +13:00
|
|
|
logger.Warn().Msg("preflight complete without config file")
|
|
|
|
} else {
|
2021-09-27 08:29:56 +13:00
|
|
|
if _, err := os.Stat(config); os.IsNotExist(err) {
|
2021-09-02 09:10:06 +12:00
|
|
|
logger.Err(err).Msg("preflight complete with nonexistent config file")
|
2021-03-19 07:47:28 +13:00
|
|
|
} else {
|
2021-09-27 08:29:56 +13:00
|
|
|
logger.Info().Msg("preflight complete with config file")
|
2021-03-19 07:47:28 +13:00
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2022-01-14 12:22:23 +13:00
|
|
|
for _, cfg := range configs {
|
|
|
|
cfg.Set()
|
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
})
|
|
|
|
|
2022-01-14 12:22:23 +13:00
|
|
|
for _, cfg := range configs {
|
|
|
|
if err := cfg.Init(root); err != nil {
|
|
|
|
log.Panic().Err(err).Msg("unable to initialize configuration")
|
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
root.SetVersionTemplate(neko.Service.Version.Details())
|
|
|
|
}
|