Opt: opt file path

pull/31/head
zijiren233 3 years ago
parent 80b1f43c26
commit 4b228748a2

@ -74,14 +74,20 @@ func Server(cmd *cobra.Command, args []string) {
if err != nil {
log.Fatal(err)
}
utils.OptFilePath(&conf.Conf.Server.Http.CertPath)
utils.OptFilePath(&conf.Conf.Server.Http.KeyPath)
if conf.Conf.Server.Rtmp.Enable {
if useMux {
muxer := cmux.New(serverHttpListener)
e := server.NewAndInit()
switch {
case conf.Conf.Server.Http.CertPath != "" && conf.Conf.Server.Http.KeyPath != "":
conf.Conf.Server.Http.CertPath, err = utils.OptFilePath(conf.Conf.Server.Http.CertPath)
if err != nil {
log.Fatalf("cert path error: %s", err)
}
conf.Conf.Server.Http.KeyPath, err = utils.OptFilePath(conf.Conf.Server.Http.KeyPath)
if err != nil {
log.Fatalf("key path error: %s", err)
}
httpl := muxer.Match(cmux.HTTP2(), cmux.TLS())
go http.ServeTLS(httpl, e.Handler(), conf.Conf.Server.Http.CertPath, conf.Conf.Server.Http.KeyPath)
if conf.Conf.Server.Http.Quic {
@ -100,6 +106,14 @@ func Server(cmd *cobra.Command, args []string) {
e := server.NewAndInit()
switch {
case conf.Conf.Server.Http.CertPath != "" && conf.Conf.Server.Http.KeyPath != "":
conf.Conf.Server.Http.CertPath, err = utils.OptFilePath(conf.Conf.Server.Http.CertPath)
if err != nil {
log.Fatalf("cert path error: %s", err)
}
conf.Conf.Server.Http.KeyPath, err = utils.OptFilePath(conf.Conf.Server.Http.KeyPath)
if err != nil {
log.Fatalf("key path error: %s", err)
}
go http.ServeTLS(serverHttpListener, e.Handler(), conf.Conf.Server.Http.CertPath, conf.Conf.Server.Http.KeyPath)
if conf.Conf.Server.Http.Quic {
go http3.ListenAndServeQUIC(udpServerHttpAddr.String(), conf.Conf.Server.Http.CertPath, conf.Conf.Server.Http.KeyPath, e.Handler())

@ -18,7 +18,7 @@ func InitDefaultConfig(ctx context.Context) error {
return nil
}
func InitConfig(ctx context.Context) error {
func InitConfig(ctx context.Context) (err error) {
if flags.SkipConfig && flags.SkipEnv {
log.Fatal("skip config and skip env at the same time")
return errors.New("skip config and skip env at the same time")
@ -28,9 +28,12 @@ func InitConfig(ctx context.Context) error {
if flags.ConfigFile == "" {
flags.ConfigFile = filepath.Join(flags.DataDir, "config.yaml")
} else {
utils.OptFilePath(&flags.ConfigFile)
flags.ConfigFile, err = utils.OptFilePath(flags.ConfigFile)
if err != nil {
log.Fatalf("config file path error: %v", err)
}
}
err := confFromConfig(flags.ConfigFile, conf.Conf)
err = confFromConfig(flags.ConfigFile, conf.Conf)
if err != nil {
log.Fatalf("load config from file error: %v", err)
}

@ -19,7 +19,7 @@ import (
"gorm.io/gorm/logger"
)
func InitDatabase(ctx context.Context) error {
func InitDatabase(ctx context.Context) (err error) {
var dialector gorm.Dialector
var opts []gorm.Option
switch conf.Conf.Database.Type {
@ -67,7 +67,10 @@ func InitDatabase(ctx context.Context) error {
if !strings.HasSuffix(conf.Conf.Database.DBName, ".db") {
conf.Conf.Database.DBName = conf.Conf.Database.DBName + ".db"
}
utils.OptFilePath(&conf.Conf.Database.DBName)
conf.Conf.Database.DBName, err = utils.OptFilePath(conf.Conf.Database.DBName)
if err != nil {
log.Fatalf("sqlite3 database file path error: %v", err)
}
dsn = fmt.Sprintf("%s?_journal_mode=WAL&_vacuum=incremental&_pragma=foreign_keys(1)", conf.Conf.Database.DBName)
log.Infof("sqlite3 database file: %s", conf.Conf.Database.DBName)
}

@ -24,10 +24,13 @@ func setLog(l *logrus.Logger) {
}
}
func InitLog(ctx context.Context) error {
func InitLog(ctx context.Context) (err error) {
setLog(logrus.StandardLogger())
if conf.Conf.Log.Enable {
utils.OptFilePath(&conf.Conf.Log.FilePath)
conf.Conf.Log.FilePath, err = utils.OptFilePath(conf.Conf.Log.FilePath)
if err != nil {
logrus.Fatalf("log: log file path error: %v", err)
}
var l = &lumberjack.Logger{
Filename: conf.Conf.Log.FilePath,
MaxSize: conf.Conf.Log.MaxSize,

@ -15,18 +15,22 @@ import (
"github.com/synctv-org/synctv/utils"
)
func InitProvider(ctx context.Context) error {
func InitProvider(ctx context.Context) (err error) {
logOur := log.StandardLogger().Writer()
logLevle := hclog.Info
if flags.Dev {
logLevle = hclog.Debug
}
for _, op := range conf.Conf.OAuth2.Plugins {
utils.OptFilePath(&op.PluginFile)
op.PluginFile, err = utils.OptFilePath(op.PluginFile)
if err != nil {
log.Fatalf("oauth2 plugin file path error: %v", err)
return err
}
log.Infof("load oauth2 plugin: %s", op.PluginFile)
err := os.MkdirAll(filepath.Dir(op.PluginFile), 0755)
if err != nil {
log.Errorf("create plugin dir: %s failed: %s", filepath.Dir(op.PluginFile), err)
log.Fatalf("create plugin dir: %s failed: %s", filepath.Dir(op.PluginFile), err)
return err
}
err = plugins.InitProviderPlugins(op.PluginFile, op.Arges, hclog.New(&hclog.LoggerOptions{
@ -36,7 +40,7 @@ func InitProvider(ctx context.Context) error {
Color: hclog.ForceColor,
}))
if err != nil {
log.Errorf("load oauth2 plugin: %s failed: %s", op.PluginFile, err)
log.Fatalf("load oauth2 plugin: %s failed: %s", op.PluginFile, err)
return err
}
}

@ -274,13 +274,11 @@ func getLocalIPs() []net.IP {
return localIPs
}
func OptFilePath(filePath *string) {
if filePath == nil || *filePath == "" {
return
}
if !filepath.IsAbs(*filePath) {
*filePath = filepath.Join(flags.DataDir, *filePath)
func OptFilePath(filePath string) (string, error) {
if !filepath.IsAbs(filePath) {
return filepath.Abs(filepath.Join(flags.DataDir, filePath))
}
return filePath, nil
}
func LIKE(s string) string {

Loading…
Cancel
Save