• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang http.Session类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/companieshouse/gotcha/http.Session的典型用法代码示例。如果您正苦于以下问题:Golang Session类的具体用法?Golang Session怎么用?Golang Session使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Session类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: render_import

func render_import(session *http.Session) {
	html, _ := session.RenderTemplate("import.html")
	session.Stash["Page"] = "Import"
	session.Stash["ImportAvailable"] = config.ImportAvailable
	session.Stash["Content"] = template.HTML(html)
	session.Render("layout.html")
}
开发者ID:companieshouse,项目名称:gopan,代码行数:7,代码来源:imports.go


示例2: settings

func settings(session *http.Session) {
	session.Stash["Title"] = "SmartPAN Settings"
	html, _ := session.RenderTemplate("settings.html")

	session.Stash["Page"] = "Settings"
	session.Stash["Content"] = template.HTML(html)
	session.Render("layout.html")
}
开发者ID:companieshouse,项目名称:gopan,代码行数:8,代码来源:main.go


示例3: help

func help(session *http.Session) {
	session.Stash["Title"] = "SmartPAN Help"
	html, _ := session.RenderTemplate("help.html")

	session.Stash["Page"] = "Help"
	session.Stash["Content"] = template.HTML(html)
	session.Render("layout.html")
}
开发者ID:companieshouse,项目名称:gopan,代码行数:8,代码来源:main.go


示例4: importstream

func importstream(session *http.Session) {
	job := imports[session.Stash["jobid"].(string)]
	session.Stash["Job"] = job
	c := session.Response.Chunked()

	session.Response.Headers.Add("Content-Type", "text/plain")

	raw := false
	if _, ok := session.Request.Form()["raw"]; ok {
		raw = true
	}

	if !raw {
		header, _ := session.RenderTemplate("layout_streamstart.html")
		c <- []byte(header)
	}

	for _, s := range job.History {
		if !raw {
			c <- []byte(s + "<br />\n")
		} else {
			c <- []byte(s + "\n")
		}
	}

	var wg sync.WaitGroup
	if !job.Complete {
		wg.Add(1)
		job.Watchers = append(job.Watchers, func(m string) {
			if m == ":DONE" {
				wg.Done()
				return
			}
			if !raw {
				c <- []byte(m + "<br />\n")
			} else {
				c <- []byte(m + "\n")
			}
		})
	}

	wg.Wait()

	if !raw {
		footer, _ := session.RenderTemplate("layout_streamend.html")
		c <- []byte(footer)
	}

	c <- make([]byte, 0)
}
开发者ID:companieshouse,项目名称:gopan,代码行数:50,代码来源:imports.go


示例5: pkgindex

func pkgindex(session *http.Session) {
	if _, ok := session.Stash["repo"]; !ok {
		session.RenderNotFound()
		return
	}

	repo := session.Stash["repo"].(string)

	for fname, _ := range indexes {
		if _, ok := indexes[fname][repo]; !ok && repo != "SmartPAN" {
			session.RenderNotFound()
			return
		}
	}

	if g, ok := session.Stash["gz"]; ok {
		if len(g.(string)) > 0 {
			// cheat and hijack gotchas gzip support
			session.Response.Headers.Set("Content-Type", "application/gzip")
			session.Response.Send()
			session.Response.Gzip()
			session.Response.Headers.Remove("Content-Encoding")
			log.Debug("Using gzip")
		}
	}

	session.Response.WriteText("File:         02packages.details.txt\n")
	session.Response.WriteText("Description:  Package names found in directory " + repo + "/authors/id\n")
	session.Response.WriteText("Columns:      package name, version, path\n")
	session.Response.WriteText("Written-By:   SmartPAN (from GoPAN)\n")
	session.Response.WriteText("Line-Count:   " + strconv.Itoa(summary.Packages) + "\n") // FIXME wrong count
	session.Response.WriteText("\n")

	if repo == "SmartPAN" {
		for _, pkg := range packages {
			writepkgindex(session, pkg)
		}
	} else {
		for _, pkg := range idxpackages[repo] {
			writepkgindex(session, pkg)
		}
	}
}
开发者ID:companieshouse,项目名称:gopan,代码行数:43,代码来源:packages.go


示例6: import2

