Browse Source

Initial import.

Wirecog 7 years ago
commit
bcd9bfdd78
5 changed files with 133 additions and 0 deletions
  1. 6
    0
      .gitignore
  2. 29
    0
      LICENSE
  3. 21
    0
      Readme.md
  4. 8
    0
      isokit.go
  5. 69
    0
      redirect.go

+ 6
- 0
.gitignore View File

1
+*~
2
+*.swp
3
+*.swo
4
+*.tmp
5
+*.out
6
+.DS_Store

+ 29
- 0
LICENSE View File

1
+The Isomorphic Go Project
2
+Copyright (c) Wirecog, LLC. All rights reserved.
3
+
4
+Redistribution and use in source and binary forms, with or without
5
+modification, are permitted provided that the following conditions are
6
+met:
7
+
8
+   * Redistributions of source code must retain the above copyright
9
+notice, this list of conditions and the following disclaimer.
10
+   * Redistributions in binary form must reproduce the above
11
+copyright notice, this list of conditions and the following disclaimer
12
+in the documentation and/or other materials provided with the
13
+distribution.
14
+   * Neither the name of Wirecog, LLC. nor the names of its contributors
15
+may be used to endorse or promote products derived from
16
+this software without specific prior written permission.
17
+
18
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+

+ 21
- 0
Readme.md View File

1
+<p align="center"><a href="http://isomorphicgo.org" target="_blank"><img src="https://github.com/isomorphicgo/isogoapp/blob/master/static/images/isomorphic_go_logo.png"></a></p>
2
+
3
+# IsoKit
4
+
5
+[![Go Report Card](https://goreportcard.com/badge/github.com/isomorphicgo/isokit)](https://goreportcard.com/report/github.com/isomorphicgo/isokit)
6
+
7
+A package providing common isomorphic functionality. 
8
+
9
+## Installation
10
+
11
+IsoKit requires GopherJS and Humble Detect.
12
+
13
+### Get IsoKit
14
+`go get -u github.com/isomorphicgo/isokit`
15
+
16
+
17
+## The Isomorphic Go Project
18
+More information on the benefits of Isomorphic Go applications can be found at the [Isomorphic Go Website](http://isomorphicgo.org).
19
+
20
+## License
21
+IsoKit is licensed under the BSD License. Read the [LICENSE](https://github.com/isomorphicgo/isokit/blob/master/LICENSE) file for more information.

+ 8
- 0
isokit.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 provides common isomorphic functionality intended to be used
7
+// in an Isomorphic Go web application.
8
+package isokit

+ 69
- 0
redirect.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 (
9
+	"net/http"
10
+
11
+	"github.com/go-humble/detect"
12
+	"github.com/gopherjs/gopherjs/js"
13
+)
14
+
15
+// ServerRedirect performs a redirect when operating on the server-side.
16
+func ServerRedirect(w http.ResponseWriter, r *http.Request, destinationURL string) {
17
+	http.Redirect(w, r, destinationURL, 301)
18
+}
19
+
20
+// ClientRedirect performs a redirect when operating on the client-side.
21
+func ClientRedirect(destinationURL string) {
22
+	js := js.Global
23
+	js.Get("location").Set("href", destinationURL)
24
+}
25
+
26
+// Redirect performs a redirect operation based on the environment that
27
+// the program is operating under.
28
+func Redirect(items ...interface{}) {
29
+	var url string
30
+	var w http.ResponseWriter
31
+	var r *http.Request
32
+
33
+	if detect.IsServer() && len(items) != 3 {
34
+		return
35
+	}
36
+
37
+	if detect.IsBrowser() && len(items) != 1 {
38
+		return
39
+	}
40
+
41
+	for _, item := range items {
42
+
43
+		switch t := item.(type) {
44
+		case http.ResponseWriter:
45
+			w = t
46
+		case *http.Request:
47
+			r = t
48
+		case string:
49
+			url = t
50
+		}
51
+
52
+	}
53
+
54
+	if detect.IsServer() && (w == nil || r == nil) {
55
+		return
56
+	}
57
+
58
+	if url == "" {
59
+		return
60
+	}
61
+
62
+	switch {
63
+	case detect.IsBrowser():
64
+		ClientRedirect(url)
65
+
66
+	case detect.IsServer():
67
+		ServerRedirect(w, r, url)
68
+	}
69
+}