-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
152 lines (129 loc) · 3.76 KB
/
utils.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"compress/gzip"
"encoding/json"
"errors"
"io"
"io/ioutil"
"log"
"math"
"net/http"
"os"
"strings"
"github.com/gorilla/sessions"
"github.com/markbates/goth"
"github.com/markbates/goth/gothic"
"github.com/markbates/goth/providers/facebook"
"github.com/markbates/goth/providers/twitter"
"github.com/nicksnyder/go-i18n/i18n"
_ "github.com/joho/godotenv/autoload"
)
// loadSession loads the session storage and initializes the auth
// providers
func loadSession() {
store := sessions.NewFilesystemStore(os.TempDir(), []byte(os.Getenv("SESSION_SECRET")))
store.MaxLength(math.MaxInt64)
gothic.Store = store
host := getHost()
goth.UseProviders(
facebook.New(os.Getenv("FACEBOOK_KEY"), os.Getenv("FACEBOOK_SECRET"), host + "/auth/callback?provider=facebook"),
twitter.New(os.Getenv("TWITTER_KEY"), os.Getenv("TWITTER_SECRET"), host + "/auth/callback?provider=twitter"),
)
}
// loadLocales loads all i18n strings from the `locales` directory
func loadLocales() {
files, err := ioutil.ReadDir("locales")
if err != nil {
log.Printf("Error: %v", err)
return
}
for _, file := range files {
i18n.MustLoadTranslationFile("locales/" + file.Name())
}
}
// initT returns a new i18n TranslateFunc based on the "Accept-Language"
// header and defaulting to "en"
func initT(acceptLang string, defaultLang string) (T i18n.TranslateFunc) {
T = i18n.MustTfunc(acceptLang, defaultLang)
return
}
// getPort returns the port by first looking at any environment variable
// nammed PORT and then defaulting to :8000
func getPort() string {
if port := os.Getenv("PORT"); port != "" {
return ":" + port
}
return ":8000"
}
// getHost returns the host URL by looking if the app is running in "dev" or
// in production (on Heroku for now)
func getHost() string {
if env := os.Getenv("ENV"); env == "dev" {
return "http://localhost:8000"
}
return os.Getenv("HOST")
}
// getUser returns the goth.User linked with the current session
// NB: for now we are only using Facebook as an OAuth provider
func getUser(r *http.Request, p string) (goth.User, error) {
session, _ := gothic.Store.Get(r, p + gothic.SessionName)
values := session.Values[p]
if values == nil {
return goth.User{}, errors.New("cannot find session values")
}
provider, _ := goth.GetProvider(p)
sess, _ := provider.UnmarshalSession(values.(string))
user, err := provider.FetchUser(sess)
if err != nil {
return goth.User{}, err
}
// Namespace the user ID
switch (p) {
case "facebook":
user.UserID = "fb-" + user.UserID
case "twitter":
user.UserID = "tw-" + user.UserID
default:
}
return user, nil
}
// GZip enconding based on https://gist.github.com/the42/1956518
type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}
func (w gzipResponseWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}
func gzipHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
h.ServeHTTP(w, r)
return
}
w.Header().Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(w)
defer gz.Close()
gzr := gzipResponseWriter{Writer: gz, ResponseWriter: w}
h.ServeHTTP(gzr, r)
})
}
func cacheHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "max-age=2332800") // 27 days
h.ServeHTTP(w, r)
})
}
// asset serves static assets using hashmark (so CSS and JS files for now!)
func asset(asset string) string {
var manifest map[string]interface{}
file, err := ioutil.ReadFile("assets.json")
if err != nil {
log.Printf("Error: %v", err)
}
err = json.Unmarshal(file, &manifest)
if err != nil {
log.Printf("Error: %v", err)
}
return manifest[asset].(string)
}