Better resilience against crashes

Even better resilience against crashes

Fixed stuff

Renamed API service

Fixed some paths

Simplified error handling

Updated README
This commit is contained in:
2021-09-04 16:24:48 +02:00
parent 8d251e545f
commit d6fd4e705f
16 changed files with 2080 additions and 182 deletions

View File

@@ -29,7 +29,8 @@ func (d Db) AccountCreate(input AccountCreateInput) (CreatedAccount, error) {
for _, field := range input.Fields {
newFieldID, uuidErr := uuid.NewRandom()
if uuidErr != nil {
d.Log.Fatal("Could not create new Uuid", "err", uuidErr.Error())
d.Log.Error("Could not create new Uuid", "err", uuidErr.Error())
return CreatedAccount{}, uuidErr
}
_, err := d.DbPool.Exec(context.Background(), accountFieldsSQL, newFieldID, input.ID, field.Name, field.Values)
@@ -158,7 +159,8 @@ func (d Db) AccountUpdateFields(accountID string, fields []AccountCreateInputFie
for _, field := range fields {
newFieldID, err := uuid.NewRandom()
if err != nil {
d.Log.Fatal("Could not create new Uuid", "err", err.Error())
d.Log.Error("Could not create new Uuid", "err", err.Error())
return Account{}, err
}
_, err = tx.Exec(context.Background(), accountFieldsSQL, newFieldID, accountID, field.Name, field.Values)

View File

@@ -3,7 +3,7 @@ package db
import (
"context"
"gitlab.larvit.se/power-plan/auth/src/utils"
"gitlab.larvit.se/power-plan/auth-api/src/utils"
)
// RenewalTokenCreate obtain a new renewal token

View File

@@ -7,7 +7,7 @@ import (
jwt "github.com/dgrijalva/jwt-go"
"github.com/gofiber/fiber/v2"
"gitlab.larvit.se/power-plan/auth/src/db"
"gitlab.larvit.se/power-plan/auth-api/src/db"
)
func (h Handlers) returnTokens(account db.Account, c *fiber.Ctx) error {

View File

@@ -5,8 +5,8 @@ import (
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"gitlab.larvit.se/power-plan/auth/src/db"
"gitlab.larvit.se/power-plan/auth/src/utils"
"gitlab.larvit.se/power-plan/auth-api/src/db"
"gitlab.larvit.se/power-plan/auth-api/src/utils"
)
type AccountInput struct {
@@ -63,12 +63,14 @@ func (h Handlers) AccountCreate(c *fiber.Ctx) error {
newAccountID, uuidErr := uuid.NewRandom()
if uuidErr != nil {
h.Log.Fatal("Could not create new Uuid", "err", uuidErr.Error())
h.Log.Error("Could not create new Uuid", "err", uuidErr.Error())
return c.Status(500).JSON([]ResJSONError{{Error: "Could not create new account UUID"}})
}
hashedPwd, pwdErr := utils.HashPassword(accountInput.Password)
if pwdErr != nil {
h.Log.Fatal("Could not hash password", "err", pwdErr.Error())
h.Log.Error("Could not hash password", "err", pwdErr.Error())
return c.Status(500).JSON([]ResJSONError{{Error: "Could not hash password: \"" + pwdErr.Error() + "\""}})
}
createdAccount, err := h.Db.AccountCreate(db.AccountCreateInput{

View File

@@ -3,7 +3,7 @@ package handlers
import (
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"gitlab.larvit.se/power-plan/auth/src/db"
"gitlab.larvit.se/power-plan/auth-api/src/db"
)
// AccountUpdateFields godoc

View File

@@ -2,7 +2,7 @@ package handlers
import (
jwt "github.com/dgrijalva/jwt-go"
"gitlab.larvit.se/power-plan/auth/src/db"
"gitlab.larvit.se/power-plan/auth-api/src/db"
"go.uber.org/zap"
)

View File

@@ -4,19 +4,20 @@ import (
"context"
"os"
"strings"
"time"
swagger "github.com/arsmn/fiber-swagger/v2"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/joho/godotenv"
"gitlab.larvit.se/power-plan/auth/src/db"
h "gitlab.larvit.se/power-plan/auth/src/handlers"
"gitlab.larvit.se/power-plan/auth/src/utils"
"gitlab.larvit.se/power-plan/auth-api/src/db"
h "gitlab.larvit.se/power-plan/auth-api/src/handlers"
"gitlab.larvit.se/power-plan/auth-api/src/utils"
"go.uber.org/zap"
// docs are generated by Swag CLI, you have to import them.
_ "gitlab.larvit.se/power-plan/auth/src/docs"
_ "gitlab.larvit.se/power-plan/auth-api/src/docs"
)
// Don't put in utils, because it creates import cycle with db... just left it here for now
@@ -59,19 +60,20 @@ func main() {
}
if os.Getenv("JWT_SHARED_SECRET") == "changeMe" {
log.Fatal("You must change JWT_SHARED_SECRET in .env")
log.Error("JWT_SHARED_SECRET ENV is not set, using very insecure \"changeMe\"")
}
if os.Getenv("ADMIN_API_KEY") == "changeMe" {
log.Fatal("You must change ADMIN_API_KEY in .env")
log.Error("ADMIN_API_KEY ENV is not set, using very insecure \"changeMe\"")
}
jwtKey := []byte(os.Getenv("JWT_SHARED_SECRET"))
dbPool, err := pgxpool.Connect(context.Background(), os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatal("Failed to open DB connection", "err", err.Error())
} else {
log.Info("Connected to PostgreSQL database")
for err != nil {
log.Error("Failed to open connection to PostgreSQL database, retrying in 1 second", "err", err.Error())
time.Sleep(1 * time.Second)
dbPool, err = pgxpool.Connect(context.Background(), os.Getenv("DATABASE_URL"))
}
log.Info("Connected to PostgreSQL database")
defer dbPool.Close()
app := fiber.New()
@@ -102,8 +104,12 @@ func main() {
log.Info("Trying to start web server", "WEB_BIND_HOST", os.Getenv("WEB_BIND_HOST"))
if err := app.Listen(os.Getenv("WEB_BIND_HOST")); err != nil {
log.Fatal("Could not start web server", "err", err.Error())
webBindHost := os.Getenv("WEB_BIND_HOST")
err = app.Listen(webBindHost)
for err != nil {
log.Error("Could not start web server", "err", err.Error(), "WEB_BIND_HOST", webBindHost)
time.Sleep(1 * time.Second)
err = app.Listen(webBindHost)
}
log.Info("Webb server closed, shutting down")

View File

@@ -53,7 +53,7 @@ func RandString(n int) string {
}
func SyslogTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
loc, _ := time.LoadLocation("") // "" == UTC
loc, _ := time.LoadLocation("UTC")
t = t.In(loc)
enc.AppendString(t.Format("2006-01-02 15:04:05"))
@@ -74,7 +74,7 @@ func GetLog() *zap.SugaredLogger {
if err != nil {
log.Panicf("Could not build logger, err: %v", err)
}
defer logger.Sync() // flushes buffer, if any
defer logger.Sync() // Flushes buffer, if any
log := logger.Sugar()
return log