Updating versions and changing log system
Some checks failed
continuous-integration/drone Build encountered an error

This commit is contained in:
2023-02-20 23:54:02 +01:00
parent 16c57cc424
commit c4a97644ed
14 changed files with 324 additions and 143 deletions

View File

@@ -8,23 +8,23 @@ import (
"gitea.larvit.se/pwrpln/auth-api/src/db"
h "gitea.larvit.se/pwrpln/auth-api/src/handlers"
"gitea.larvit.se/pwrpln/auth-api/src/utils"
"gitea.larvit.se/pwrpln/go_log"
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"
"go.uber.org/zap"
// docs are generated by Swag CLI, you have to import them.
_ "gitea.larvit.se/pwrpln/auth-api/src/docs"
)
// Don't put in utils, because it creates import cycle with db... just left it here for now
func createAdminAccount(Db db.Db, log *zap.SugaredLogger) {
func createAdminAccount(Db db.Db, log go_log.Log) {
adminAccountID, uuidErr := uuid.NewRandom()
if uuidErr != nil {
log.Fatal("Could not create new Uuid", "err", uuidErr.Error())
log.Error("Could not create new Uuid", "err", uuidErr.Error())
os.Exit(1)
}
_, adminAccountErr := Db.AccountCreate(db.AccountCreateInput{
ID: adminAccountID,
@@ -34,9 +34,10 @@ func createAdminAccount(Db db.Db, log *zap.SugaredLogger) {
Fields: []db.AccountCreateInputFields{{Name: "role", Values: []string{"admin"}}},
})
if adminAccountErr != nil && strings.HasPrefix(adminAccountErr.Error(), "ERROR: duplicate key") {
log.Info("Admin account already created, nothing written to database")
log.Verbose("Admin account already created, nothing written to database")
} else if adminAccountErr != nil {
log.Fatal("Could not create admin account", "err", adminAccountErr.Error())
log.Error("Could not create admin account", "err", adminAccountErr.Error())
os.Exit(1)
}
}
@@ -52,7 +53,7 @@ func createAdminAccount(Db db.Db, log *zap.SugaredLogger) {
// @BasePath /
func main() {
log := utils.GetLog()
log := go_log.GetLog()
err := godotenv.Load()
if err != nil {
@@ -60,20 +61,33 @@ func main() {
}
if os.Getenv("JWT_SHARED_SECRET") == "changeMe" {
log.Error("JWT_SHARED_SECRET ENV is not set, using very insecure \"changeMe\"")
log.Warn("JWT_SHARED_SECRET ENV is not set, using very insecure \"changeMe\"")
}
if os.Getenv("ADMIN_API_KEY") == "changeMe" {
log.Error("ADMIN_API_KEY ENV is not set, using very insecure \"changeMe\"")
log.Warn("ADMIN_API_KEY ENV is not set, using very insecure \"changeMe\"")
}
if os.Getenv("LOG_MIN_LVL") == "" {
log.Info("LOG_MIN_LVL ENV is not set, using default \"Info\"")
log.MinLogLvl = go_log.LogLvlFromStr("Info")
} else {
minLogLvl := go_log.LogLvlFromStr(os.Getenv("LOG_MIN_LVL"))
if minLogLvl == 0 {
log.Warn("Invalid LOG_MIN_LVL ENV, using default \"Info\"")
log.MinLogLvl = go_log.LogLvlFromStr("Info")
} else {
log.MinLogLvl = minLogLvl
}
}
jwtKey := []byte(os.Getenv("JWT_SHARED_SECRET"))
dbPool, err := pgxpool.Connect(context.Background(), os.Getenv("DATABASE_URL"))
for err != nil {
log.Error("Failed to open connection to PostgreSQL database, retrying in 1 second", "err", err.Error())
log.Warn("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")
log.Verbose("Connected to PostgreSQL database")
defer dbPool.Close()
app := fiber.New()
@@ -96,21 +110,21 @@ func main() {
app.Delete("/account/:accountID", handlers.AccountDel)
app.Get("/account/:accountID", handlers.AccountGet)
app.Post("/account", handlers.AccountCreate)
// app.Get("/accounts", handlers.AccountsGet)
app.Post("/auth/api-key", handlers.AccountAuthAPIKey)
app.Post("/auth/password", handlers.AccountAuthPassword)
app.Post("/renew-token", handlers.RenewToken)
app.Put("/account/:accountID/fields", handlers.AccountUpdateFields)
// app.Put("")
log.Info("Trying to start web server", "WEB_BIND_HOST", os.Getenv("WEB_BIND_HOST"))
log.Info("Starting web server", "WEB_BIND_HOST", os.Getenv("WEB_BIND_HOST"))
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)
log.Warn("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")
log.Info("Web server closed, shutting down")
}