2015-05-01 20:59:46 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2016-01-04 18:26:32 +00:00
|
|
|
"fmt"
|
2015-05-28 23:59:08 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2018-12-15 10:30:29 +00:00
|
|
|
"runtime"
|
2015-05-28 23:59:08 +00:00
|
|
|
|
2015-12-11 03:35:48 +00:00
|
|
|
"github.com/khlieng/dispatch/assets"
|
2018-12-11 09:51:20 +00:00
|
|
|
"github.com/khlieng/dispatch/config"
|
2015-12-11 03:35:48 +00:00
|
|
|
"github.com/khlieng/dispatch/server"
|
|
|
|
"github.com/khlieng/dispatch/storage"
|
2018-05-31 21:24:59 +00:00
|
|
|
"github.com/khlieng/dispatch/storage/bleve"
|
|
|
|
"github.com/khlieng/dispatch/storage/boltdb"
|
2018-11-22 10:31:02 +00:00
|
|
|
"github.com/khlieng/dispatch/version"
|
2018-11-11 06:08:53 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
2015-05-01 20:59:46 +00:00
|
|
|
)
|
|
|
|
|
2016-01-04 18:26:32 +00:00
|
|
|
const logo = `
|
|
|
|
____ _ _ _
|
|
|
|
| _ \ (_) ___ _ __ __ _ | |_ ___ | |__
|
|
|
|
| | | || |/ __|| '_ \ / _ || __|/ __|| '_ \
|
|
|
|
| |_| || |\__ \| |_) || (_| || |_| (__ | | | |
|
|
|
|
|____/ |_||___/| .__/ \__,_| \__|\___||_| |_|
|
|
|
|
|_|
|
2018-11-16 04:17:20 +00:00
|
|
|
|
|
|
|
%s
|
|
|
|
Commit: %s
|
|
|
|
Build Date: %s
|
2018-12-15 10:30:29 +00:00
|
|
|
Runtime: %s
|
2018-11-16 04:17:20 +00:00
|
|
|
|
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) {
|
2018-12-11 09:51:20 +00:00
|
|
|
if viper.GetBool("version") {
|
2018-11-26 00:49:16 +00:00
|
|
|
printVersion()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2018-12-11 09:51:20 +00:00
|
|
|
if cmd == cmd.Root() {
|
2018-12-15 10:30:29 +00:00
|
|
|
fmt.Printf(logo, version.Tag, version.Commit, version.Date, runtime.Version())
|
2016-01-04 18:26:32 +00:00
|
|
|
}
|
2015-05-28 23:59:08 +00:00
|
|
|
|
2020-03-10 22:56:07 +00:00
|
|
|
storage.Initialize()
|
2015-05-28 23:59:08 +00:00
|
|
|
|
2020-03-10 22:56:07 +00:00
|
|
|
initConfig(storage.ConfigPath.Config(), viper.GetBool("reset-config"))
|
2016-01-04 18:26:32 +00:00
|
|
|
},
|
2016-01-07 22:59:38 +00:00
|
|
|
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2017-06-06 22:17:46 +00:00
|
|
|
if viper.GetBool("dev") {
|
|
|
|
log.Println("Running in development mode, access client at http://localhost:3000")
|
|
|
|
}
|
2020-03-10 22:56:07 +00:00
|
|
|
log.Println("Storing data at", storage.DataPath.Root())
|
2016-01-07 22:59:38 +00:00
|
|
|
|
2020-03-10 22:56:07 +00:00
|
|
|
db, err := boltdb.New(storage.DataPath.Database())
|
2018-05-31 21:24:59 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
2018-12-11 09:51:20 +00:00
|
|
|
cfg, cfgUpdated := config.LoadConfig()
|
|
|
|
dispatch := server.New(cfg)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
dispatch.SetConfig(<-cfgUpdated)
|
|
|
|
log.Println("New config loaded")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
dispatch.Store = db
|
|
|
|
dispatch.SessionStore = db
|
|
|
|
|
|
|
|
dispatch.GetMessageStore = func(user *storage.User) (storage.MessageStore, error) {
|
2020-03-10 22:56:07 +00:00
|
|
|
return boltdb.New(storage.DataPath.Log(user.Username))
|
2018-12-11 09:51:20 +00:00
|
|
|
}
|
2018-05-31 21:24:59 +00:00
|
|
|
|
2018-12-11 09:51:20 +00:00
|
|
|
dispatch.GetMessageSearchProvider = func(user *storage.User) (storage.MessageSearchProvider, error) {
|
2020-03-10 22:56:07 +00:00
|
|
|
return bleve.New(storage.DataPath.Index(user.Username))
|
2018-05-31 21:24:59 +00:00
|
|
|
}
|
2016-01-25 05:01:40 +00:00
|
|
|
|
2018-12-11 09:51:20 +00:00
|
|
|
dispatch.Run()
|
2016-01-07 22:59:38 +00:00
|
|
|
},
|
2016-01-04 18:26:32 +00:00
|
|
|
}
|
2015-05-28 23:59:08 +00:00
|
|
|
|
2016-01-04 18:26:32 +00:00
|
|
|
func Execute() {
|
|
|
|
rootCmd.Execute()
|
|
|
|
}
|
2015-05-01 20:59:46 +00:00
|
|
|
|
|
|
|
func init() {
|
2015-05-01 23:02:21 +00:00
|
|
|
rootCmd.AddCommand(clearCmd)
|
2015-05-25 02:00:21 +00:00
|
|
|
rootCmd.AddCommand(configCmd)
|
2018-11-26 00:49:16 +00:00
|
|
|
rootCmd.AddCommand(versionCmd)
|
2015-05-25 02:00:21 +00:00
|
|
|
|
2020-03-10 22:56:07 +00:00
|
|
|
rootCmd.PersistentFlags().String("data", storage.DefaultDirectory(), "directory to store data in")
|
|
|
|
rootCmd.PersistentFlags().String("conf", storage.DefaultDirectory(), "directory to store configuration in")
|
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")
|
2018-08-22 07:16:44 +00:00
|
|
|
rootCmd.Flags().StringP("address", "a", "", "interface to which the server will bind")
|
2016-01-25 05:01:40 +00:00
|
|
|
rootCmd.Flags().IntP("port", "p", 80, "port to listen on")
|
|
|
|
rootCmd.Flags().Bool("dev", false, "development mode")
|
2018-11-26 00:49:16 +00:00
|
|
|
rootCmd.Flags().BoolP("version", "v", false, "show version")
|
2015-05-01 23:02:21 +00:00
|
|
|
|
2018-12-11 09:51:20 +00:00
|
|
|
viper.BindPFlags(rootCmd.PersistentFlags())
|
|
|
|
viper.BindPFlags(rootCmd.Flags())
|
2016-01-14 05:35:26 +00:00
|
|
|
|
2018-08-10 18:24:29 +00:00
|
|
|
viper.SetDefault("hexIP", false)
|
2018-12-11 09:51:20 +00:00
|
|
|
viper.SetDefault("verify_certificates", true)
|
2015-05-01 20:59:46 +00:00
|
|
|
}
|
|
|
|
|
2018-04-14 23:25:21 +00:00
|
|
|
func initConfig(configPath string, overwrite bool) {
|
|
|
|
if _, err := os.Stat(configPath); overwrite || os.IsNotExist(err) {
|
2015-05-28 23:59:08 +00:00
|
|
|
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)
|
|
|
|
|
2015-05-28 23:59:08 +00:00
|
|
|
err = ioutil.WriteFile(configPath, config, 0600)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|