dispatch/commands/dispatch.go

128 lines
3.3 KiB
Go
Raw Normal View History

package commands
import (
2016-01-04 18:26:32 +00:00
"fmt"
"io/ioutil"
"log"
"os"
2018-04-14 22:57:11 +00:00
"time"
2018-04-14 22:57:11 +00:00
"github.com/fsnotify/fsnotify"
2016-03-01 00:51:26 +00:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
2015-12-11 03:35:48 +00:00
"github.com/khlieng/dispatch/assets"
"github.com/khlieng/dispatch/server"
"github.com/khlieng/dispatch/storage"
"github.com/khlieng/dispatch/storage/bleve"
"github.com/khlieng/dispatch/storage/boltdb"
)
2016-01-04 18:26:32 +00:00
const logo = `
____ _ _ _
| _ \ (_) ___ _ __ __ _ | |_ ___ | |__
| | | || |/ __|| '_ \ / _ || __|/ __|| '_ \
| |_| || |\__ \| |_) || (_| || |_| (__ | | | |
|____/ |_||___/| .__/ \__,_| \__|\___||_| |_|
|_|
2017-06-29 05:57:53 +00:00
v0.4
2016-01-04 18:26:32 +00:00
`
var rootCmd = &cobra.Command{
Use: "dispatch",
Short: "Web-based IRC client in Go.",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if cmd.Use == "dispatch" {
fmt.Println(logo)
}
2016-01-07 22:59:38 +00:00
storage.Initialize(viper.GetString("dir"))
2018-04-14 23:25:21 +00:00
initConfig(storage.Path.Config(), viper.GetBool("reset_config"))
2016-01-04 18:26:32 +00:00
viper.SetConfigName("config")
viper.AddConfigPath(storage.Path.Root())
viper.ReadInConfig()
2018-04-14 22:57:11 +00:00
viper.WatchConfig()
prev := time.Now()
viper.OnConfigChange(func(e fsnotify.Event) {
now := time.Now()
// fsnotify sometimes fires twice
if now.Sub(prev) > time.Second {
log.Println("New config loaded")
prev = now
}
})
2016-01-04 18:26:32 +00:00
},
2016-01-07 22:59:38 +00:00
Run: func(cmd *cobra.Command, args []string) {
if viper.GetBool("dev") {
log.Println("Running in development mode, access client at http://localhost:3000")
}
2016-01-07 22:59:38 +00:00
log.Println("Storing data at", storage.Path.Root())
db, err := boltdb.New(storage.Path.Database())
if err != nil {
log.Fatal(err)
}
defer db.Close()
srv := server.Dispatch{
Store: db,
SessionStore: db,
GetMessageStore: func(user *storage.User) (storage.MessageStore, error) {
return boltdb.New(storage.Path.Log(user.Username))
},
GetMessageSearchProvider: func(user *storage.User) (storage.MessageSearchProvider, error) {
return bleve.New(storage.Path.Index(user.Username))
},
}
srv.Run()
2016-01-07 22:59:38 +00:00
},
2016-01-04 18:26:32 +00:00
}
2016-01-04 18:26:32 +00:00
func Execute() {
rootCmd.Execute()
}
func init() {
rootCmd.AddCommand(clearCmd)
rootCmd.AddCommand(configCmd)
2016-01-04 18:26:32 +00:00
rootCmd.PersistentFlags().String("dir", storage.DefaultDirectory(), "directory to store config and data in")
2018-04-14 23:25:21 +00:00
rootCmd.PersistentFlags().Bool("reset-config", false, "reset to the default configuration, overwriting the current one")
rootCmd.Flags().StringP("address", "a", "", "interface to which the server will bind")
rootCmd.Flags().IntP("port", "p", 80, "port to listen on")
rootCmd.Flags().Bool("dev", false, "development mode")
viper.BindPFlag("dir", rootCmd.PersistentFlags().Lookup("dir"))
2018-04-14 23:25:21 +00:00
viper.BindPFlag("reset_config", rootCmd.PersistentFlags().Lookup("reset-config"))
viper.BindPFlag("address", rootCmd.Flags().Lookup("address"))
viper.BindPFlag("port", rootCmd.Flags().Lookup("port"))
viper.BindPFlag("dev", rootCmd.Flags().Lookup("dev"))
2016-01-14 05:35:26 +00:00
viper.SetDefault("hexIP", false)
2016-01-14 05:35:26 +00:00
viper.SetDefault("verify_client_certificates", true)
}
2018-04-14 23:25:21 +00:00
func initConfig(configPath string, overwrite bool) {
if _, err := os.Stat(configPath); overwrite || os.IsNotExist(err) {
config, err := assets.Asset("config.default.toml")
if err != nil {
log.Println(err)
return
}
2016-01-04 18:26:32 +00:00
log.Println("Writing default config to", configPath)
err = ioutil.WriteFile(configPath, config, 0600)
if err != nil {
log.Println(err)
}
}
}