Skip to content

Commit 77b2a36

Browse files
author
raul-brainattica
committed
Settings added.
1 parent 5761fcd commit 77b2a36

File tree

8 files changed

+75
-5
lines changed

8 files changed

+75
-5
lines changed

core/authentication/jwt_backend.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package authentication
33
import (
44
"api.jwt.auth/core/redis"
55
"api.jwt.auth/services/models"
6+
"api.jwt.auth/settings"
67
"code.google.com/p/go-uuid/uuid"
78
jwt "github.com/dgrijalva/jwt-go"
89
"golang.org/x/crypto/bcrypt"
910
"io/ioutil"
10-
"path/filepath"
1111
"time"
1212
)
1313

@@ -83,8 +83,7 @@ func (backend *JWTAuthenticationBackend) IsInBlacklist(token string) bool {
8383
}
8484

8585
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)
8887
if err != nil {
8988
panic(err)
9089
}
@@ -93,8 +92,7 @@ func getPrivateKey() []byte {
9392
}
9493

9594
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)
9896
if err != nil {
9997
panic(err)
10098
}

server.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package main
22

33
import (
44
"api.jwt.auth/routers"
5+
"api.jwt.auth/settings"
56
"github.com/codegangsta/negroni"
67
"net/http"
78
)
89

910
func main() {
11+
settings.Init()
1012
router := routers.InitRoutes()
1113
n := negroni.Classic()
1214
n.UseHandler(router)
File renamed without changes.

settings/pre.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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+
}

settings/prod.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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+
}

settings/settings.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
}

settings/tests.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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+
}

0 commit comments

Comments
 (0)