本文整理匯總了Golang中http.Redirect函數的典型用法代碼示例。如果您正苦於以下問題:Golang Redirect函數的具體用法?Golang Redirect怎麽用?Golang Redirect使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Redirect函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: register
func register(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
g := Member{
Usern: r.FormValue("usern"),
Name: r.FormValue("name"),
Passwd: r.FormValue("passwd"),
Repasswd: r.FormValue("repasswd"),
Phone: r.FormValue("phone"),
Email: r.FormValue("email"),
Study: r.FormValue("study"),
Address: r.FormValue("address"),
Date: datastore.SecondsToTime(time.Seconds()),
}
if g.Passwd == g.Repasswd && g.Usern != "" && g.Name != "" && g.Phone != "" && g.Email != "" {
_, err := datastore.Put(c, datastore.NewIncompleteKey("Member"), &g)
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
return
}
} else {
http.Redirect(w, r, "/signin", http.StatusFound)
}
http.Redirect(w, r, "/view", http.StatusFound)
}
示例2: loginHandler
func loginHandler(w http.ResponseWriter, r *http.Request) {
// Determine the virtual host
server, err := findServer(r.Host)
if err != nil {
errorHandler(w, r, err.String())
return
}
username := r.FormValue("username")
password := r.FormValue("password")
log.Println("LOGIN:", username)
if err = server.UserAccountDatabase.CheckCredentials(username, password); err != nil {
log.Println(err)
http.Redirect(w, r, server.Config.LoginPage, 303)
return
}
s, err := server.SessionDatabase.CreateSession(username)
if err != nil {
log.Println(err)
http.Redirect(w, r, server.Config.LoginPage, 303)
return
}
s.SetCookie(w)
http.Redirect(w, r, server.Config.MainPage, 303)
}
示例3: signupHandler
func signupHandler(w http.ResponseWriter, r *http.Request) {
// Determine the virtual host
server, err := findServer(r.Host)
if err != nil {
errorHandler(w, r, err.String())
return
}
username := r.FormValue("username")
password := r.FormValue("password")
email := r.FormValue("email")
nickname := r.FormValue("nickname")
log.Println("Register:", username)
user, err := server.UserAccountDatabase.SignUpUser(email, nickname, username, password)
if err != nil {
log.Println(err)
http.Redirect(w, r, server.Config.SignupPage, 303)
return
}
s, err := server.SessionDatabase.CreateSession(user.Username)
if err != nil {
log.Println(err)
http.Redirect(w, r, server.Config.LoginPage, 303)
return
}
s.SetCookie(w)
http.Redirect(w, r, server.Config.MainPage, 303)
}
示例4: c1LoginHandler
func c1LoginHandler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
if l, err := c1IsLoggedIn(c); err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
return
} else if l {
/// http.Redirect(w, r, "/", http.StatusFound)
http.Redirect(w, r, "/index", http.StatusFound)
return
}
if r.Method == "GET" {
w.Write([]byte(
"<html><body><form action='/c1Login' method='POST'>" +
"<input type='text' name='usr'></input>" +
"<input type='password' name='pwd'></input>" +
"<input type='submit' value='Submit'></input>" +
"</form></body></html>"))
return
} else if r.Method == "POST" {
usr := r.FormValue("usr")
pwd := r.FormValue("pwd")
_, err := C1Login(c, usr, pwd)
if err == C1AuthError {
http.Error(w, err.String(), http.StatusUnauthorized)
return
}
http.Redirect(w, r, "/index", http.StatusFound)
/// http.Redirect(w, r, "/", http.StatusFound)
}
}
示例5: serveFile
func serveFile(w http.ResponseWriter, r *http.Request) {
relpath := r.URL.Path[1:] // serveFile URL paths start with '/'
abspath := absolutePath(relpath, *goroot)
// pick off special cases and hand the rest to the standard file server
switch r.URL.Path {
case "/":
serveHTMLDoc(w, r, filepath.Join(*goroot, "doc", "root.html"), "doc/root.html")
return
case "/doc/root.html":
// hide landing page from its real name
http.Redirect(w, r, "/", http.StatusMovedPermanently)
return
}
switch path.Ext(relpath) {
case ".html":
if strings.HasSuffix(relpath, "/index.html") {
// We'll show index.html for the directory.
// Use the dir/ version as canonical instead of dir/index.html.
http.Redirect(w, r, r.URL.Path[0:len(r.URL.Path)-len("index.html")], http.StatusMovedPermanently)
return
}
serveHTMLDoc(w, r, abspath, relpath)
return
case ".go":
serveTextFile(w, r, abspath, relpath, "Source file")
return
}
dir, err := fs.Lstat(abspath)
if err != nil {
log.Print(err)
serveError(w, r, relpath, err)
return
}
if dir != nil && dir.IsDirectory() {
if redirect(w, r) {
return
}
if index := filepath.Join(abspath, "index.html"); isTextFile(index) {
serveHTMLDoc(w, r, index, relativeURL(index))
return
}
serveDirectory(w, r, abspath, relpath)
return
}
if isTextFile(abspath) {
serveTextFile(w, r, abspath, relpath, "Text file")
return
}
fileServer.ServeHTTP(w, r)
}
示例6: searchHandler
func searchHandler(w http.ResponseWriter, r *http.Request) {
context := &Search{}
// prepare the template
t, err := template.ParseFile("template/search.html")
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
}
context.T = t
ae := appengine.NewContext(r)
query := r.FormValue("q")
queryLength := len([]int(query)) // unicode length
// max 8 letters
if queryLength > 8 {
query = query[0:8]
http.Redirect(w, r, fmt.Sprintf("search?q=%s", query), 302)
return
}
// lowercase only
if query != strings.ToLower(query) {
http.Redirect(w, r, fmt.Sprintf("search?q=%s", strings.ToLower(query)), 302)
return
}
context.Q = query
hashTable := make(map[string]byte, 0)
if 0 != queryLength {
if queryLength > 8 {
query = query[0:8]
}
// make sure enable is loaded
var e *word.Enable
var err os.Error
if e, err = loadEnable(); err != nil {
ae.Errorf("%v", err)
}
channel := word.StringPermutations(query)
for p := range channel {
if valid := e.WordIsValid(p); !valid {
continue
}
if _, inHash := hashTable[p]; !inHash {
context.Permutations = append(context.Permutations, p)
hashTable[p] = 1
}
}
}
// display template
if err := context.T.Execute(w, context); err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
}
}
示例7: SaveHandler
func SaveHandler(c http.ResponseWriter, r *http.Request) {
fmt.Println(r)
title, id, origID, dateString, desc, body, fileName, importance, delete := HandleNess(c, r)
date, e := time.Parse(timeFormat, string(dateString))
event := new(Event)
// The ID changed, remove the old one.
if !bytes.Equal(id, origID) && len(origID) > 0 {
fmt.Println("ID Changed!")
origEvent := new(Event)
origEvent.ID = string(origID)
origEvent.Load()
// Copy the info to the new event
*event = *origEvent
origEvent.Delete()
}
event.ID = string(id)
event.Load()
// TODO: Delete the uploaded image as well!
if delete {
event.Delete()
http.Redirect(c, r, "/events", 301)
return
}
event.Importance = importance
event.Title = string(title)
event.Desc = string(desc)
event.Link = "/events/" + string(id)
event.Time = (*Time)(date)
if len(fileName) > 0 {
event.Img = "/img/events/data/" + fileName
}
e = pages.SavePage("events/"+string(id), body, title)
event.Save()
fmt.Println(title, id, dateString, date, e, desc, body)
fmt.Println(r.FormValue("img"))
http.Redirect(c, r, "/events/"+event.ID, 301)
}
示例8: handleUpload
func handleUpload(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
blobs, _, err := blobstore.ParseUpload(r)
if err != nil {
serveError(c, w, err)
return
}
file := blobs["file"]
if len(file) == 0 {
c.Errorf("no file uploaded")
http.Redirect(w, r, "/", http.StatusFound)
return
}
http.Redirect(w, r, "/serve/?blobKey="+string(file[0].BlobKey), http.StatusFound)
}
示例9: accept_event
func accept_event(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
event, key, err_string := find_single_event(c, r.FormValue("sender"),
r.FormValue("date"))
if len(err_string) > 0 {
fmt.Fprintf(w, err_string)
return
}
if event.OwnerDate > 0 {
fmt.Fprintf(w, "This message is already owned by %s.", event.Owner)
return
}
event.OwnerDate = datastore.SecondsToTime(time.Seconds())
event.Owner = "Someone"
_, err := datastore.Put(c, key, &event)
if err != nil {
fmt.Fprintf(w, err.String())
return
} else {
target_url := fmt.Sprintf("/show?sender=%s&date=%d", event.Sender,
event.RecieptDate)
http.Redirect(w, r, target_url, http.StatusFound)
}
}
示例10: fileHandler
func fileHandler(w http.ResponseWriter, r *http.Request) {
// Determine the virtual host
server, err := findServer(r.Host)
if err != nil {
errorHandler(w, r, err.String())
return
}
// log.Println("URI is ", "/_static" + r.URL.Path)
// Parse the URI, prepend "/_static"
uri, ok := lightwave.NewURI("/_static" + r.URL.Path)
if !ok {
errorHandler(w, r, "Error parsing URI")
return
}
// GET handler
if r.Method != "GET" {
errorHandler(w, r, "Unsupported HTTP method")
}
// Requests for the application page are redirected to the login page
if r.URL.Path == server.Config.MainPage {
// Find the session
_, err := server.SessionDatabase.FindSession(r)
if err != nil {
http.Redirect(w, r, server.Config.LoginPage, 303)
return
}
}
ch := make(chan bool)
req := &lightwave.GetRequest{lightwave.Request{w, r.URL.RawQuery, ch, lightwave.ClientOrigin, uri, nil}}
server.Get(req)
<-ch
}
示例11: redirect
func redirect(w http.ResponseWriter, r *http.Request) (redirected bool) {
if canonical := path.Clean(r.URL.Path) + "/"; r.URL.Path != canonical {
http.Redirect(w, r, canonical, http.StatusMovedPermanently)
redirected = true
}
return
}
示例12: handleLogout
func handleLogout(w http.ResponseWriter, r *http.Request) {
c = appengine.NewContext(r)
returnURL := "/"
// parse form
err := r.ParseForm()
if err != nil {
serveError(c, w, err)
return
}
if r.FormValue("continue") != "" {
returnURL = r.FormValue("continue")
}
if useOpenID {
// adjust returnURL to bring us back to a local user login form
laterReturnUrl := returnURL
returnURL = "/Login/?chooseLogin=1&continue=" + http.URLEscape(laterReturnUrl)
}
// redirect to google logout (for OpenID as well, or else we won't be locally logged out)
lourl, err := user.LogoutURL(c, returnURL)
if err != nil {
c.Errorf("handleLogout: error getting LogoutURL")
}
c.Debugf("handleLogout: redirecting to logoutURL=%v", lourl)
http.Redirect(w, r, lourl, http.StatusFound)
return
}
示例13: addWidget
func addWidget(w http.ResponseWriter, r *http.Request) {
var err os.Error
if fixup == nil {
fixup, err = regexp.Compile(`[^A-Za-z0-9\-_. ]`)
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
return
}
}
ctx := appengine.NewContext(r)
name := fixup.ReplaceAllString(r.FormValue("name"), "")
if len(name) == 0 {
http.Error(w, "Invalid/no name provided", http.StatusInternalServerError)
return
}
widget := NewWidget(ctx, name)
err = widget.Commit()
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/widget/list", http.StatusFound)
}
示例14: saveHandler
func saveHandler(w http.ResponseWriter, r *http.Request) {
title := r.URL.Path[lenPath:]
body := r.FormValue("body")
p := &Page{Title: title, Body: []byte(body)}
p.save()
http.Redirect(w, r, "/view/"+title, http.StatusFound)
}
示例15: handleTest
//This is what sends the Post data to the primarry "run" function
func handleTest(writer http.ResponseWriter, request *http.Request) {
BoardSize, _ := strconv.Atoi(request.FormValue("BoardSize"))
NumberThreads, _ := strconv.Atoi(request.FormValue("NumberThreads"))
NumberIterations, _ := strconv.Atoi(request.FormValue("NumberIterations"))
run(writer, BoardSize, NumberThreads, NumberIterations)
http.Redirect(writer, request, "/", http.StatusFound)
}