Skip to content

Commit

Permalink
Correct init of containers + Configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
titouanfreville committed Mar 5, 2017
1 parent be8a1d1 commit a837575
Show file tree
Hide file tree
Showing 19 changed files with 443 additions and 84 deletions.
23 changes: 21 additions & 2 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 13 additions & 5 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
"github.com/pressly/chi"
"github.com/pressly/chi/docgen"
"github.com/pressly/chi/middleware"
"github.com/titouanfreville/popcubeapi/configs"
"github.com/titouanfreville/popcubeapi/datastores"
"github.com/titouanfreville/popcubeapi/utils"
)

type testDb struct {
type saveDb struct {
db *gorm.DB
}

Expand All @@ -22,7 +23,7 @@ type key string

var (
routes = flag.Bool("routes", false, "Generate router documentation")
dbStore = testDb{}
dbStore = saveDb{}
error422 = utils.NewAPIError(422, "parse.request.body", "Request json object not correct.")
error503 = utils.NewAPIError(503, "database.maintenance", "Database is currently in maintenance state. We are doing our best to get it back online ASAP.")
)
Expand All @@ -40,7 +41,7 @@ func initMiddleware(router *chi.Mux) {
router.Use(middleware.Recoverer)
router.Use(middleware.StripSlashes)
router.Use(middleware.Timeout(5 * 1000))
router.Use(middleware.Heartbeat("/ping"))
// router.Use(middleware.Heartbeat("/heartbeat"))
router.Use(middleware.CloseNotify)
}

Expand Down Expand Up @@ -83,9 +84,16 @@ func basicRoutes(router *chi.Mux) {
}

// StartAPI initialise the api with provided host and port.
func StartAPI(hostname string, port string) {
func StartAPI(hostname string, port string, DbConnectionInfo *configs.DbConnection) {
router := newRouter()
dbStore.db = datastores.Store().InitConnection("root", "popcube_test", "popcube_dev", "database", "3306")
// Init DB connection
user := DbConnectionInfo.User
db := DbConnectionInfo.Database
pass := DbConnectionInfo.Password
host := DbConnectionInfo.Host
dbport := DbConnectionInfo.Port
dbStore.db = datastores.Store().InitConnection(user, db, pass, host, dbport)

initMiddleware(router)
basicRoutes(router)
initAvatarRoute(router)
Expand Down
38 changes: 38 additions & 0 deletions api/api_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,41 @@ type channelObjectParam struct {
}

// <><><><><> <><><><><> <><><><><> <><><><><> //
// <><><><><> EMOJI PARAMETERS <><><><><> //

// swagger:parameters getEmojiFromLink
type emojiLinkParam struct {
//Link of the emoji in server.
// in:path
EmojiLink string `json:"emojiLink"`
}

// swagger:parameters getEmojiFromName
type emojiNameParam struct {
//Link of the emoji in server.
// in:path
EmojiName string `json:"emojiName"`
}

// swagger:parameters getEmojiFromShortcut
type emojiShortcutParam struct {
//Link of the emoji in server.
// in:path
EmojiShortcut string `json:"emojiShortcut"`
}

// swagger:parameters updateEmoji deleteEmoji
type emojiIDParam struct {
//Link of the emoji in server.
// in:path
EmojiID int `json:"emojiID"`
}

// swagger:parameters newEmoji updateEmoji
type emojiObjectParam struct {
//Link of the emoji in server.
// in:body
Emoji models.Emoji `json:"emoji"`
}

// <><><><><> <><><><><> <><><><><> <><><><><> //
21 changes: 21 additions & 0 deletions api/api_responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,24 @@ type channelObjectSuccess struct {
}

// <><><><><> <><><><><> <><><><><> <><><><><> //

// <><><><><> EMOJI RESPONSES <><><><><> //

// emojiSlice Array of emojis
//
// swagger:response emojiArraySuccess
type emojiArraySuccess struct {
// in:body
Emojis []models.Emoji
}

// emojiObjectSuccess list of emojis
//
// swagger:response emojiObjectSuccess
type emojiObjectSuccess struct {
// in:body
// List of emojis returned
Emoji models.Emoji `json:"emoji"`
}

// <><><><><> <><><><><> <><><><><> <><><><><> //
2 changes: 1 addition & 1 deletion api/avatar_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func initAvatarRoute(router chi.Router) {
// This will return the new avatar object
//
// Responses:
// 200: avatarObjectSuccess
// 200: avatzarObjectSuccess
// 422: wrongEntity
// 503: databaseError
// default: genericError
Expand Down
35 changes: 21 additions & 14 deletions api/emojis_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ import (
renderPackage "github.com/unrolled/render"
)

const (
emojiNameKey key = "emojiName"
emojiLinkKey key = "emojiLink"
emojiShortcutKey key = "emojiShortcut"
oldEmojiKey key = "oldEmoji"
)

func initEmojiRoute(router chi.Router) {
router.Route("/emoji", func(r chi.Router) {
r.Get("/", getAllEmoji)
Expand Down Expand Up @@ -51,13 +58,13 @@ func emojiContext(next http.Handler) http.Handler {
link := chi.URLParam(r, "emojiLink")
shortcut := chi.URLParam(r, "emojiShortcut")
oldEmoji := models.Emoji{}
ctx := context.WithValue(r.Context(), "emojiName", name)
ctx = context.WithValue(ctx, "emojiLink", link)
ctx = context.WithValue(ctx, "emojiShortcut", shortcut)
ctx := context.WithValue(r.Context(), emojiNameKey, name)
ctx = context.WithValue(ctx, emojiLinkKey, link)
ctx = context.WithValue(ctx, emojiShortcutKey, shortcut)
if err == nil {
oldEmoji = datastores.Store().Emoji().GetByID(emojiID, dbStore.db)
}
ctx = context.WithValue(ctx, "oldEmoji", oldEmoji)
ctx = context.WithValue(ctx, oldEmojiKey, oldEmoji)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
Expand All @@ -70,15 +77,15 @@ func getAllEmoji(w http.ResponseWriter, r *http.Request) {
result := store.Emoji().GetAll(db)
render.JSON(w, 200, result)
} else {
render.JSON(w, 500, "Connection failure : DATABASE")
render.JSON(w, error503.StatusCode, error503)
}
}

func getEmojiFromName(w http.ResponseWriter, r *http.Request) {
store := datastores.Store()
render := renderPackage.New()
db := dbStore.db
name := r.Context().Value("emojiName").(string)
name := r.Context().Value(emojiNameKey).(string)
emoji := store.Emoji().GetByName(name, db)
render.JSON(w, 200, emoji)
}
Expand All @@ -87,7 +94,7 @@ func getEmojiFromShortcut(w http.ResponseWriter, r *http.Request) {
store := datastores.Store()
render := renderPackage.New()
db := dbStore.db
link := r.Context().Value("emojiShortcut").(string)
link := r.Context().Value(emojiShortcutKey).(string)
emoji := store.Emoji().GetByShortcut(link, db)
render.JSON(w, 200, emoji)
}
Expand All @@ -96,7 +103,7 @@ func getEmojiFromLink(w http.ResponseWriter, r *http.Request) {
store := datastores.Store()
render := renderPackage.New()
db := dbStore.db
link := r.Context().Value("emojiLink").(string)
link := r.Context().Value(emojiLinkKey).(string)
emoji := store.Emoji().GetByLink(link, db)
render.JSON(w, 200, emoji)
}
Expand All @@ -112,7 +119,7 @@ func newEmoji(w http.ResponseWriter, r *http.Request) {
request := r.Body
err := chiRender.Bind(request, &data)
if err != nil {
render.JSON(w, 500, "Internal server error")
render.JSON(w, error422.StatusCode, error422)
} else {
if err := db.DB().Ping(); err == nil {
err := store.Emoji().Save(data.Emoji, db)
Expand All @@ -122,7 +129,7 @@ func newEmoji(w http.ResponseWriter, r *http.Request) {
render.JSON(w, err.StatusCode, err)
}
} else {
render.JSON(w, 500, "Connection failure : DATABASE")
render.JSON(w, error503.StatusCode, error503)
}
}
}
Expand All @@ -137,9 +144,9 @@ func updateEmoji(w http.ResponseWriter, r *http.Request) {
db := dbStore.db
request := r.Body
err := chiRender.Bind(request, &data)
emoji := r.Context().Value("oldEmoji").(models.Emoji)
emoji := r.Context().Value(oldEmojiKey).(models.Emoji)
if err != nil {
render.JSON(w, 500, "Internal server error")
render.JSON(w, error422.StatusCode, error422)
} else {
if err := db.DB().Ping(); err == nil {
err := store.Emoji().Update(&emoji, data.Emoji, db)
Expand All @@ -149,13 +156,13 @@ func updateEmoji(w http.ResponseWriter, r *http.Request) {
render.JSON(w, err.StatusCode, err)
}
} else {
render.JSON(w, 500, "Connection failure : DATABASE")
render.JSON(w, error503.StatusCode, error503)
}
}
}

func deleteEmoji(w http.ResponseWriter, r *http.Request) {
emoji := r.Context().Value("emoji").(models.Emoji)
emoji := r.Context().Value(oldEmojiKey).(models.Emoji)
store := datastores.Store()
render := renderPackage.New()
message := deleteMessageModel{
Expand Down
14 changes: 7 additions & 7 deletions api/folder_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func getAllFolder(w http.ResponseWriter, r *http.Request) {
result := store.Folder().GetAll(db)
render.JSON(w, 200, result)
} else {
render.JSON(w, 500, "Connection failure : DATABASE")
render.JSON(w, error503.StatusCode, error503)
}
}

Expand Down Expand Up @@ -113,13 +113,13 @@ func getFolderFromMessage(w http.ResponseWriter, r *http.Request) {
request := r.Body
err := chiRender.Bind(request, &data)
if err != nil {
render.JSON(w, 500, "Internal server error")
render.JSON(w, error422.StatusCode, error422)
} else {
if err := db.DB().Ping(); err == nil {
role := store.Folder().GetByMessage(data.Message, db)
render.JSON(w, 200, role)
} else {
render.JSON(w, 500, "Connection failure : DATABASE")
render.JSON(w, error503.StatusCode, error503)
}
}
}
Expand All @@ -135,7 +135,7 @@ func newFolder(w http.ResponseWriter, r *http.Request) {
request := r.Body
err := chiRender.Bind(request, &data)
if err != nil {
render.JSON(w, 500, "Internal server error")
render.JSON(w, error422.StatusCode, error422)
} else {
if err := db.DB().Ping(); err == nil {
err := store.Folder().Save(data.Folder, db)
Expand All @@ -145,7 +145,7 @@ func newFolder(w http.ResponseWriter, r *http.Request) {
render.JSON(w, err.StatusCode, err)
}
} else {
render.JSON(w, 500, "Connection failure : DATABASE")
render.JSON(w, error503.StatusCode, error503)
}
}
}
Expand All @@ -162,7 +162,7 @@ func updateFolder(w http.ResponseWriter, r *http.Request) {
err := chiRender.Bind(request, &data)
folder := r.Context().Value("oldFolder").(models.Folder)
if err != nil {
render.JSON(w, 500, "Internal server error")
render.JSON(w, error422.StatusCode, error422)
} else {
if err := db.DB().Ping(); err == nil {
err := store.Folder().Update(&folder, data.Folder, db)
Expand All @@ -172,7 +172,7 @@ func updateFolder(w http.ResponseWriter, r *http.Request) {
render.JSON(w, err.StatusCode, err)
}
} else {
render.JSON(w, 500, "Connection failure : DATABASE")
render.JSON(w, error503.StatusCode, error503)
}
}
}
Expand Down
Loading

0 comments on commit a837575

Please sign in to comment.