/
redirect.go
53 lines (40 loc) · 1.29 KB
/
redirect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// The Isomorphic Go Project
// Copyright (c) Wirecog, LLC. All rights reserved.
// Use of this source code is governed by a BSD-style
// license, which can be found in the LICENSE file.
// +build !clientonly
package isokit
import (
"errors"
"net/http"
"github.com/gopherjs/gopherjs/js"
)
// ServerRedirect performs a redirect when operating on the server-side.
func ServerRedirect(w http.ResponseWriter, r *http.Request, destinationURL string) {
http.Redirect(w, r, destinationURL, 302)
}
// ClientRedirect performs a redirect when operating on the client-side.
func ClientRedirect(destinationURL string) {
js := js.Global
js.Get("location").Set("href", destinationURL)
}
type RedirectParams struct {
ResponseWriter http.ResponseWriter
Request *http.Request
URL string
}
func Redirect(params *RedirectParams) error {
if params.URL == "" {
return errors.New("The URL must be specified!")
}
if OperatingEnvironment() == ServerEnvironment && (params.ResponseWriter == nil || params.Request == nil) {
return errors.New("Either the response writer and/or the request is nil!")
}
switch OperatingEnvironment() {
case WebBrowserEnvironment:
ClientRedirect(params.URL)
case ServerEnvironment:
ServerRedirect(params.ResponseWriter, params.Request, params.URL)
}
return nil
}