Skip to content

Commit

Permalink
Merge pull request #4 from arngrimur/kubernetes
Browse files Browse the repository at this point in the history
Kubernetes
  • Loading branch information
arngrimur committed Jan 8, 2022
2 parents e0fd4eb + 409208b commit 53e815e
Show file tree
Hide file tree
Showing 22 changed files with 1,597 additions and 63 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ name: "CodeQL"

on:
push:
branches: [ main ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ main ]
schedule:
- cron: '17 21 * * 2'

Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ name: Go

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:

Expand Down
14 changes: 7 additions & 7 deletions RESTendpoints/welcome_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package RESTendpoints

import (
"csn/db"
"csn/database_test_helper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/http"
Expand All @@ -15,21 +15,21 @@ func TestWelcome(t *testing.T) {
m WelcomeModel
status int
}
var testDbConfig = db.DbConfig{
DbSecrets: db.DbSecrets{
var testDbConfig = database_test_helper.DbConfig{
DbSecrets: database_test_helper.DbSecrets{
DatabaseUser: "testuser",
DatabasePassword: "testpassword",
},
HostConfig: db.HostConfig{
HostConfig: database_test_helper.HostConfig{
AutoRemove: true,
RestartPolicy: "no",
},
ExpireTime: uint(5000),
DatabaseName: "csn_db",
}
var connString, pool, resource = db.SetupDatbase(testDbConfig)
defer db.Purge(pool, resource)
var sqlDb, err = db.InitDatabase(*connString)
var connString, pool, resource = database_test_helper.SetupDatbase(testDbConfig)
defer database_test_helper.Purge(pool, resource)
var sqlDb, err = database_test_helper.InitDatabase(*connString)
require.NoError(t, err, "Could not set up database")

tests := []struct {
Expand Down
2 changes: 1 addition & 1 deletion db/datastore.go → database_test_helper/datastore.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package db
package database_test_helper

import (
"database/sql"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package db
package database_test_helper

import (
"github.com/stretchr/testify/assert"
Expand Down
File renamed without changes.
40 changes: 40 additions & 0 deletions db/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package db

import (
"database/sql"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
_ "github.com/lib/pq"
"log"
)

func InitDatabase() (*sql.DB, error) {
connString := createConnectionString()
db, openErr := sql.Open("postgres", connString)
if openErr != nil {
log.Fatalf("Can create connection with database at '%s'", connString)
return nil, openErr
}
driver, driverErr := postgres.WithInstance(db, &postgres.Config{})
if driverErr != nil {
log.Fatalf("Can insatnciate database")
return nil, driverErr
}
m, migrateErr := migrate.NewWithDatabaseInstance(
"file:///migrations",
"csn_db", driver)
if migrateErr != nil {
log.Fatalf("Can not migrate datbase.")
return nil, migrateErr
}
upErr := m.Up()
if upErr != nil {
return nil, upErr
} // or m.Step(2) if you want to explicitly set the number of migrations to run
return db, nil
}

func createConnectionString() string {
panic("Implement me!!! Grab from kubenetes secets")
}
1 change: 1 addition & 0 deletions db/migrations/v1.0.0_initialSetUp.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS hits;
1 change: 1 addition & 0 deletions db/migrations/v1.0.0_initialSetUp.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE IF NOT EXISTS hits (ip varchar(15) PRIMARY KEY , hit_count smallint);
2 changes: 1 addition & 1 deletion docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ services:
mysql:
image: mysql:8.0.27
volumes:
- ./db/:/root/db/
- ./database_test_helper/:/root/database_test_helper/
2 changes: 1 addition & 1 deletion docker/postgres/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ FROM mysql:8.0.27
MAINTAINER Arngrimur Bjarnason

ENV MYSQL_DATABASE csn_db
ENV PGDATA=/var/lib/postgresql/data/pgdata
EXPOSE 3306
COPY data/create.sql /docker-entrypoint-initdb.d
24 changes: 15 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,32 @@ module csn
go 1.17

require (
github.com/golang-migrate/migrate/v4 v4.15.1
github.com/lib/pq v1.10.4
github.com/ory/dockertest/v3 v3.8.1
github.com/stretchr/testify v1.7.0
)

)

require (
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.5.1 // indirect
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
github.com/cenkalti/backoff/v4 v4.1.2 // indirect
github.com/containerd/continuity v0.0.0-20190827140505-75bee3e2ccb6 // indirect
github.com/containerd/continuity v0.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/cli v20.10.11+incompatible // indirect
github.com/docker/docker v20.10.7+incompatible // indirect
github.com/docker/docker v20.10.9+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/moby/term v0.0.0-20201216013528-df9cb8a40635 // indirect
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/opencontainers/runc v1.0.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand All @@ -34,8 +37,11 @@ require (
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
golang.org/x/net v0.0.0-20201224014010-6772e930b67b // indirect
golang.org/x/sys v0.0.0-20210426230700-d19ff857e887 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
go.uber.org/atomic v1.6.0 // indirect
golang.org/x/net v0.0.0-20211013171255-e13a2654a71e // indirect
golang.org/x/sys v0.0.0-20211013075003-97ac67df715c // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)

replace golang.org/x/net => golang.org/x/net v0.0.0-20210520170846-37e1c6afe023
Loading

0 comments on commit 53e815e

Please sign in to comment.