Ver código fonte

Use a global struct for configuration

Andrea Fazzi 7 anos atrás
pai
commit
e2a5e39e49

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+**/*~
+core
+

+ 7 - 29
api/api_test.go

@@ -1,16 +1,14 @@
 package api
 
 import (
-	"encoding/json"
 	"io/ioutil"
-	"net/http"
 	"net/http/httptest"
 	"path/filepath"
 	"testing"
 
 	"github.com/remogatto/prettytest"
-	"gogs.carducci-dante.gov.it/andrea.fazzi/karmen/config"
-	"gogs.carducci-dante.gov.it/andrea.fazzi/karmen/renderer"
+	"gogs.carducci-dante.gov.it/karmen/core/config"
+	"gogs.carducci-dante.gov.it/karmen/core/renderer"
 )
 
 var (
@@ -41,36 +39,16 @@ func TestRunner(t *testing.T) {
 }
 
 func (t *testSuite) BeforeAll() {
-	config.LogLevel = config.LOG_LEVEL_OFF
-	config.DocumentRootDir = "./testdata/documents"
+	err := config.Load("testdata/config.yaml", config.Config)
+	if err != nil {
+		panic(err)
+	}
+	config.Config.LogLevel = config.LOG_LEVEL_OFF
 
 	rend := renderer.NewJSONRenderer()
 	renderer.Use(rend)
 }
 
-func (t *testSuite) TestGetDocumentsFromFS() {
-	documents := getDocumentsFromFS()
-	t.Equal(2, len(documents))
-	t.Equal("Document 1", documents[0].Config.Name)
-	t.Equal("Document 2", documents[1].Config.Name)
-
-}
-
-func (t *testSuite) TestDocumentsHandler() {
-	var data struct {
-		Documents []Document
-	}
-	req, err := http.NewRequest("GET", "/documents", nil)
-	t.Nil(err)
-	rr := httptest.NewRecorder()
-	documentsHandler().ServeHTTP(rr, req)
-	t.Equal(http.StatusOK, rr.Code)
-	err = json.Unmarshal(rr.Body.Bytes(), &data)
-	t.Nil(err)
-	t.Equal(2, len(data.Documents))
-	t.Equal("Document 1", data.Documents[0].Config.Name)
-}
-
 // func (t *testSuite) TestGetToken() {
 // 	tokenUrl := fmt.Sprintf("%s/get-token", server.URL)
 // 	resp, err := http.Get(tokenUrl)

+ 2 - 2
api/login.go

@@ -57,8 +57,8 @@ func loginHandler() http.Handler {
 }
 
 func queryDB(username string, password string) (*User, error) {
-	log.Println(username, config.AdminUsername, password, config.AdminPassword)
-	if username == config.AdminUsername && password == config.AdminPassword {
+	log.Println(username, config.Config.Admin.Username, password, config.Config.Admin.Password)
+	if username == config.Config.Admin.Username && password == config.Config.Admin.Password {
 		return &User{username, true}, nil
 	}
 	return nil, errors.New("Authentication failed!")

+ 30 - 0
api/testdata/config.yaml

@@ -0,0 +1,30 @@
+domain: "foo.org"
+log_level: 2
+
+orm:
+  connection: "root:password@tcp(localhost:3306)/karmen_dev"
+  options: "charset=utf8&parseTime=True&loc=Local"
+  automigrate: true
+  reset: false
+  regenerate: false
+
+admin:
+  username: "admin"
+  password: "admin"
+
+ldap:
+  host: "localhost"
+  port: 389
+  bind_password: "admin"
+  bind_user: "cn=admin,dc=foo,dc=org"
+  root_dn: "dc=foo,dc=org"
+
+limesurvey:
+  url: "http://localhost:8081/index.php/admin/remotecontrol"
+  username: "admin"
+  password: "password"
+
+cloud:
+  url: "http://localhost:8082/remote.php/webdav/"
+  username: "admin"
+  password: "password"

+ 69 - 35
config/config.go

@@ -1,56 +1,90 @@
 package config
 
+import (
+	"io/ioutil"
+
+	yaml "gopkg.in/yaml.v2"
+)
+
 const (
 	LOG_LEVEL_OFF = iota
 	LOG_LEVEL_INFO
 	LOG_LEVEL_DEBUG
 )
 
-var (
-	// Database
+type ConfigT struct {
 
-	DBConnection      = "root:password@tcp(localhost:3306)/karmen_dev"
-	DBConnectionOpts  = "charset=utf8&parseTime=True&loc=Local"
-	ResetOnBoot       = false
-	AutoMigrateOnBoot = true
-	RegenerateOnBoot  = false
+	// Domain
 
-	AdminUsername = "admin"
-	AdminPassword = "admin"
+	Domain string
 
-	Domain = "foo.org"
+	// Logging
 
-	// LDAP
+	LogLevel int `yaml:"log_level"`
 
-	LDAPHostAddress  = "localhost"
-	LDAPHostPort     = 389
-	LDAPBindPassword = "admin"
-	LDAPBindUser     = "cn=admin,dc=foo,dc=org"
-	LDAPRootDN       = "dc=foo,dc=org"
+	// Admin credentials
 
-	LDAPPeopleOU        = "Persone"
-	LDAPTeacherOU       = "Docenti"
-	LDAPGroupsOU        = "Gruppi"
-	LDAPTeachersGroupOU = "Docenti"
+	Admin struct {
+		Username string
+		Password string
+	}
 
-	// LimeSurvey Remote Control URL
-
-	LSAPIRemoteControlUrl = "http://localhost:8081/index.php/admin/remotecontrol"
-	LSAPIUsername         = "admin"
-	LSAPIPassword         = "password"
+	// Database
 
-	// WebDAV
+	Orm struct {
+		Connection  string
+		Options     string
+		Reset       bool
+		AutoMigrate bool
+		Regenerate  bool
+	}
 
-	WebDAVUrl      = "http://localhost:8082/remote.php/webdav/"
-	WebDavUsername = "admin"
-	WebDAVPassword = "password"
+	// LDAP
 
-	// Document generator
+	Ldap struct {
+		Host         string
+		Port         string
+		BindPassword string `yaml:"bind_password"`
+		BindUser     string `yaml:"bind_user"`
+		RootDN       string `yaml:"root_dn"`
 
-	DocumentRootDir  = "documents/"
-	DocumentConfigFn = "config.yaml"
+		OuPeople        string `yaml:"ou_people"`
+		OuTeacher       string `yaml:"ou_teacher"`
+		OuGroup         string `yaml:"ou_group"`
+		OuTeachersGroup string `yaml:"ou_teachers_group"`
+	}
 
-	// Logging
+	// LimeSurvey Remote Control URL
 
-	LogLevel = LOG_LEVEL_DEBUG
-)
+	Limesurvey struct {
+		Url      string
+		Username string
+		Password string
+	}
+
+	// Cloud
+
+	Cloud struct {
+		Url      string
+		Username string
+		Password string
+	}
+}
+
+var Config *ConfigT
+
+func init() {
+	Config = new(ConfigT)
+}
+
+// Load loads the config file placed at the given path.
+func Load(path string, config *ConfigT) error {
+	cf, err := ioutil.ReadFile(path)
+	if err != nil {
+		return err
+	}
+	if err := yaml.Unmarshal(cf, config); err != nil {
+		return err
+	}
+	return nil
+}

+ 30 - 0
config/config.yaml

@@ -0,0 +1,30 @@
+domain: "foo.org"
+log_level: 2
+
+orm:
+  connection: "root:password@tcp(localhost:3306)/karmen_dev"
+  options: "charset=utf8&parseTime=True&loc=Local"
+  automigrate: true
+  reset: false
+  regenerate: false
+
+admin:
+  username: "admin"
+  password: "admin"
+
+ldap:
+  host: "localhost"
+  port: 389
+  bind_password: "admin"
+  bind_user: "cn=admin,dc=foo,dc=org"
+  root_dn: "dc=foo,dc=org"
+
+limesurvey:
+  url: "http://localhost:8081/index.php/admin/remotecontrol"
+  username: "admin"
+  password: "password"
+
+cloud:
+  url: "http://localhost:8082/remote.php/webdav/"
+  username: "admin"
+  password: "password"

+ 10 - 4
main.go

@@ -39,26 +39,32 @@ func regenerateRelations(db *gorm.DB) error {
 }
 
 func main() {
+	log.Println("Loading config file...")
+	err := config.Load("config/config.yaml", config.Config)
+	if err != nil {
+		panic(err)
+	}
+
 	log.Println("Starting karmen...")
 
-	db, err := orm.New(fmt.Sprintf("%s?%s", config.DBConnection, config.DBConnectionOpts))
+	db, err := orm.New(fmt.Sprintf("%s?%s", config.Config.Orm.Connection, config.Config.Orm.Options))
 	if err != nil {
 		panic(err)
 	}
 
 	orm.Use(db)
 
-	if config.ResetOnBoot {
+	if config.Config.Orm.Reset {
 		log.Print("Resetting the DB...")
 		orm.Reset()
 	}
 
-	if config.AutoMigrateOnBoot {
+	if config.Config.Orm.AutoMigrate {
 		log.Print("Automigrating...")
 		orm.AutoMigrate()
 	}
 
-	if config.RegenerateOnBoot {
+	if config.Config.Orm.Regenerate {
 		log.Print("Regenerate relations...")
 		if err := regenerateRelations(db); err != nil {
 			panic(err)

+ 1 - 1
renderer/renderer.go

@@ -57,7 +57,7 @@ func NewHTMLRenderer(templatePath string) (*HTMLRenderer, error) {
 	}
 
 	for _, fn := range fns {
-		if config.LogLevel > config.LOG_LEVEL_OFF {
+		if config.Config.LogLevel > config.LOG_LEVEL_OFF {
 			log.Printf("Load %s template", fn)
 		}
 

+ 1 - 1
vendor/gogs.carducci-dante.gov.it/karmen/orm/teacher.go

@@ -234,7 +234,7 @@ func (t Teacher) IsMinuter() []Class {
 func (t *Teacher) generateCredential() error {
 	t.Username = t.GenerateUsername()
 	t.Password = t.GenerateSaltedPassword(t.Username)
-	t.Email = fmt.Sprintf("%s@%s", t.Username, config.Domain)
+	t.Email = fmt.Sprintf("%s@%s", t.Username, config.Config.Domain)
 	return nil
 }