Skip to content

Commit 4060cfa

Browse files
committed
Feat: Add saas for DB
- use x-location-group for connecion to deferent BDs - add PORT to run app on port from config
1 parent b0d1c5f commit 4060cfa

File tree

5 files changed

+30
-5
lines changed

5 files changed

+30
-5
lines changed

conf/settings.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ var Config = struct {
1515
DNS string
1616
}
1717
VERBOSE string
18+
LocationGroup string
19+
PORT string
1820
}{}
1921

2022
func init() {

server.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@ import (
77
"os"
88

99
"github.com/gorilla/handlers"
10+
"knowledge-base/conf"
1011
)
1112

1213
func main() {
1314
routes := routes.CreateRoutes()
1415
router := router.NewHatchRouter(routes)
1516
loggerRouter := handlers.CombinedLoggingHandler(os.Stdout, router)
1617

17-
http.ListenAndServe(":3810", loggerRouter)
18+
port := conf.Config.PORT
19+
if port == "" {
20+
port = "3810"
21+
}
22+
port = ":" + port
23+
24+
http.ListenAndServe(port, loggerRouter)
1825
}

store/db.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ func (c *MongoConnection) CloseConnection() {
5454
func (c *MongoConnection) getSessionAndCollection(collectionName string) (session *mgo.Session, collection *mgo.Collection, err error) {
5555
if c.originalSession != nil {
5656
session = c.originalSession.Copy()
57-
collection = session.DB(conf.Config.DB.Name).C(collectionName)
57+
dbName := conf.Config.DB.Name
58+
if conf.Config.LocationGroup != "" {
59+
dbName = dbName + "-" + conf.Config.LocationGroup
60+
}
61+
62+
collection = session.DB(dbName).C(collectionName)
5863
} else {
5964
err = errors.New("No original session found")
6065
}

webActions/articlesWebActions.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strconv"
77

88
"github.com/gorilla/mux"
9+
"knowledge-base/conf"
910
)
1011

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

2023
data, total, left, err := art.model.Read(filter)
2124
w.Header().Set("X-Total-Count", strconv.Itoa(total))
@@ -30,7 +33,8 @@ func (art *ArticlesWebActions) Read(w http.ResponseWriter, r *http.Request) {
3033
//ReadOne get article by ID
3134
func (art *ArticlesWebActions) ReadOne(w http.ResponseWriter, r *http.Request) {
3235
ID := mux.Vars(r)["id"]
33-
36+
lg := r.Header.Get("x-location-group")
37+
conf.Config.LocationGroup = lg
3438
data, err := art.model.ReadOne(ID)
3539

3640
if err != nil {
@@ -43,7 +47,8 @@ func (art *ArticlesWebActions) ReadOne(w http.ResponseWriter, r *http.Request) {
4347
//Create add new article
4448
func (art *ArticlesWebActions) Create(w http.ResponseWriter, r *http.Request) {
4549
body := r.Body
46-
50+
lg := r.Header.Get("x-location-group")
51+
conf.Config.LocationGroup = lg
4752
data, err := art.model.Create(body)
4853

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

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

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

7077
//Delete entry by ID
7178
func (art *ArticlesWebActions) Delete(w http.ResponseWriter, r *http.Request) {
72-
79+
lg := r.Header.Get("x-location-group")
80+
conf.Config.LocationGroup = lg
7381
ID := mux.Vars(r)["id"]
7482

7583
err := art.model.Delete(ID)

webActions/tagsWebAction.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package webActions
33
import (
44
"knowledge-base/model"
55
"net/http"
6+
"knowledge-base/conf"
67
)
78

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

1720
if err != nil {
1821
ErrorWithJSON(w, r, err.Error(), 404)

0 commit comments

Comments
 (0)