當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Session.Redirect方法代碼示例

本文整理匯總了Golang中github.com/companieshouse/gotcha/http.Session.Redirect方法的典型用法代碼示例。如果您正苦於以下問題:Golang Session.Redirect方法的具體用法?Golang Session.Redirect怎麽用?Golang Session.Redirect使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/companieshouse/gotcha/http.Session的用法示例。


在下文中一共展示了Session.Redirect方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: import1

func import1(session *http.Session) {
	session.Stash["indexes"] = indexes
	session.Stash["Title"] = "SmartPAN Import"

	m := &ImportForm{}
	f := form.New(session, m)
	session.Stash["fh"] = f

	log.Info("Headers: %s", session.Request.Header())

	if session.Request.Method != "POST" {
		render_import(session)
		return
	}

	f.Populate(true)
	f.Validate()

	if f.HasErrors {
		render_import(session)
		return
	}

	log.Info("Importing into: %s", m.ImportInto)
	if m.ImportInto == "new_index" {
		if len(m.NewIndex) == 0 {
			f.HasErrors = true
			f.Errors["NewIndex"] = make(map[string]error)
			f.Errors["NewIndex"]["required"] = errors.New("Please give the new repository a name")
			render_import(session)
			return
		}
		log.Info("=> Creating new index: %s", m.NewIndex)
	}

	b := make([]byte, 20)
	rand.Read(b)
	en := base64.URLEncoding
	d := make([]byte, en.EncodedLen(len(b)))
	en.Encode(d, b)

	job := &ImportJob{
		Form:     m,
		Complete: false,
		Id:       string(d),
		Watchers: make([]func(string), 0),
	}

	if len(m.Cpanfile) > 0 {
		log.Info("Got cpanfile:")
		log.Info(m.Cpanfile)
	}

	log.Info("=> Created import job: %s", job.Id)
	imports[job.Id] = job

	go do_import(session, job)

	//render_import(session)
	if _, ok := session.Request.Form()["stream"]; ok {
		session.Redirect(&url.URL{Path: "/import/" + job.Id + "/stream", RawQuery: "raw=y"})
	} else {
		session.Redirect(&url.URL{Path: "/import/" + job.Id})
	}
}
開發者ID:companieshouse,項目名稱:gopan,代碼行數:65,代碼來源:imports.go

示例2: getindex

func getindex(session *http.Session) {
	idx := session.Stash["index"]

	switch idx {
	case "CPAN":
		go func() {
			config.CPANStatus = "Downloading"

			res, err := nethttp.Get("https://s3-eu-west-1.amazonaws.com/gopan/cpan_index.gz")
			if err != nil {
				log.Error("Error downloading index: %s", err.Error())
				session.RenderException(500, errors.New("Error downloading CPAN index: "+err.Error()))
				config.CPANStatus = "Failed"
				return
			}
			defer res.Body.Close()
			b, err := ioutil.ReadAll(res.Body)
			if err != nil {
				log.Error("Error reading index: %s", err.Error())
				session.RenderException(500, errors.New("Error reading CPAN index: "+err.Error()))
				config.CPANStatus = "Failed"
				return
			}
			fi, err := os.Create(config.CacheDir + "/" + config.CPANIndex)
			if err != nil {
				log.Error("Error creating output file: %s", err.Error())
				session.RenderException(500, errors.New("Error creating output file: "+err.Error()))
				config.CPANStatus = "Failed"
				return
			}
			defer fi.Close()
			fi.Write(b)

			config.CPANStatus = "Downloaded"
			config.HasCPANIndex = true
			config.CPANIndexDate = time.Now().String()

			config.CPANStatus = "Loading"
			load_index(config.CPANIndex, config.CacheDir+"/"+config.CPANIndex)

			config.CPANStatus = "Indexing"
			update_indexes()

			config.CPANStatus = "Loaded"
		}()

		session.Redirect(&url.URL{Path: "/settings"})
		return
	case "BackPAN":
		go func() {
			config.BackPANStatus = "Downloading"

			res, err := nethttp.Get("https://s3-eu-west-1.amazonaws.com/gopan/backpan_index.gz")
			if err != nil {
				log.Error("Error downloading index: %s", err.Error())
				session.RenderException(500, errors.New("Error downloading BackPAN index: "+err.Error()))
				config.BackPANStatus = "Failed"
				return
			}
			defer res.Body.Close()
			b, err := ioutil.ReadAll(res.Body)
			if err != nil {
				log.Error("Error reading index: %s", err.Error())
				session.RenderException(500, errors.New("Error reading BackPAN index: "+err.Error()))
				config.BackPANStatus = "Failed"
				return
			}
			fi, err := os.Create(config.CacheDir + "/" + config.BackPANIndex)
			if err != nil {
				log.Error("Error creating output file: %s", err.Error())
				session.RenderException(500, errors.New("Error creating output file: "+err.Error()))
				config.BackPANStatus = "Failed"
				return
			}
			defer fi.Close()
			fi.Write(b)

			config.BackPANStatus = "Downloaded"
			config.HasBackPANIndex = true
			config.BackPANIndexDate = time.Now().String()

			config.BackPANStatus = "Loading"
			load_index(config.BackPANIndex, config.CacheDir+"/"+config.BackPANIndex)

			config.BackPANStatus = "Indexing"
			update_indexes()

			config.BackPANStatus = "Loaded"
		}()

		session.Redirect(&url.URL{Path: "/settings"})
		return
	}

	session.RenderNotFound()
}
開發者ID:companieshouse,項目名稱:gopan,代碼行數:96,代碼來源:main.go


注:本文中的github.com/companieshouse/gotcha/http.Session.Redirect方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。