Beispiel #1
0
// Start starts the system.  If the second error parameter is not nil, then the
// system failed to start.  Future system halting errors are captured and
// returned on the channel.
func Start(configFile string) (chan error, error) {
	config.SetFile(configFile)

	dbUser := config.MustGet("Database", "username")
	dbPass := config.MustGet("Database", "password")
	dbName := config.MustGet("Database", "database")
	dbHost := config.MustGet("Database", "host")

	db, _ = sql.Open("postgres", fmt.Sprintf("user=%v password=%v dbname=%v host=%v", dbUser, dbPass, dbName, dbHost))
	dbMap = &gorp.DbMap{Db: db, Dialect: gorp.PostgresDialect{}}

	global.DB = db
	global.DbMap = dbMap

	dbLogger := golog.New(DBLog.Writer(log.INFO), "", 0)
	dbMap.TraceOn("GORP", dbLogger)

	criticalErrors := make(chan error)

	web.CreateSessionStore(SessionKey)

	if db == nil {
		return nil, errors.New("Database connection must be set before call to Start")
	}

	router := mux.NewRouter()

	templates.Router = router

	adder := func(path string, handle web.Handle) *mux.Route {
		ModuleLog.Info("Registered Path %v", path)
		return router.Handle(path, handle)
	}

	for _, module := range modules {
		err := boostrapModule(module)
		if err != nil {
			return nil, err
		}

		if pageModule, ok := module.(PageModule); ok {
			ModuleLog.Info(fmt.Sprintf("Registering paths from %v", module.Name()))
			pageModule.AddRoutes(adder)
		}
	}

	questions.InitializeDatabase(db, dbMap)
	subjects.Initialize(dbMap)

	questions.AddRoutes(adder)
	subjects.AddRoutes(adder)

	http.Handle("/static/", http.FileServer(http.Dir(AssetDirectory)))
	http.Handle("/", router)
	port, _ := config.Get("DEFAULT", "port")
	http.ListenAndServe(":"+port, nil)

	return criticalErrors, nil
}
Beispiel #2
0
func AddRoutes(handleAdder web.HandlerAdder) error {
	types.AddRoutes(handleAdder)
	rootURI := config.MustGet("Branding", "rootSubjectURIPrefix")
	handleAdder(fmt.Sprintf("/%s/{id:[0-9]+}", rootURI), simpleSubjectPage).Methods("GET")
	handleAdder("/s/{id:[0-9]+}", saveSubject).Methods("POST")
	return nil
}
Beispiel #3
0
func newPage(r *http.Request, title string, data interface{}) *page {
	p := &page{
		Title:   title,
		Data:    data,
		Flashes: web.Flashes(r),
		User:    context.Get(r, "user"),
		Meta: meta{
			SiteName: config.MustGet("Branding", "siteName"),
			CSRF:     web.CSRF(r),
		},
	}

	return p
}