Skip to content

Commit

Permalink
Feat: Add saas for DB
Browse files Browse the repository at this point in the history
- use x-location-group for connecion to deferent BDs
- add PORT to run app on port from config
  • Loading branch information
andreychuk committed Mar 13, 2017
1 parent b0d1c5f commit 4060cfa
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
2 changes: 2 additions & 0 deletions conf/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ var Config = struct {
DNS string
}
VERBOSE string
LocationGroup string
PORT string
}{}

func init() {
Expand Down
9 changes: 8 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ import (
"os"

"github.com/gorilla/handlers"
"knowledge-base/conf"
)

func main() {
routes := routes.CreateRoutes()
router := router.NewHatchRouter(routes)
loggerRouter := handlers.CombinedLoggingHandler(os.Stdout, router)

http.ListenAndServe(":3810", loggerRouter)
port := conf.Config.PORT
if port == "" {
port = "3810"
}
port = ":" + port

http.ListenAndServe(port, loggerRouter)
}
7 changes: 6 additions & 1 deletion store/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ func (c *MongoConnection) CloseConnection() {
func (c *MongoConnection) getSessionAndCollection(collectionName string) (session *mgo.Session, collection *mgo.Collection, err error) {
if c.originalSession != nil {
session = c.originalSession.Copy()
collection = session.DB(conf.Config.DB.Name).C(collectionName)
dbName := conf.Config.DB.Name
if conf.Config.LocationGroup != "" {
dbName = dbName + "-" + conf.Config.LocationGroup
}

collection = session.DB(dbName).C(collectionName)
} else {
err = errors.New("No original session found")
}
Expand Down
14 changes: 11 additions & 3 deletions webActions/articlesWebActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"

"github.com/gorilla/mux"
"knowledge-base/conf"
)

//ArticlesWebActions ...
Expand All @@ -16,6 +17,8 @@ type ArticlesWebActions struct {
//Read get all articles
func (art *ArticlesWebActions) Read(w http.ResponseWriter, r *http.Request) {
filter := r.URL.Query().Get("filter")
lg := r.Header.Get("x-location-group")
conf.Config.LocationGroup = lg

data, total, left, err := art.model.Read(filter)
w.Header().Set("X-Total-Count", strconv.Itoa(total))
Expand All @@ -30,7 +33,8 @@ func (art *ArticlesWebActions) Read(w http.ResponseWriter, r *http.Request) {
//ReadOne get article by ID
func (art *ArticlesWebActions) ReadOne(w http.ResponseWriter, r *http.Request) {
ID := mux.Vars(r)["id"]

lg := r.Header.Get("x-location-group")
conf.Config.LocationGroup = lg
data, err := art.model.ReadOne(ID)

if err != nil {
Expand All @@ -43,7 +47,8 @@ func (art *ArticlesWebActions) ReadOne(w http.ResponseWriter, r *http.Request) {
//Create add new article
func (art *ArticlesWebActions) Create(w http.ResponseWriter, r *http.Request) {
body := r.Body

lg := r.Header.Get("x-location-group")
conf.Config.LocationGroup = lg
data, err := art.model.Create(body)

if err != nil {
Expand All @@ -57,6 +62,8 @@ func (art *ArticlesWebActions) Create(w http.ResponseWriter, r *http.Request) {
func (art *ArticlesWebActions) Update(w http.ResponseWriter, r *http.Request) {
ID := mux.Vars(r)["id"]
body := r.Body
lg := r.Header.Get("x-location-group")
conf.Config.LocationGroup = lg

data, err := art.model.Update(ID, body)

Expand All @@ -69,7 +76,8 @@ func (art *ArticlesWebActions) Update(w http.ResponseWriter, r *http.Request) {

//Delete entry by ID
func (art *ArticlesWebActions) Delete(w http.ResponseWriter, r *http.Request) {

lg := r.Header.Get("x-location-group")
conf.Config.LocationGroup = lg
ID := mux.Vars(r)["id"]

err := art.model.Delete(ID)
Expand Down
3 changes: 3 additions & 0 deletions webActions/tagsWebAction.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package webActions
import (
"knowledge-base/model"
"net/http"
"knowledge-base/conf"
)

//TagsWebActions ...
Expand All @@ -13,6 +14,8 @@ type TagsWebActions struct {
//All return all ags in collection Tags
func (tag *TagsWebActions) All(w http.ResponseWriter, r *http.Request) {
data, err := tag.model.All()
lg := r.Header.Get("x-location-group")
conf.Config.LocationGroup = lg

if err != nil {
ErrorWithJSON(w, r, err.Error(), 404)
Expand Down

0 comments on commit 4060cfa

Please sign in to comment.