Skip to content

Commit

Permalink
Settings added.
Browse files Browse the repository at this point in the history
  • Loading branch information
raul-brainattica committed Apr 16, 2015
1 parent 5761fcd commit 77b2a36
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 5 deletions.
8 changes: 3 additions & 5 deletions core/authentication/jwt_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package authentication
import (
"api.jwt.auth/core/redis"
"api.jwt.auth/services/models"
"api.jwt.auth/settings"
"code.google.com/p/go-uuid/uuid"
jwt "github.com/dgrijalva/jwt-go"
"golang.org/x/crypto/bcrypt"
"io/ioutil"
"path/filepath"
"time"
)

Expand Down Expand Up @@ -83,8 +83,7 @@ func (backend *JWTAuthenticationBackend) IsInBlacklist(token string) bool {
}

func getPrivateKey() []byte {
privateKeyPath, _ := filepath.Abs("./core/authentication/keys/private_key")
privateKey, err := ioutil.ReadFile(privateKeyPath)
privateKey, err := ioutil.ReadFile(settings.Get().PrivateKeyPath)
if err != nil {
panic(err)
}
Expand All @@ -93,8 +92,7 @@ func getPrivateKey() []byte {
}

func getPublicKey() []byte {
publicKeyPath, _ := filepath.Abs("./core/authentication/keys/public_key.pub")
publicKey, err := ioutil.ReadFile(publicKeyPath)
publicKey, err := ioutil.ReadFile(settings.Get().PublicKeyPath)
if err != nil {
panic(err)
}
Expand Down
2 changes: 2 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package main

import (
"api.jwt.auth/routers"
"api.jwt.auth/settings"
"github.com/codegangsta/negroni"
"net/http"
)

func main() {
settings.Init()
router := routers.InitRoutes()
n := negroni.Classic()
n.UseHandler(router)
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions settings/pre.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"PrivateKeyPath": "/home/vagrant/go/src/api.jwt.auth/settings/keys/private_key",
"PublicKeyPath": "/home/vagrant/go/src/api.jwt.auth/settings/keys/public_key.pub"
}
4 changes: 4 additions & 0 deletions settings/prod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"PrivateKeyPath": "/home/vagrant/go/src/api.jwt.auth/settings/keys/private_key",
"PublicKeyPath": "/home/vagrant/go/src/api.jwt.auth/settings/keys/public_key.pub"
}
58 changes: 58 additions & 0 deletions settings/settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package settings

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)

var environments = map[string]string{
"production": "settings/prod.json",
"preproduction": "settings/pre.json",
"tests": "../../settings/tests.json",
}

type Settings struct {
PrivateKeyPath string
PublicKeyPath string
}

var settings Settings = Settings{}
var env = "preproduction"

func Init() {
env = os.Getenv("GO_ENV")
if env == "" {
fmt.Println("Warning: Setting preproduction environment due to lack of GO_ENV value")
env = "preproduction"
}
LoadSettingsByEnv(env)
}

func LoadSettingsByEnv(env string) {
content, err := ioutil.ReadFile(environments[env])
if err != nil {
fmt.Println("Error while reading config file", err)
}
settings = Settings{}
jsonErr := json.Unmarshal(content, &settings)
if jsonErr != nil {
fmt.Println("Error while parsing config file", jsonErr)
}
}

func GetEnvironment() string {
return env
}

func Get() Settings {
if &settings == nil {
Init()
}
return settings
}

func IsTestEnvironment() bool {
return env == "tests"
}
4 changes: 4 additions & 0 deletions settings/tests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"PrivateKeyPath": "/home/vagrant/go/src/api.jwt.auth/settings/keys/private_key",
"PublicKeyPath": "/home/vagrant/go/src/api.jwt.auth/settings/keys/public_key.pub"
}

0 comments on commit 77b2a36

Please sign in to comment.