Skip to content
This repository has been archived by the owner on Nov 16, 2020. It is now read-only.

Improved error handeling #10

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
POSTGRES_HOST=localhost
POSTGRES_DB=postgres
POSTGRES_USER=postgres
POSTGRES_PASSWORD=ito
5 changes: 3 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"log"

"github.com/ito-org/go-backend/tcn"
"github.com/jmoiron/sqlx"
Expand All @@ -20,7 +21,7 @@ func NewDBConnection(dbHost, dbUser, dbPassword, dbName string) (*DBConnection,

db, err := sqlx.Connect("postgres", connStr)
if err != nil {
fmt.Printf("Failed to connect to Postgres database: %s\n", err.Error())
log.Printf("Failed to connect to Postgres database: %s\n", err.Error())
return nil, err
}
return &DBConnection{db}, err
Expand All @@ -44,7 +45,7 @@ func (db *DBConnection) insertMemo(memo *tcn.Memo) (uint64, error) {
memo.Type,
memo.Data[:],
).Scan(&newID); err != nil {
fmt.Sprintf("Failed to insert memo into database: %s\n", err.Error())
log.Printf("Failed to insert memo into database: %s\n", err.Error())
return 0, err
}
return newID, nil
Expand Down
23 changes: 12 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,29 @@ package main

import (
"fmt"
"log"
"os"

"github.com/joho/godotenv"
"github.com/urfave/cli"
)

func readPostgresSettings(useEnvFile bool) (dbName, dbUser, dbPassword string) {
func readPostgresSettings(useEnvFile bool) (dbHostname, dbName, dbUser, dbPassword string) {
if useEnvFile {
godotenv.Load()
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
}

dbHostname = os.Getenv("POSTGRES_HOSTNAME")
dbName = os.Getenv("POSTGRES_DB")
dbUser = os.Getenv("POSTGRES_USER")
dbPassword = os.Getenv("POSTGRES_PASSWORD")

if dbHostname == "" {
dbHostname = "localhost"
}
if dbName == "" {
dbName = "postgres"
}
Expand All @@ -33,7 +41,6 @@ func readPostgresSettings(useEnvFile bool) (dbName, dbUser, dbPassword string) {
func main() {
var (
port string
dbHost string
useEnvFile bool
)

Expand All @@ -45,20 +52,14 @@ func main() {
Usage: "Port for the server to run on",
Destination: &port,
},
&cli.StringFlag{
Name: "dbhost",
Value: "127.0.0.1",
Usage: "The Postgres host to be used",
Destination: &dbHost,
},
&cli.BoolFlag{
Name: "env",
Usage: "Set to true to read from environment variable file",
Destination: &useEnvFile,
},
},
Action: func(ctx *cli.Context) error {
dbName, dbUser, dbPassword := readPostgresSettings(useEnvFile)
dbHost, dbName, dbUser, dbPassword := readPostgresSettings(useEnvFile)
dbConnection, err := NewDBConnection(dbHost, dbUser, dbPassword, dbName)
if err != nil {
return err
Expand All @@ -68,6 +69,6 @@ func main() {
}

if err := app.Run(os.Args); err != nil {
fmt.Println(err.Error())
log.Fatal(err.Error())
}
}
4 changes: 2 additions & 2 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ func TestMain(m *testing.M) {
// requests. This allows us to create and the database connection which
// would otherwise not happen.

dbName, dbUser, dbPassword := readPostgresSettings(true)
dbHost, dbName, dbUser, dbPassword := readPostgresSettings(true)

dbConn, err := NewDBConnection("localhost", dbUser, dbPassword, dbName)
dbConn, err := NewDBConnection(dbHost, dbUser, dbPassword, dbName)
if err != nil {
panic(err.Error())
}
Expand Down
16 changes: 13 additions & 3 deletions tcn/tcn.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/sha256"
"encoding/binary"
"errors"
"log"
"math"
)

Expand Down Expand Up @@ -112,9 +113,18 @@ type TemporaryContactKey struct {
// contact number.
func (tck *TemporaryContactKey) Ratchet() (*TemporaryContactKey, error) {
nextHash := sha256.New()
nextHash.Write([]byte(HTCKDomainSep))
nextHash.Write(tck.RVK)
nextHash.Write(tck.TCKBytes[:])
_, err := nextHash.Write([]byte(HTCKDomainSep))
if err != nil {
log.Fatal("Could not write hash (HTCKDomainSep)")
}
_, err = nextHash.Write(tck.RVK)
if err != nil {
log.Fatal("Could not write hash (TVK)")
}
_, err = nextHash.Write(tck.TCKBytes[:])
if err != nil {
log.Fatal("Could not write hash (TCK)")
}

if tck.Index == math.MaxUint16 {
return nil, errors.New("rak should be rotated")
Expand Down