func import2(session *http.Session) {

	if !config.ImportAvailable {
		session.Render("error.html")
		session.Stash["GoPANError"] = "Import is currently unavailable"
		return
	}

	session.Stash["Page"] = "Import"
	session.Stash["Title"] = "Import job " + session.Stash["jobid"].(string)

	job := imports[session.Stash["jobid"].(string)]
	session.Stash["Job"] = job

	html, _ := session.RenderTemplate("import2.html")
	session.Stash["Content"] = template.HTML(html)

	session.Render("layout.html")
}
开发者ID:companieshouse,项目名称:gopan,代码行数:19,代码来源:imports.go


示例7: download

func download(session *http.Session) {
	if _, ok := session.Stash["repo"]; !ok {
		session.RenderNotFound()
		return
	}

	if _, ok := session.Stash["file"]; !ok {
		session.RenderNotFound()
		return
	}

	repo := session.Stash["repo"].(string)
	file := session.Stash["file"].(string)

	if repo == "SmartPAN" {
		if _, ok := filemap[file]; !ok {
			log.Debug("SmartPAN repo - file [%s] not found in any index", file)
			session.RenderNotFound()
			return
		}

		repo = filemap[file]
		log.Debug("SmartPAN repo - file [%s] found in [%s]", file, repo)
	}

	log.Debug("Repo [%s], file [%s]", repo, file)

	nfile := config.CacheDir + "/" + repo + "/" + file

	if _, err := os.Stat(nfile); err != nil {
		log.Debug("File not found on disk, considering readthrough")

		for fn, _ := range indexes {
			log.Debug("Trying file: %s", fn)
			if src, ok := indexes[fn][repo]; ok {
				log.Debug("Found matching repo")
				if strings.HasPrefix(src.URL, "http:") {
					log.Debug("Found HTTP URL, trying: %s", src.URL+"/"+file)

					res, err := nethttp.Get(src.URL + "/" + file)
					if err != nil {
						log.Debug("Error on readthrough: %s", err.Error())
						continue
					}
					defer res.Body.Close()
					b, err := ioutil.ReadAll(res.Body)
					if err != nil {
						log.Debug("Error reading body: %s", err.Error())
						continue
					}

					session.Response.Write(b)
					return
				}
			}
		}

		log.Debug("No readthrough available")
		session.RenderNotFound()
		return
	}

	f, err := os.Open(nfile)
	if err != nil {
		log.Error(err.Error())
		session.RenderNotFound()
		return
	}

	defer f.Close()

	b, err := ioutil.ReadAll(f)
	if err != nil {
		log.Error(err.Error())
		session.RenderNotFound()
		return
	}

	session.Response.Write(b)
}
开发者ID:companieshouse,项目名称:gopan,代码行数:80,代码来源:downloads.go


示例8: 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


示例9: 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


示例10: delete_file

