genofire/hs_monolith
genofire
/
hs_monolith
Archived
1
0
Fork 0

[TASK] add review to status api + and a first version of review api

This commit is contained in:
Martin Geno 2017-03-30 18:43:18 +02:00
parent 4e966a59df
commit 19be8eb253
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
6 changed files with 58 additions and 7 deletions

View File

@ -1,7 +1,7 @@
webserver_bind = ":8080" webserver_bind = ":8080"
[database] [database]
type = "sqlite" type = "sqlite3"
connection = "file::memory:?mode=memory&cache=shared" connection = "file::memory:?mode=memory&cache=shared"
# For Master-Slave cluster # For Master-Slave cluster
# read_connection = "" # read_connection = ""

View File

@ -6,5 +6,6 @@ import (
) )
func BindAPI(router *goji.Mux) { func BindAPI(router *goji.Mux) {
router.HandleFunc(pat.Get("/api/status"), statusHandler) router.HandleFunc(pat.Get("/api/status"), status)
router.HandleFunc(pat.Get("/api/reviews"), listReview)
} }

18
http/review.go Normal file
View File

@ -0,0 +1,18 @@
package http
import (
"net/http"
"github.com/genofire/hs_master-kss-monolith/lib/database"
lib "github.com/genofire/hs_master-kss-monolith/lib/http"
logger "github.com/genofire/hs_master-kss-monolith/lib/log"
"github.com/genofire/hs_master-kss-monolith/models"
)
func listReview(w http.ResponseWriter, r *http.Request) {
log := logger.HTTP(r)
var list []*models.Review
database.Read.Find(&list)
lib.Write(w, list)
log.Info("done")
}

25
http/review_test.go Normal file
View File

@ -0,0 +1,25 @@
package http
import (
"net/http"
"testing"
"github.com/genofire/hs_master-kss-monolith/lib/database"
"github.com/genofire/hs_master-kss-monolith/test"
)
func TestReview(t *testing.T) {
database.Open(database.Config{
Type: "sqlite3",
Connection: ":memory:",
})
database.Write.LogMode(true)
assertion, router := test.Init(t)
BindAPI(router)
session := test.NewSession(router)
result, w := session.JSONRequest("GET", "/api/reviews", nil)
assertion.Equal(http.StatusOK, w.StatusCode)
assertion.Equal([]interface{}{}, result)
}

View File

@ -3,12 +3,17 @@ package http
import ( import (
"net/http" "net/http"
"github.com/genofire/hs_master-kss-monolith/lib/database"
lib "github.com/genofire/hs_master-kss-monolith/lib/http" lib "github.com/genofire/hs_master-kss-monolith/lib/http"
logger "github.com/genofire/hs_master-kss-monolith/lib/log" logger "github.com/genofire/hs_master-kss-monolith/lib/log"
"github.com/genofire/hs_master-kss-monolith/models"
) )
func statusHandler(w http.ResponseWriter, r *http.Request) { func status(w http.ResponseWriter, r *http.Request) {
log := logger.HTTP(r) log := logger.HTTP(r)
lib.Write(w, "running") var reviews []*models.Review
log.Info("show status") var count int64
database.Read.Find(&reviews).Count(&count)
lib.Write(w, map[string]interface{}{"status": "running", "review_count": count})
log.Info("done")
} }

View File

@ -12,8 +12,10 @@ func TestStatus(t *testing.T) {
BindAPI(router) BindAPI(router)
session := test.NewSession(router) session := test.NewSession(router)
result, w := session.JSONRequest("GET", "/api/status", nil) r, w := session.JSONRequest("GET", "/api/status", nil)
result := r.(map[string]interface{})
assertion.Equal(http.StatusOK, w.StatusCode) assertion.Equal(http.StatusOK, w.StatusCode)
assertion.Equal("running", result) assertion.Equal("running", result["status"])
assertion.Equal(float64(0), result["review_count"])
} }