2017-05-05 15:08:02 +02:00
package config
2017-05-05 14:10:35 +02:00
import (
"flag"
"io"
2017-07-10 14:11:05 +02:00
"sync"
2017-05-05 14:10:35 +02:00
2017-07-22 01:24:13 +02:00
"fmt"
2017-07-10 14:11:05 +02:00
"github.com/jinzhu/configor"
2017-05-31 04:21:57 +02:00
yaml "gopkg.in/yaml.v2"
2017-05-08 23:40:54 +02:00
)
2017-07-10 14:11:05 +02:00
var config * Config
var once sync . Once
2017-07-23 07:46:29 +02:00
// Configpaths default configuration file paths
2017-07-22 01:24:13 +02:00
var Configpaths = [ ] string { "config/config.yml" , "config/default_config.yml" }
2017-07-10 14:11:05 +02:00
2017-07-23 07:46:29 +02:00
// Get config variable
2017-07-10 14:11:05 +02:00
func Get ( ) * Config {
once . Do ( func ( ) {
config = & Config { }
} )
return config
}
2017-05-31 04:21:57 +02:00
2017-05-26 12:12:52 +02:00
// IsSukebei : Tells if we are on the sukebei website
2017-05-20 12:45:27 +02:00
func IsSukebei ( ) bool {
2017-07-10 14:11:05 +02:00
return Get ( ) . Models . TorrentsTableName == "sukebei_torrents"
2017-05-29 06:51:43 +02:00
}
2017-05-14 00:55:17 +02:00
2017-06-08 20:08:30 +02:00
// WebAddress : Returns web address for current site
func WebAddress ( ) string {
if IsSukebei ( ) {
2017-07-10 14:11:05 +02:00
return Get ( ) . WebAddress . Sukebei
2017-06-08 20:08:30 +02:00
} else {
2017-07-10 14:11:05 +02:00
return Get ( ) . WebAddress . Nyaa
2017-06-08 20:08:30 +02:00
}
}
2017-05-05 14:10:35 +02:00
var allowedDatabaseTypes = map [ string ] bool {
"sqlite3" : true ,
"postgres" : true ,
"mysql" : true ,
"mssql" : true ,
}
2017-05-11 08:01:12 +02:00
var allowedDBLogModes = map [ string ] bool {
"default" : true , // errors only
"detailed" : true ,
"silent" : true ,
}
2017-05-31 04:21:57 +02:00
func init ( ) {
2017-07-10 14:11:05 +02:00
Reload ( )
2017-05-31 04:21:57 +02:00
}
2017-07-10 14:11:05 +02:00
// Reload the configuration from the files provided in the config variables
func Reload ( ) {
2017-07-22 01:24:13 +02:00
fmt . Println ( "Config reload" )
fmt . Println ( Configpaths )
2017-07-21 01:36:25 +02:00
configor . Load ( Get ( ) , Configpaths ... )
2017-05-05 14:10:35 +02:00
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
// BindFlags returns a function which is to be used after
// flag.Parse to check and copy the flags' values to the Config instance.
2017-07-21 01:20:04 +02:00
func BindFlags ( ) func ( ) {
2017-07-21 01:36:25 +02:00
confFile := flag . String ( "conf" , Configpaths [ 1 ] , "path to the configuration file" )
2017-07-10 14:11:05 +02:00
flag . StringVar ( & Get ( ) . DBType , "dbtype" , Get ( ) . DBType , "database backend" )
flag . StringVar ( & Get ( ) . Host , "host" , Get ( ) . Host , "binding address of the server" )
flag . IntVar ( & Get ( ) . Port , "port" , Get ( ) . Port , "port of the server" )
flag . StringVar ( & Get ( ) . DBParams , "dbparams" , Get ( ) . DBParams , "parameters to open the database (see Gorm's doc)" )
flag . StringVar ( & Get ( ) . DBLogMode , "dblogmode" , Get ( ) . DBLogMode , "database log verbosity (errors only by default)" )
2017-07-21 01:20:04 +02:00
return func ( ) {
if * confFile != "" {
2017-07-22 01:24:13 +02:00
Configpaths = append ( [ ] string { * confFile } , Configpaths ... )
2017-07-21 01:20:04 +02:00
Reload ( )
}
}
2017-05-05 14:10:35 +02:00
}
2017-05-26 12:12:52 +02:00
// Pretty : Write config json in a file
2017-05-05 14:10:35 +02:00
func ( config * Config ) Pretty ( output io . Writer ) error {
2017-05-31 04:21:57 +02:00
data , err := yaml . Marshal ( config )
2017-05-05 14:10:35 +02:00
if err != nil {
return err
}
_ , err = output . Write ( data )
return err
2017-05-07 21:51:37 +02:00
}