func delete_file(session *http.Session) {
	session.Stash["Title"] = "Delete file"
	html, _ := session.RenderTemplate("delete.html")

	repo := session.Stash["repo"].(string)
	file := session.Stash["file"].(string)
	auth1 := session.Stash["auth1"].(string)
	auth2 := session.Stash["auth2"].(string)
	auth3 := session.Stash["auth3"].(string)

	fname := config.CacheDir + "/" + repo + "/" + auth1 + "/" + auth2 + "/" + auth3 + "/" + file

	if _, err := os.Stat(fname); err != nil {
		session.RenderNotFound()
		return
	}

	// Remove file from indexes
	for f, _ := range indexes {
		if _, ok := indexes[f][repo]; !ok {
			continue
		}
		if _, ok := indexes[f][repo].Authors[auth3]; !ok {
			continue
		}
		if _, ok := indexes[f][repo].Authors[auth3].Packages[file]; !ok {
			continue
		}
		log.Debug("Removing from index: %s", repo)

		pkg := indexes[f][repo].Authors[auth3].Packages[file]
		delete(indexes[f][repo].Authors[auth3].Packages, file)

		if len(indexes[f][repo].Authors[auth3].Packages) == 0 {
			log.Debug("Removing author")
			delete(indexes[f][repo].Authors, auth3)
		}

		if len(indexes[f][repo].Authors) == 0 {
			log.Debug("Removing index")
			delete(indexes[f], repo)
		}

		if auth, ok := mapped[repo][auth1][auth2][auth3]; ok {
			if len(auth.Packages) == 0 {
				log.Debug("Removing author from mapped index")
				delete(mapped[repo][auth1][auth2], auth3)
				delete(mapped[repo]["*"][auth2], auth3)
				delete(mapped[repo][auth1]["**"], auth3)
				delete(mapped[repo]["*"]["**"], auth3)
			}

			if len(mapped[repo][auth1][auth2]) == 0 {
				log.Debug("Removing auth1/auth2 from mapped index")
				delete(mapped[repo][auth1], auth2)
			}

			if len(mapped[repo]["*"][auth2]) == 0 {
				log.Debug("Removing author **/auth2 from mapped index")
				delete(mapped[repo][auth1], auth2)
			}
			if len(mapped[repo][auth1]["**"]) == 0 {
				log.Debug("Removing author auth1/** from mapped index")
				delete(mapped[repo][auth1], auth2)
			}
			if len(mapped[repo]["*"]["**"]) == 0 {
				log.Debug("Removing author */** from mapped index")
				delete(mapped[repo][auth1], auth2)
			}
			if len(mapped[repo]["*"]) == 0 {
				log.Debug("Removing author * from mapped index")
				delete(mapped[repo][auth1], auth2)
			}

			if len(mapped[repo][auth1]) == 1 {
				log.Debug("Removing author auth1 from mapped index")
				delete(mapped[repo], auth1)
			}

			if len(mapped[repo]) == 1 {
				log.Debug("Removing repo from mapped index")
				delete(mapped, repo)
			}
		}

		for _, prov := range pkg.Provides {
			parts := strings.Split(prov.Name, "::")
			// TODO remove from packages/idxpackages
			if ctx, ok := packages[parts[0]]; ok {
				parts = parts[1:]
				for len(parts) > 0 {
					if c, ok := ctx.Children[parts[0]]; ok {
						ctx = c
					} else {
						log.Debug("Package not found in packages: %s", parts)
						break
					}
					parts = parts[1:]
				}
				if len(parts) == 0 {
//.........这里部分代码省略.........
开发者ID:companieshouse,项目名称:gopan,代码行数:101,代码来源:delete.go


示例11: search

func search(session *http.Session) {
	if q, ok := session.Request.Form()["q"]; ok {
		query := strings.ToLower(q[0])
		session.Stash["Query"] = q[0]
		results := make([]*SearchResult, 0)
		var lock sync.Mutex

		tStart := time.Now().UnixNano()

		log.Trace("Searching for [%s]", query)

		var wg sync.WaitGroup

		for fname, _ := range indexes {
			log.Trace("=> Searching file: %s", fname)
			for _, idx := range indexes[fname] {
				log.Trace("=> Searching index: %s", idx.Name)
				wg.Add(1)
				go func(idx *gopan.Source) {
					defer wg.Done()

					if strings.Contains(strings.ToLower(idx.Name), query) {
						lock.Lock()
						results = append(results, &SearchResult{
							Name:  idx.Name,
							Type:  "Index",
							URL:   idx.Name,
							Obj:   idx,
							Glyph: "list",
						})
						lock.Unlock()
					}

					for _, auth := range idx.Authors {
						wg.Add(1)
						go func(idx *gopan.Source, auth *gopan.Author) {
							defer wg.Done()

							if strings.Contains(strings.ToLower(auth.Name), query) {
								lock.Lock()
								results = append(results, &SearchResult{
									Name:  auth.Name,
									Type:  "Author",
									URL:   idx.Name + "/authors/id/" + auth.Name[:1] + "/" + auth.Name[:2] + "/" + auth.Name,
									Obj:   auth,
									Glyph: "user",
								})
								lock.Unlock()
							}
							for _, pkg := range auth.Packages {
								if strings.Contains(strings.ToLower(pkg.Name), query) {
									lock.Lock()
									results = append(results, &SearchResult{
										Name:  pkg.Name,
										Type:  "Module",
										URL:   idx.Name + "/authors/id/" + auth.Name[:1] + "/" + auth.Name[:2] + "/" + auth.Name + "/" + pkg.Name,
										Obj:   pkg,
										Glyph: "compressed",
									})
									lock.Unlock()
								}
								for _, prov := range pkg.Provides {
									if strings.Contains(strings.ToLower(prov.Name), query) {
										lock.Lock()
										results = append(results, &SearchResult{
											Name:  prov.Name,
											Type:  "Package",
											URL:   idx.Name + "/modules/" + strings.Replace(prov.Name, "::", "/", -1),
											Obj:   prov,
											Glyph: "briefcase",
										})
										lock.Unlock()
									}
								}
							}
						}(idx, auth)
					}
				}(idx)
			}
		}

		wg.Wait()

		t := float64(time.Now().UnixNano()-tStart) / 100000 // ms

		sort.Sort(ByName(results))

		session.Stash["Results"] = results
		session.Stash["Duration"] = t
	}

	session.Stash["Title"] = "SmartPAN"
	html, _ := session.RenderTemplate("search.html")

	session.Stash["Page"] = "Search"
	session.Stash["Content"] = template.HTML(html)
	session.Render("layout.html")
}
开发者ID:companieshouse,项目名称:gopan,代码行数:98,代码来源:search.go


示例12: browse

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

	path := ""
	repo := ""
	itype := ""
	fpath := ""
	if r, ok := session.Stash["repo"]; ok {
		repo = r.(string)
		fpath += repo + "/"

		found := false
		if repo == "SmartPAN" {
			found = true
		} else {
			for fname, _ := range indexes {
				if _, ok := indexes[fname][repo]; ok {
					found = true
					break
				}
			}
		}

		if !found {
			session.RenderNotFound()
			return
		}
	}
	if i, ok := session.Stash["type"]; ok {
		itype = i.(string)
		fpath += itype + "/"
	}
	if p, ok := session.Stash["path"]; ok {
		path = p.(string)
		fpath += path + "/"
	}

	fpath = strings.TrimSuffix(fpath, "/")
	session.Stash["path"] = fpath

	bits := strings.Split(path, "/")
	fbits := strings.Split(fpath, "/")
	dirs := make(map[string]map[string]string, 0)
	files := make(map[string]map[string]string, 0)

	log.Info("Path: %s, Bits: %d", path, len(bits))

	if repo == "" {
		dirs = toplevelRepo()
	} else if itype == "" {
		dirs, files = tlRepo1(repo)
	} else {
		switch itype {
		case "authors":
			if len(path) == 0 {
				dirs, files = tlRepo2(repo, itype)
			} else {
				switch len(bits) {
				case 1:
					log.Info("tlAuthor1")
					dirs = tlAuthor1(repo)
				case 2:
					log.Info("tlAuthor2: %s", bits[1])
					dirs = tlAuthor2(repo, bits[1])
				case 3:
					log.Info("tlAuthor3: %s, %s", bits[1], bits[2])
					dirs = tlAuthor3(repo, bits[1], bits[2])
				case 4:
					log.Info("tlModuleList: %s, %s", repo, bits[1], bits[2], bits[3])
					files = tlModuleList(repo, bits[1], bits[2], bits[3])
				default:
					log.Info("Invalid path - rendering not found")
					session.RenderNotFound()
				}
			}
		case "modules":
			if path == "" {
				dirs, files = tlRepo2(repo, itype)
			} else {
				if repo == "SmartPAN" {
					rbits := bits[1:]
					ctx := packages[bits[0]]
					log.Info("Starting with context: %s", ctx.Namespace)
					for len(rbits) > 0 {
						ctx = ctx.Children[rbits[0]]
						rbits = rbits[1:]
					}
					log.Info("Stashing package context: %s", ctx.Namespace)
					session.Stash["Package"] = ctx
					for ns, _ := range ctx.Children {
						files[ns] = map[string]string{
							"Name": ns,
							"Path": "/" + repo + "/modules/" + path + "/" + ns,
						}
					}
				} else {
					rbits := bits[1:]
					ctx := idxpackages[repo][bits[0]]
					log.Info("Starting with context: %s", ctx.Namespace)
					for len(rbits) > 0 {
//.........这里部分代码省略.........
开发者ID:companieshouse,项目名称:gopan,代码行数:101,代码来源:browse.go



注:本文中的github.com/companieshouse/gotcha/http.Session类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang message.Msg类代码示例发布时间:2022-05-23
下一篇:
Golang client.StatsSummary类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap