File tree Expand file tree Collapse file tree 8 files changed +75
-5
lines changed Expand file tree Collapse file tree 8 files changed +75
-5
lines changed Original file line number Diff line number Diff line change @@ -3,11 +3,11 @@ package authentication
3
3
import (
4
4
"api.jwt.auth/core/redis"
5
5
"api.jwt.auth/services/models"
6
+ "api.jwt.auth/settings"
6
7
"code.google.com/p/go-uuid/uuid"
7
8
jwt "github.com/dgrijalva/jwt-go"
8
9
"golang.org/x/crypto/bcrypt"
9
10
"io/ioutil"
10
- "path/filepath"
11
11
"time"
12
12
)
13
13
@@ -83,8 +83,7 @@ func (backend *JWTAuthenticationBackend) IsInBlacklist(token string) bool {
83
83
}
84
84
85
85
func getPrivateKey () []byte {
86
- privateKeyPath , _ := filepath .Abs ("./core/authentication/keys/private_key" )
87
- privateKey , err := ioutil .ReadFile (privateKeyPath )
86
+ privateKey , err := ioutil .ReadFile (settings .Get ().PrivateKeyPath )
88
87
if err != nil {
89
88
panic (err )
90
89
}
@@ -93,8 +92,7 @@ func getPrivateKey() []byte {
93
92
}
94
93
95
94
func getPublicKey () []byte {
96
- publicKeyPath , _ := filepath .Abs ("./core/authentication/keys/public_key.pub" )
97
- publicKey , err := ioutil .ReadFile (publicKeyPath )
95
+ publicKey , err := ioutil .ReadFile (settings .Get ().PublicKeyPath )
98
96
if err != nil {
99
97
panic (err )
100
98
}
Original file line number Diff line number Diff line change @@ -2,11 +2,13 @@ package main
2
2
3
3
import (
4
4
"api.jwt.auth/routers"
5
+ "api.jwt.auth/settings"
5
6
"github.com/codegangsta/negroni"
6
7
"net/http"
7
8
)
8
9
9
10
func main () {
11
+ settings .Init ()
10
12
router := routers .InitRoutes ()
11
13
n := negroni .Classic ()
12
14
n .UseHandler (router )
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
1
+ {
2
+ "PrivateKeyPath" : " /home/vagrant/go/src/api.jwt.auth/settings/keys/private_key" ,
3
+ "PublicKeyPath" : " /home/vagrant/go/src/api.jwt.auth/settings/keys/public_key.pub"
4
+ }
Original file line number Diff line number Diff line change
1
+ {
2
+ "PrivateKeyPath" : " /home/vagrant/go/src/api.jwt.auth/settings/keys/private_key" ,
3
+ "PublicKeyPath" : " /home/vagrant/go/src/api.jwt.auth/settings/keys/public_key.pub"
4
+ }
Original file line number Diff line number Diff line change
1
+ package settings
2
+
3
+ import (
4
+ "encoding/json"
5
+ "fmt"
6
+ "io/ioutil"
7
+ "os"
8
+ )
9
+
10
+ var environments = map [string ]string {
11
+ "production" : "settings/prod.json" ,
12
+ "preproduction" : "settings/pre.json" ,
13
+ "tests" : "../../settings/tests.json" ,
14
+ }
15
+
16
+ type Settings struct {
17
+ PrivateKeyPath string
18
+ PublicKeyPath string
19
+ }
20
+
21
+ var settings Settings = Settings {}
22
+ var env = "preproduction"
23
+
24
+ func Init () {
25
+ env = os .Getenv ("GO_ENV" )
26
+ if env == "" {
27
+ fmt .Println ("Warning: Setting preproduction environment due to lack of GO_ENV value" )
28
+ env = "preproduction"
29
+ }
30
+ LoadSettingsByEnv (env )
31
+ }
32
+
33
+ func LoadSettingsByEnv (env string ) {
34
+ content , err := ioutil .ReadFile (environments [env ])
35
+ if err != nil {
36
+ fmt .Println ("Error while reading config file" , err )
37
+ }
38
+ settings = Settings {}
39
+ jsonErr := json .Unmarshal (content , & settings )
40
+ if jsonErr != nil {
41
+ fmt .Println ("Error while parsing config file" , jsonErr )
42
+ }
43
+ }
44
+
45
+ func GetEnvironment () string {
46
+ return env
47
+ }
48
+
49
+ func Get () Settings {
50
+ if & settings == nil {
51
+ Init ()
52
+ }
53
+ return settings
54
+ }
55
+
56
+ func IsTestEnvironment () bool {
57
+ return env == "tests"
58
+ }
Original file line number Diff line number Diff line change
1
+ {
2
+ "PrivateKeyPath" : " /home/vagrant/go/src/api.jwt.auth/settings/keys/private_key" ,
3
+ "PublicKeyPath" : " /home/vagrant/go/src/api.jwt.auth/settings/keys/public_key.pub"
4
+ }
You can’t perform that action at this time.
0 commit comments