Skip to content

Commit

Permalink
Add domain entry in configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-bouvier committed Nov 6, 2019
1 parent 3b4f69e commit 245c127
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 36 deletions.
2 changes: 1 addition & 1 deletion auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const (
)

// InitJWT reads the key files before starting http handlers
func InitJWT(config Config) error {
func InitJWT() error {
signBytes, err := ioutil.ReadFile(config.PrivateKeyPath)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions cmd/insapp-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ type withCORS struct {
}

func main() {
config, _ := insapp.InitConfig()
config := insapp.InitConfig()

log.Println("Starting server on 0.0.0.0:" + config.Port)
log.Fatal(http.ListenAndServe(":"+config.Port, &withCORS{insapp.NewRouter(config)}))
log.Fatal(http.ListenAndServe(":"+config.Port, &withCORS{insapp.NewRouter()}))
}

// Simple wrapper to Allow CORS
Expand Down
3 changes: 2 additions & 1 deletion config.json.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"domain":"REPLACE_WITH_YOUR_DOMAIN",
"env":"REPLACE_WITH_THE_ENVIRONMENT_TYPE",
"google_email":"REPLACE_WITH_YOUR_GOOGLE_EMAIL",
"google_password":"REPLACE_WITH_YOUR_GOOGLE_PASSWORD",
"mongo_database_name":"insapp",
Expand All @@ -7,6 +9,5 @@
"mongo_database_password":"REPLACE_WITH_THE_MONGO_PASSWORD",
"private_key_path":"REPLACE_WITH_THE_PRIVATE_KEY_PATH",
"public_key_path":"REPLACE_WITH_THE_PUBLIC_KEY_PATH",
"env":"REPLACE_WITH_THE_ENVIRONMENT_TYPE",
"port":"REPLACE_WITH_THE_API_PORT"
}
50 changes: 26 additions & 24 deletions configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

// Config defines how to model a Config
type Config struct {
Domain string `json:"domain"`
Environment string `json:"env"`
GoogleEmail string `json:"google_email"`
GooglePassword string `json:"google_password"`
FirebaseKey string `json:"firebase_key"`
Expand All @@ -20,53 +22,35 @@ type Config struct {
DatabasePassword string `json:"mongo_database_password"`
PrivateKeyPath string `json:"private_key_path"`
PublicKeyPath string `json:"public_key_path"`
Environment string `json:"env"`
Port string `json:"port"`
}

var mgoSession *mgo.Session
var config *Config

// InitConfig loads the configuration from the filesystem.
func InitConfig() (Config, *mgo.DialInfo) {
func InitConfig() *Config {
file, err1 := os.Open("config.json")
decoder := json.NewDecoder(file)

if err1 != nil {
log.Fatal(err1)
}

config := Config{}
err2 := decoder.Decode(&config)
if err2 != nil {
log.Fatal("Error when parsing config file. Make sure the configuration file (config.json) is valid.")
}

var address []string
if config.Environment == "local" {
address = append(address, "localhost:27017")
} else {
address = append(address, "db")
}

info := &mgo.DialInfo{
Addrs: address,
Database: config.DatabaseName,
Source: config.DatabaseSource,
Username: config.DatabaseUsername,
Password: config.DatabasePassword,
Timeout: time.Second * 10,
}

return config, info
return config
}

// GetMongoSession creates a new session.
// If there is an active mongo session it will return a Clone.
func GetMongoSession() *mgo.Session {
if mgoSession == nil {
var err error
_, info := InitConfig()
mgoSession, err = mgo.DialWithInfo(info)
mgoSession, err = mgo.DialWithInfo(initMongoConfig())

if err != nil {
log.Fatal(err)
Expand All @@ -85,12 +69,30 @@ func (config Config) GetCDN() string {

switch config.Environment {
case "prod":
cdn = "https://insapp.fr/cdn/"
cdn = config.Domain + "/cdn/"
case "dev":
cdn = "https://insapp.insa-rennes.fr/cdn/"
cdn = config.Domain + "/cdn/"
case "local":
cdn = "test"
}

return cdn
}

func initMongoConfig() *mgo.DialInfo {
var address []string
if config.Environment == "local" {
address = append(address, "localhost:27017")
} else {
address = append(address, "db")
}

return &mgo.DialInfo{
Addrs: address,
Database: config.DatabaseName,
Source: config.DatabaseSource,
Username: config.DatabaseUsername,
Password: config.DatabasePassword,
Timeout: time.Second * 10,
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/gorilla/mux v1.7.3
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
github.com/thomas-bouvier/palette-extractor v0.0.0-20180722182330-7ab9b90f05ff
github.com/urfave/cli v1.22.1
golang.org/x/net v0.0.0-20191014212845-da9a3fd4c582
google.golang.org/api v0.11.0 // indirect
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ firebase.google.com/go v3.9.0+incompatible/go.mod h1:xlah6XbEyW6tbfSklcfe5FHJIwj
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
Expand Down Expand Up @@ -57,9 +59,16 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/thomas-bouvier/palette-extractor v0.0.0-20180722182330-7ab9b90f05ff h1:MuptVUFAO6NccMzFEqU9w4RUdHQenzzHW2cMt9VycbY=
github.com/thomas-bouvier/palette-extractor v0.0.0-20180722182330-7ab9b90f05ff/go.mod h1:wbhBBip5VedjDpNOeFogo9GiY9DJA7Fs/9rhA0K3Mq8=
github.com/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0 h1:C9hSCOW830chIVkdja34wa6Ky+IzWllkUinR+BtRZd4=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
Expand Down Expand Up @@ -163,6 +172,7 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 h1:VpOs+IwYnYBaFnrNAeB8UUWtL3vEUnzSCL1nVjPhqrw=
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
2 changes: 2 additions & 0 deletions loginController.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,14 @@ func setAuthAndRefreshCookies(w *http.ResponseWriter, authToken string, refreshT
http.SetCookie(*w, &http.Cookie{
Name: "AuthToken",
Value: authToken,
Domain: config.Domain,
HttpOnly: true,
})

http.SetCookie(*w, &http.Cookie{
Name: "RefreshToken",
Value: refreshToken,
Domain: config.Domain,
HttpOnly: true,
})
}
Expand Down
7 changes: 1 addition & 6 deletions mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import (
)

func SendEmail(to string, subject string, body string) {
config, _ := InitConfig()

from := config.GoogleEmail
pass := config.GooglePassword
cc := config.GoogleEmail

if config.Environment != "prod" {
to = from
subject = "[DEV] " + subject
Expand Down Expand Up @@ -45,8 +44,6 @@ func SendAssociationEmailSubscription(email string, password string) error {
}

func SendAssociationEmailForCommentOnEvent(email string, event Event, comment Comment, user User) error {
config, _ := InitConfig()

data := struct {
EventName string
EventImage string
Expand All @@ -70,8 +67,6 @@ func SendAssociationEmailForCommentOnEvent(email string, event Event, comment Co
}

func SendAssociationEmailForCommentOnPost(email string, post Post, comment Comment, user User) error {
config, _ := InitConfig()

data := struct {
PostName string
PostImage string
Expand Down
4 changes: 2 additions & 2 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type Routes []Route

// NewRouter is the constructor of the Router
// It will create every routes from the routes variable just above
func NewRouter(config Config) *mux.Router {
err := InitJWT(config)
func NewRouter() *mux.Router {
err := InitJWT()
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit 245c127

Please sign in to comment.