Example #1
0
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

}
Example #2
0
func BuildServer(config *ServerConfig) (IServer, error) {

	s := &Server{
		config: config,
	}
	var err error

	s.core, err = config.GetCore()
	if err != nil {
		return nil, err
	}

	lilo := torch.GetBasicLoginLogout(s.core.DB, "staff")

	authData := map[string]interface{}{}
	if config.GoogleAuthConfig != nil {
		lilo.AddAuthenticator(config.GoogleAuthConfig)
		authData["AuthCallback"] = config.GoogleAuthConfig.RedirectURI
		authData["GoogleClientID"] = config.GoogleAuthConfig.ClientID
	}

	s.sessionStore = torch.InMemorySessionStore(config.SessionDumpFile, lilo.LoadUserById, s.core.OpenDatabaseConnection, s.core.CloseDatabaseConnection)

	parser := torch.BasicParser(s.sessionStore, config.PublicPatternsRaw)

	// Register the AJAX and Socket methods (These methods work on both)
	handlerMap := map[string]actions.Handler{
		"get":     &actions.SelectQuery{Core: s.core},
		"set":     &actions.UpdateQuery{Core: s.core},
		"create":  &actions.CreateQuery{Core: s.core},
		"delete":  &actions.DeleteQuery{Core: s.core},
		"custom":  &actions.CustomQuery{Core: s.core},
		"dynamic": &actions.DynamicHandler{Core: s.core},
		"ping":    &actions.PingAction{Core: s.core},
		"msg":     &actions.MsgAction{Core: s.core},
	}

	socketManager := socket.GetManager(parser.Store)
	s.router = router.GetRouter(parser, config.PublicPatternsRaw)

	for funcName, handler := range handlerMap {
		func(funcName string, handler actions.Handler) {
			socketManager.RegisterHandler(funcName, handler)
			s.router.AddRoute("/ajax/"+funcName, actions.AsRouterHandler(handler), "POST")
		}(funcName, handler)
	}

	loginViewHandler := s.core.TemplateManager.GetSimpleTemplateHandler("login.html", authData)

	setPasswordViewHandler := s.core.TemplateManager.GetSimpleTemplateHandler("set_password.html", nil)

	// SET UP URLS

	http.Handle("/socket", socketManager.GetListener())
	http.HandleFunc("/script/", s.core.runScript)

	s.router.AddRoute("/login", loginViewHandler, "GET")
	s.router.AddRouteFunc("/oauth_callback", lilo.HandleOauthCallback, "GET")
	s.router.AddRouteFunc("/login", lilo.HandleLogin, "POST")
	s.router.AddRouteFunc("/logout", lilo.HandleLogout)
	s.router.AddRoute("/set_password", setPasswordViewHandler, "GET")
	s.router.AddRouteFunc("/set_password", lilo.HandleSetPassword, "POST")

	s.router.AddRoutePathFunc("/report/%s/%d/*.html", s.core.Reporter.HandleReportRequest, "GET")
	s.router.AddRoutePathFunc("/report/%s/%d/*.pdf", s.core.PDFHandler.Handle, "GET")

	s.router.AddRoutePathFunc("/emailpreview/%s/%d", s.core.Reporter.HandleReportRequest, "GET")
	s.router.AddRoutePathFunc("/sendmail/%s/%d/%s/%s", s.core.MailHandler.Handle)

	s.router.AddRoutePathFunc("/csv/%s", s.core.CSVHandler.Handle)

	/*
		model := s.core.GetModel()
			fileHandler := file.GetFileHandler(config.UploadDirectory, model)
			http.HandleFunc("/upload/", parser.Wrap(fileHandler.Upload))
			http.HandleFunc("/download/", parser.Wrap(fileHandler.Download))
	*/
	s.router.Redirect("/", "app.html")
	s.router.Fallthrough(config.WebRoot)

	return s, nil
}