Skip to content

Commit

Permalink
feat(db): Adding database connection and crud (#3)
Browse files Browse the repository at this point in the history
* feat(db): Adding database connection and crud

* refactor: Return crud error from service, not database

* chore(deps): Bumping to httputils 3.13
  • Loading branch information
ViBiOh authored Apr 7, 2020
1 parent ab6914f commit 950acb0
Show file tree
Hide file tree
Showing 11 changed files with 434 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ profile.out
# Files
cacert.pem
zoneinfo.zip
.env
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ Usage of ketchup:
[swagger] API Title {KETCHUP_SWAGGER_TITLE} (default "API")
-swaggerVersion string
[swagger] API Version {KETCHUP_SWAGGER_VERSION} (default "1.0.0")
-targetsDefaultPage uint
[targets] Default page {KETCHUP_TARGETS_DEFAULT_PAGE} (default 1)
-targetsDefaultPageSize uint
[targets] Default page size {KETCHUP_TARGETS_DEFAULT_PAGE_SIZE} (default 20)
-targetsMaxPageSize uint
[targets] Max page size {KETCHUP_TARGETS_MAX_PAGE_SIZE} (default 100)
-targetsName string
[targets] Resource's name {KETCHUP_TARGETS_NAME}
-targetsPath string
[targets] HTTP Path {KETCHUP_TARGETS_PATH}
-url string
[alcotest] URL to check {KETCHUP_URL}
-userAgent string
Expand Down
22 changes: 18 additions & 4 deletions cmd/ketchup/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"flag"
"net/http"
"os"
"strings"

"github.com/ViBiOh/httputils/v3/pkg/alcotest"
"github.com/ViBiOh/httputils/v3/pkg/cors"
"github.com/ViBiOh/httputils/v3/pkg/crud"
"github.com/ViBiOh/httputils/v3/pkg/db"
"github.com/ViBiOh/httputils/v3/pkg/httputils"
"github.com/ViBiOh/httputils/v3/pkg/logger"
Expand All @@ -15,11 +17,11 @@ import (
"github.com/ViBiOh/httputils/v3/pkg/swagger"
"github.com/ViBiOh/ketchup/pkg/github"
"github.com/ViBiOh/ketchup/pkg/ketchup"
"github.com/ViBiOh/ketchup/pkg/target"
)

const (
helloPath = "/hello"
dumpPath = "/dump"
targetPath = "/targets"
)

func main() {
Expand All @@ -35,6 +37,7 @@ func main() {
dbConfig := db.Flags(fs, "db")
githubConfig := github.Flags(fs, "github")
ketchupConfig := ketchup.Flags(fs, "ketchup")
crudTargetConfig := crud.GetConfiguredFlags("targets", "Target of Ketchup")(fs, "targets")

logger.Fatal(fs.Parse(os.Args[1:]))

Expand All @@ -47,16 +50,27 @@ func main() {

ketchupDb, err := db.New(dbConfig)
logger.Fatal(err)
server.Health(ketchupDb.Ping)

targetApp := target.New(ketchupDb)
githubApp := github.New(githubConfig)
ketchupAp := ketchup.New(ketchupConfig, ketchupDb, githubApp)
ketchupAp := ketchup.New(ketchupConfig, targetApp, githubApp)

swaggerApp, err := swagger.New(swaggerConfig, server.Swagger)
crudTargetApp, err := crud.New(crudTargetConfig, targetApp)
logger.Fatal(err)

swaggerApp, err := swagger.New(swaggerConfig, server.Swagger, crudTargetApp.Swagger)
logger.Fatal(err)

crudTargetHandler := http.StripPrefix(targetPath, crudTargetApp.Handler())
swaggerHandler := swaggerApp.Handler()

handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, targetPath) {
crudTargetHandler.ServeHTTP(w, r)
return
}

swaggerHandler.ServeHTTP(w, r)
})

Expand Down
19 changes: 19 additions & 0 deletions ddl.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- Cleaning
DROP TABLE IF EXISTS target;

DROP INDEX IF EXISTS target_id;

DROP SEQUENCE IF EXISTS target_id_seq;

-- target
CREATE SEQUENCE target_seq;
CREATE TABLE target (
id BIGINT NOT NULL DEFAULT nextval('target_seq'),
owner TEXT NOT NULL,
repository TEXT NOT NULL,
version TEXT NOT NULL,
creation_date TIMESTAMP WITH TIME ZONE DEFAULT now()
);
ALTER SEQUENCE target_seq OWNED BY target.id;

CREATE UNIQUE INDEX target_id ON target (id);
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/ViBiOh/ketchup

go 1.14

require github.com/ViBiOh/httputils/v3 v3.12.0
require github.com/ViBiOh/httputils/v3 v3.13.0
13 changes: 2 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
github.com/ViBiOh/httputils/v3 v3.12.0 h1:BhSgGoBAaM1dKvcDp0dRJEmE0FlizTg/uWMfFRZeLqA=
github.com/ViBiOh/httputils/v3 v3.12.0/go.mod h1:exwp/UNyjMLfZTIl8i7x+AfBc4+9x2ar1VDvV0qWbv0=
github.com/ViBiOh/httputils/v3 v3.13.0 h1:AMsqtoQREdNeTTJToJyZ6SfmcZyuEN8uhD14Ym7BgtI=
github.com/ViBiOh/httputils/v3 v3.13.0/go.mod h1:exwp/UNyjMLfZTIl8i7x+AfBc4+9x2ar1VDvV0qWbv0=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0 h1:HWo1m869IqiPhD389kmkxeTalrjNbbJTC8LXupb+sl0=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cheekybits/is v0.0.0-20150225183255-68e9c0620927/go.mod h1:h/aW8ynjgkuj+NQRlZcDbAbM1ORAbXjXX77sX7T289U=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
Expand All @@ -27,7 +25,6 @@ github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Expand All @@ -51,17 +48,13 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v1.0.0 h1:vrDKnkGzuGvhNAL56c7DBz29ZL+KxnoR0x7enabFceM=
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
github.com/prometheus/client_golang v1.5.1 h1:bdHYieyGlH+6OLEk2YQha8THib30KP0/yD0YH9m6xcA=
github.com/prometheus/client_golang v1.5.1/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3OK1iX/F2sw+iXX5zU=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 h1:S/YWwWx/RA8rT8tKFRuGUZhuA90OyIBpPCXkcbwU8DE=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M=
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand All @@ -78,7 +71,6 @@ github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/tdewolff/minify/v2 v2.7.3 h1:ngzhF7SaunCtbsBjgm7WJzl9HdiKlA1gYC/Qyx9CVMo=
Expand Down Expand Up @@ -108,7 +100,6 @@ gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLks
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
30 changes: 23 additions & 7 deletions pkg/ketchup/ketchup.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package ketchup

import (
"database/sql"
"context"
"flag"
"fmt"
"strings"
"time"

"github.com/ViBiOh/httputils/v3/pkg/cron"
"github.com/ViBiOh/httputils/v3/pkg/flags"
"github.com/ViBiOh/httputils/v3/pkg/logger"
"github.com/ViBiOh/ketchup/pkg/github"
"github.com/ViBiOh/ketchup/pkg/target"
)

// App of package
Expand All @@ -27,7 +29,7 @@ type app struct {
emailTo string
timezone string

db *sql.DB
targetApp target.App
githubApp github.App
}

Expand All @@ -40,12 +42,12 @@ func Flags(fs *flag.FlagSet, prefix string) Config {
}

// New creates new App from Config
func New(config Config, db *sql.DB, githubApp github.App) App {
func New(config Config, targetApp target.App, githubApp github.App) App {
return &app{
emailTo: strings.TrimSpace(*config.emailTo),
timezone: strings.TrimSpace(*config.timezone),

db: db,
targetApp: targetApp,
githubApp: githubApp,
}
}
Expand All @@ -57,11 +59,25 @@ func (a app) Start() {
}

func (a app) checkUpdates(_ time.Time) error {
release, err := a.githubApp.LastRelease("vibioh", "viws")
targets, _, err := a.targetApp.List(context.Background(), 1, 100, "", false, nil)
if err != nil {
return err
return fmt.Errorf("unable to get targets: %s", err)
}

for _, o := range targets {
target := o.(target.Target)

release, err := a.githubApp.LastRelease(target.Owner, target.Repository)
if err != nil {
return err
}

if release.TagName != target.Version {
logger.Info("New version available for %s/%s", target.Owner, target.Repository)
} else {
logger.Info("%s/%s is up-to-date!", target.Owner, target.Repository)
}
}

logger.Info("%+v", release)
return nil
}
6 changes: 6 additions & 0 deletions pkg/model/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package model

// RowScanner describes scan ability of a row
type RowScanner interface {
Scan(...interface{}) error
}
Loading

0 comments on commit 950acb0

Please sign in to comment.