Browse Source

Refactored redirect code

Wirecog 7 years ago
parent
commit
694129ea91
2 changed files with 93 additions and 8 deletions
  1. 86
    0
      opdetails.go
  2. 7
    8
      redirect.go

+ 86
- 0
opdetails.go View File

1
+// The Isomorphic Go Project
2
+// Copyright (c) Wirecog, LLC. All rights reserved.
3
+// Use of this source code is governed by a BSD-style
4
+// license, which can be found in the LICENSE file.
5
+
6
+package isokit
7
+
8
+import "github.com/gopherjs/gopherjs/js"
9
+
10
+type OperatingDetails struct {
11
+	Environment int
12
+	Runtime     int
13
+}
14
+
15
+const (
16
+	ServerEnvironment = iota
17
+	WebBrowserEnvironment
18
+)
19
+
20
+const (
21
+	GoRuntime = iota
22
+	JSRuntime
23
+)
24
+
25
+var (
26
+	operatingEnvironment int
27
+	operatingRuntime     int
28
+)
29
+
30
+func isJSRuntime() bool {
31
+	return js.Global != nil
32
+}
33
+
34
+func isGoRuntime() bool {
35
+	return !isJSRuntime()
36
+}
37
+
38
+func isWebBrowserEnvironment() bool {
39
+	return isJSRuntime() && js.Global.Get("document") != js.Undefined
40
+}
41
+
42
+func isServerEnvironment() bool {
43
+
44
+	if isGoRuntime() == true {
45
+		return true
46
+	} else if isJSRuntime() == true {
47
+		return !isWebBrowserEnvironment()
48
+	} else {
49
+		return true
50
+	}
51
+
52
+}
53
+
54
+func OperatingEnvironment() int {
55
+	return operatingEnvironment
56
+}
57
+
58
+func OperatingRuntime() int {
59
+	return operatingRuntime
60
+}
61
+
62
+func initializeOperatingDetails() {
63
+
64
+	if isJSRuntime() == true {
65
+		operatingRuntime = WebBrowserEnvironment
66
+	}
67
+
68
+	if isGoRuntime() == true {
69
+		operatingRuntime = GoRuntime
70
+	}
71
+
72
+	if isServerEnvironment() == true {
73
+		operatingEnvironment = ServerEnvironment
74
+	}
75
+
76
+	if isWebBrowserEnvironment() == true {
77
+		operatingEnvironment = WebBrowserEnvironment
78
+	}
79
+
80
+}
81
+
82
+func init() {
83
+
84
+	initializeOperatingDetails()
85
+
86
+}

+ 7
- 8
redirect.go View File

8
 import (
8
 import (
9
 	"net/http"
9
 	"net/http"
10
 
10
 
11
-	"github.com/go-humble/detect"
12
 	"github.com/gopherjs/gopherjs/js"
11
 	"github.com/gopherjs/gopherjs/js"
13
 )
12
 )
14
 
13
 
15
 // ServerRedirect performs a redirect when operating on the server-side.
14
 // ServerRedirect performs a redirect when operating on the server-side.
16
 func ServerRedirect(w http.ResponseWriter, r *http.Request, destinationURL string) {
15
 func ServerRedirect(w http.ResponseWriter, r *http.Request, destinationURL string) {
17
-	http.Redirect(w, r, destinationURL, 301)
16
+	http.Redirect(w, r, destinationURL, 302)
18
 }
17
 }
19
 
18
 
20
 // ClientRedirect performs a redirect when operating on the client-side.
19
 // ClientRedirect performs a redirect when operating on the client-side.
30
 	var w http.ResponseWriter
29
 	var w http.ResponseWriter
31
 	var r *http.Request
30
 	var r *http.Request
32
 
31
 
33
-	if detect.IsServer() && len(items) != 3 {
32
+	if OperatingEnvironment() == ServerEnvironment && len(items) != 3 {
34
 		return
33
 		return
35
 	}
34
 	}
36
 
35
 
37
-	if detect.IsBrowser() && len(items) != 1 {
36
+	if OperatingEnvironment() == WebBrowserEnvironment && len(items) != 1 {
38
 		return
37
 		return
39
 	}
38
 	}
40
 
39
 
51
 
50
 
52
 	}
51
 	}
53
 
52
 
54
-	if detect.IsServer() && (w == nil || r == nil) {
53
+	if OperatingEnvironment() == ServerEnvironment && (w == nil || r == nil) {
55
 		return
54
 		return
56
 	}
55
 	}
57
 
56
 
59
 		return
58
 		return
60
 	}
59
 	}
61
 
60
 
62
-	switch {
63
-	case detect.IsBrowser():
61
+	switch OperatingEnvironment() {
62
+	case WebBrowserEnvironment:
64
 		ClientRedirect(url)
63
 		ClientRedirect(url)
65
 
64
 
66
-	case detect.IsServer():
65
+	case ServerEnvironment:
67
 		ServerRedirect(w, r, url)
66
 		ServerRedirect(w, r, url)
68
 	}
67
 	}
69
 }
68
 }