func GetServer(config *ServerConfig) (*Server, error) { db, err := sql.Open("mysql", config.DSN) if err != nil { return nil, err } config.PublicPatterns = append(config.PublicPatterns, "/login") s := &Server{ Config: config, DB: db, } templateManager := view.GetTemplateManager(config.TemplateRoot, config.TemplateIncludeRoot) s.TemplateManager = templateManager lilo := torch.GetBasicLoginLogout(db, "user") s.sessionStore = torch.InMemorySessionStore(config.SessionDumpFile, lilo.LoadUserById, s.getDb) s.parser = torch.BasicParser(s.sessionStore, config.PublicPatterns) s.router = router.GetRouter(s.parser, config.PublicPatterns) s.router.Fallthrough(config.PublicRoot) s.router.AddRouteFunc("/login", lilo.HandleLogin, "POST") s.router.AddRouteFunc("/logout", lilo.HandleLogout) s.router.AddRouteFunc("/set_password", lilo.HandleSetPassword, "POST") return s, nil }
func (config *ServerConfig) GetCore() (core *GSDCore, err error) { Expand(&config.Xero.PrivateKeyFile) Expand(&config.ModelFile) Expand(&config.TemplateRoot) Expand(&config.WebRoot) Expand(&config.UploadDirectory) Expand(&config.TemplateIncludeRoot) Expand(&config.ScriptDirectory) Expand(config.ReportFile) Expand(config.SessionDumpFile) timezoneName := "Australia/Melbourne" if config.Timezone != nil { timezoneName = *config.Timezone } loc, err := time.LoadLocation(timezoneName) //"Australia/Melbourne") if err != nil { return nil, err } time.Local = loc core = &GSDCore{} if config.ReportFile != nil { log.Printf("Load email config from %s\n", *config.ReportFile) var rc map[string]reporter.ReportConfig err := FileNameToObject(*config.ReportFile, &rc) if err != nil { return nil, err } config.Reports = rc } ////////////////////// // Template Manager // log.Println("Load Template Manager") templateManager := view.GetTemplateManager(string(config.TemplateRoot), string(config.TemplateIncludeRoot)) core.TemplateManager = templateManager model, err := databath.ReadModelFromFile(string(config.ModelFile)) if err != nil { return nil, fmt.Errorf("Error reading model file: %s", err.Error()) } core.Model = model //////////// // Mailer // log.Println("Load Mailer") mailer := &mailer.Mailer{ Config: config.SmtpConfig, } core.Mailer = mailer ///////////// // Runner // log.Println("Load Script Runner") runner := &dynamic.DynamicRunner{ BaseDirectory: string(config.ScriptDirectory), Mailer: mailer, Xero: core.Xero, } core.Runner = runner ////////////// // Reporter // log.Println("Load Reporter") reporter := &reporter.Reporter{ ViewManager: templateManager, Runner: runner, Model: model, Reports: config.Reports, } core.Reporter = reporter /////////// // PDFer // log.Println("Load PDFer") pdfer := &pdfer.PDFer{ Binary: *config.PDFBinary, } core.PDFer = pdfer //////////////// // PDFHandler // log.Println("Load PDF Handler") pdfHandler := &pdfhandler.PDFHandler{ Reporter: reporter, PDFer: pdfer, } core.PDFHandler = pdfHandler ////////////////// // MailHandler // log.Println("Load Mail Handler") mailHandler := &mailhandler.MailHandler{ Mailer: mailer, Reporter: reporter, } core.MailHandler = mailHandler ///////// // CSV // log.Println("Load CSV Handler") csv := &csv.CSVHandler{ Model: model, } core.CSVHandler = csv //////////// // Hooker // log.Println("Load Hooker") hooker := &hooker.Hooker{ Model: model, Runner: runner, Reporter: reporter, Mailer: mailer, } core.Hooker = hooker core.Config = config log.Println("Connect to Database") db, err := sql.Open(core.Config.Database.Driver, core.Config.Database.DataSourceName) if err != nil { return nil, err } err = db.Ping() if err != nil { return nil, fmt.Errorf("Database ping failed: %s", err.Error()) } core.DB = db log.Println("End Config Loading") return core, err }