Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/logger and health #26

Merged
merged 8 commits into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,6 @@ sketch
kavach/

### Vid check server ###
server/vid-check-server.yml
server/config.*

# End of https://www.toptal.com/developers/gitignore/api/code,intellij,go,react
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,18 @@ Once the application is up and running you should be able to access it using the
REACT_APP_BASE_API_URL=http://127.0.0.1:4455/.factly/vid-check/server/
REACT_APP_PUBLIC_URL=http://127.0.0.1:4455/.factly/vid-check/web/
```

- Create config file with name config (and extension .env, .yml, .json) in `server/` and add cnofig variables (eg. below)
```
DATABASE_HOST=postgres
DATABASE_USER=postgres
DATABASE_PASSWORD=postgres
DATABASE_NAME=vidcheck
DATABASE_PORT=5432
DATABASE_SSL_MODE=disable

KAVACH_URL=http://kavach-server:8000

DEGA_INTEGRATION=false
DEGA_URL=http://dega-server:8000
```
2 changes: 1 addition & 1 deletion server/action/rating/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func list(w http.ResponseWriter, r *http.Request) {

//GetDegaRatings fetches all the ratings from dega-server
func GetDegaRatings(uID, sID int) (map[uint]model.Rating, error) {
url := fmt.Sprint(viper.GetString("dega.url"), "/fact-check/ratings?all=true")
url := fmt.Sprint(viper.GetString("dega_url"), "/fact-check/ratings?all=true")
req, _ := http.NewRequest("GET", url, nil)

req.Header.Set("Content-Type", "application/json")
Expand Down
11 changes: 10 additions & 1 deletion server/action/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"net/http"

"github.com/factly/vidcheck/model"

"github.com/factly/vidcheck/config"

"github.com/factly/vidcheck/action/rating"
Expand All @@ -13,6 +15,7 @@ import (

"github.com/factly/vidcheck/action/video"
"github.com/factly/vidcheck/util"
"github.com/factly/x/healthx"
"github.com/factly/x/loggerx"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
Expand Down Expand Up @@ -47,7 +50,13 @@ func RegisterRoutes() http.Handler {
fmt.Println("swagger-ui http://localhost:8080/swagger/index.html")
}

r.With(util.CheckUser, util.CheckSpace).Group(func(r chi.Router) {
sqlDB, _ := model.DB.DB()
healthx.RegisterRoutes(r, healthx.ReadyCheckers{
"database": sqlDB.Ping,
"kavach": util.KavachChecker,
})

r.With(util.GormRequestID, util.CheckUser, util.CheckSpace).Group(func(r chi.Router) {
r.Mount("/analysis", videoanalysis.Router())
r.Mount("/videos", video.Router())
if !config.DegaIntegrated() {
Expand Down
2 changes: 1 addition & 1 deletion server/action/space/my.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func my(w http.ResponseWriter, r *http.Request) {

func getMyOrganisations(uID int) ([]orgWithSpace, error) {
// Fetched all organisations of the user
req, err := http.NewRequest("GET", viper.GetString("kavach.url")+"/organisations/my", nil)
req, err := http.NewRequest("GET", viper.GetString("kavach_url")+"/organisations/my", nil)
if err != nil {
return nil, err
}
Expand Down
45 changes: 31 additions & 14 deletions server/config/vars.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,59 @@
package config

import (
"flag"
"log"

"github.com/spf13/viper"
)

// SetupVars setups all the config variables to run application
func SetupVars() {
var configPath string

flag.StringVar(&configPath, "config", "./config.yaml", "Config file path")
flag.Parse()

viper.SetConfigFile(configPath)
viper.AddConfigPath(".")
viper.SetConfigName("config")
viper.SetEnvPrefix("vidcheck")
viper.AutomaticEnv()

err := viper.ReadInConfig()
if err != nil {
log.Fatal("config file not found...")
}

if !viper.IsSet("postgres.dsn") {
log.Fatal("please provide postgres.dsn in config file")
if !viper.IsSet("database_host") {
log.Fatal("please provide database_host config param")
}

if !viper.IsSet("database_user") {
log.Fatal("please provide database_user config param")
}

if !viper.IsSet("database_name") {
log.Fatal("please provide database_name config param")
}

if !viper.IsSet("database_password") {
log.Fatal("please provide database_password config param")
}

if !viper.IsSet("database_port") {
log.Fatal("please provide database_port config param")
}

if !viper.IsSet("database_ssl_mode") {
log.Fatal("please provide database_ssl_mode config param")
}

if !viper.IsSet("kavach.url") {
log.Fatal("please provide kavach.url in config file")
if !viper.IsSet("kavach_url") {
log.Fatal("please provide kavach_url in config file")
}

if DegaIntegrated() {
if !viper.IsSet("dega.url") {
log.Fatal("please provide dega.url in config file")
if !viper.IsSet("dega_url") {
log.Fatal("please provide dega_url in config file")
}
}
}

// DegaIntegrated checks if dega integration is on
func DegaIntegrated() bool {
return viper.IsSet("dega.integration") && viper.GetBool("dega.integration")
return viper.IsSet("dega_integration") && viper.GetBool("dega_integration")
}
4 changes: 2 additions & 2 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.14
require (
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/factly/x v0.0.5
github.com/factly/x v0.0.9
github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072 // indirect
github.com/gavv/httpexpect v2.0.0+incompatible
github.com/gavv/httpexpect/v2 v2.1.0
Expand All @@ -24,5 +24,5 @@ require (
gopkg.in/h2non/gock.v1 v1.0.15
gopkg.in/yaml.v2 v2.3.0 // indirect
gorm.io/driver/postgres v1.0.2
gorm.io/gorm v1.20.2
gorm.io/gorm v1.20.7
)
6 changes: 6 additions & 0 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DP
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0=
github.com/factly/x v0.0.5 h1:sm6hJa9SXFeXc5eg89nSdmOTN5Zc8+Yjsdu4zJ5Ggxs=
github.com/factly/x v0.0.5/go.mod h1:jm4ezbmIa3fq02gu9WeS7WxhTw2jAO/fvtu5wIdpMrc=
github.com/factly/x v0.0.9 h1:P4T2IOsccskuac+aRXCrvDrv3AZQGZUmmpZ/XjIfqXM=
github.com/factly/x v0.0.9/go.mod h1:SM5ziDUrCL/Ua0nqyGEea4BRk7CPc27pmTIb9ovA2zY=
github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072 h1:DddqAaWDpywytcG8w/qoQ5sAN8X12d3Z3koB0C3Rxsc=
github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072/go.mod h1:duJ4Jxv5lDcvg4QuQr0oowTf7dz4/CR8NtyCooz9HL8=
github.com/fasthttp/websocket v1.4.2 h1:AU/zSiIIAuJjBMf5o+vO0syGOnEfvZRu40xIhW/3RuM=
Expand Down Expand Up @@ -328,6 +330,8 @@ github.com/onsi/ginkgo v1.10.1 h1:q/mM8GF/n0shIN8SaAZ0V+jnLPzen6WIVZdiwrRlMlo=
github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME=
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
Expand Down Expand Up @@ -633,6 +637,8 @@ gorm.io/driver/postgres v1.0.2 h1:mB5JjD4QglbCTdMT1aZDxQzHr87XDK1qh0MKIU3P96g=
gorm.io/driver/postgres v1.0.2/go.mod h1:FvRSYfBI9jEp6ZSjlpS9qNcSjxwYxFc03UOTrHdvvYA=
gorm.io/gorm v1.20.2 h1:bZzSEnq7NDGsrd+n3evOOedDrY5oLM5QPlCjZJUK2ro=
gorm.io/gorm v1.20.2/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.20.7 h1:rMS4CL3pNmYq1V5/X+nHHjh1Dx6dnf27+Cai5zabo+M=
gorm.io/gorm v1.20.7/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
3 changes: 1 addition & 2 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/factly/vidcheck/config"
_ "github.com/factly/vidcheck/docs"
"github.com/factly/vidcheck/model"
"github.com/spf13/viper"
)

// @title VidCheck API
Expand All @@ -36,7 +35,7 @@ func main() {
config.SetupVars()

// db setup
model.SetupDB(viper.GetString("postgres.dsn"))
model.SetupDB()
fmt.Println("DB Migration Done...")

// register routes
Expand Down
21 changes: 18 additions & 3 deletions server/model/setup.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package model

import (
"fmt"
"log"
"time"

"gorm.io/gorm/logger"

"github.com/factly/x/loggerx"
"github.com/spf13/viper"
"gorm.io/gorm/schema"

Expand All @@ -16,13 +19,25 @@ import (
var DB *gorm.DB

// SetupDB is database setup
func SetupDB(DSN interface{}) {
func SetupDB() {
var err error
DB, err = gorm.Open(postgres.Open(viper.GetString("postgres.dsn")), &gorm.Config{

dbString := fmt.Sprint("host=", viper.GetString("database_host"), " ",
"user=", viper.GetString("database_user"), " ",
"password=", viper.GetString("database_password"), " ",
"dbname=", viper.GetString("database_name"), " ",
"port=", viper.GetInt("database_port"), " ",
"sslmode=", viper.GetString("database_ssl_mode"))

DB, err = gorm.Open(postgres.Open(dbString), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
SingularTable: true,
},
Logger: logger.Default.LogMode(logger.Info),
Logger: loggerx.NewGormLogger(logger.Config{
SlowThreshold: 200 * time.Millisecond,
LogLevel: logger.Info,
Colorful: true,
}),
})

if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions server/test/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func (a AnyTime) Match(v driver.Value) bool {
// SetupMockDB setups the mock sql db
func SetupMockDB() sqlmock.Sqlmock {

viper.Set("kavach.url", "http://kavach:6620")
viper.Set("dega.url", "http://dega:8000")
viper.Set("kavach_url", "http://kavach:6620")
viper.Set("dega_url", "http://dega:8000")

db, mock, err := sqlmock.New()
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions server/test/mockServers.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var Dummy_OrgList = []map[string]interface{}{
}

func KavachGock() {
gock.New(viper.GetString("kavach.url") + "/organisations/my").
gock.New(viper.GetString("kavach_url") + "/organisations/my").
Persist().
Reply(http.StatusOK).
JSON(Dummy_OrgList)
Expand Down Expand Up @@ -91,7 +91,7 @@ func DegaGock() {
"nodes": Dummy_RatingList,
}

gock.New(viper.GetString("dega.url")).
gock.New(viper.GetString("dega_url")).
Get("/fact-check/ratings").
MatchParam("all", "true").
Persist().
Expand Down Expand Up @@ -125,7 +125,7 @@ func DegaSpaceGock() {
DummyOwner_Org,
}

gock.New(viper.GetString("dega.url")).
gock.New(viper.GetString("dega_url")).
Get("/core/spaces").
Persist().
Reply(http.StatusOK).
Expand Down
2 changes: 1 addition & 1 deletion server/test/rating/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestRatingDelete(t *testing.T) {
WithPath("rating_id", "invalid_id").
WithHeaders(headers).
Expect().
Status(http.StatusNotFound)
Status(http.StatusBadRequest)

test.ExpectationsMet(t, mock)
})
Expand Down
2 changes: 1 addition & 1 deletion server/test/rating/details_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestRatingDetails(t *testing.T) {
WithPath("rating_id", "invalid_id").
WithHeaders(headers).
Expect().
Status(http.StatusNotFound)
Status(http.StatusBadRequest)
test.ExpectationsMet(t, mock)
})

Expand Down
2 changes: 1 addition & 1 deletion server/test/rating/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestRatingUpdate(t *testing.T) {
WithPath("rating_id", "invalid_id").
WithHeaders(headers).
Expect().
Status(http.StatusNotFound)
Status(http.StatusBadRequest)
test.ExpectationsMet(t, mock)
})

Expand Down
2 changes: 1 addition & 1 deletion server/test/space/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestSpaceDelete(t *testing.T) {
WithPath("space_id", "invalid").
WithHeader("X-User", "1").
Expect().
Status(http.StatusNotFound)
Status(http.StatusBadRequest)
})

t.Run("space record does not exist", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion server/test/space/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestSpaceUpdate(t *testing.T) {
WithHeader("X-User", "1").
WithJSON(Data).
Expect().
Status(http.StatusNotFound)
Status(http.StatusBadRequest)
})

t.Run("invalid space body", func(t *testing.T) {
Expand Down
16 changes: 8 additions & 8 deletions server/test/video/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func TestVideoCreate(t *testing.T) {
})

t.Run("create a video with analysis when integrated with dega", func(t *testing.T) {
viper.Set("dega.integration", true)
viper.Set("dega_integration", true)

mock.ExpectBegin()
mock.ExpectQuery(`INSERT INTO "video"`).
Expand Down Expand Up @@ -165,18 +165,18 @@ func TestVideoCreate(t *testing.T) {

checkResponse(res)
test.ExpectationsMet(t, mock)
viper.Set("dega.integration", false)
viper.Set("dega_integration", false)
})

t.Run("dega returns invalid response", func(t *testing.T) {
viper.Set("dega.integration", true)
viper.Set("dega_integration", true)

gock.Off()

gock.New(testServer.URL).EnableNetworking().Persist()
defer gock.Off()
test.DegaSpaceGock()
gock.New(viper.GetString("dega.url")).
gock.New(viper.GetString("dega_url")).
Get("/fact-check/ratings").
MatchParam("all", "true").
Persist().
Expand All @@ -197,18 +197,18 @@ func TestVideoCreate(t *testing.T) {
Expect().
Status(http.StatusInternalServerError)
test.ExpectationsMet(t, mock)
viper.Set("dega.integration", false)
viper.Set("dega_integration", false)
})

t.Run("ratings not in dega server", func(t *testing.T) {
viper.Set("dega.integration", true)
viper.Set("dega_integration", true)

gock.Off()

gock.New(testServer.URL).EnableNetworking().Persist()
defer gock.Off()
test.DegaSpaceGock()
gock.New(viper.GetString("dega.url")).
gock.New(viper.GetString("dega_url")).
Get("/fact-check/ratings").
MatchParam("all", "true").
Persist().
Expand All @@ -235,7 +235,7 @@ func TestVideoCreate(t *testing.T) {
Expect().
Status(http.StatusInternalServerError)
test.ExpectationsMet(t, mock)
viper.Set("dega.integration", false)
viper.Set("dega_integration", false)
})

}
Loading