auth-api/src/main.go

90 lines
2.5 KiB
Go
Raw Normal View History

2021-01-02 12:09:16 +01:00
package main
import (
"context"
"os"
"strings"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/joho/godotenv"
log "github.com/sirupsen/logrus"
"gitlab.larvit.se/power-plan/auth/src/db"
h "gitlab.larvit.se/power-plan/auth/src/handlers"
)
func createAdminAccount(Db db.Db) {
adminAccountID, uuidErr := uuid.NewRandom()
if uuidErr != nil {
log.Fatal("Could not create new Uuid, err: " + uuidErr.Error())
}
_, adminAccountErr := Db.AccountCreate(db.AccountCreateInput{
ID: adminAccountID,
Name: "admin",
APIKey: os.Getenv("ADMIN_API_KEY"),
Password: "",
Fields: []db.AccountCreateInputFields{{Name: "role", Values: []string{"admin"}}},
2021-01-02 12:09:16 +01:00
})
if adminAccountErr != nil && strings.HasPrefix(adminAccountErr.Error(), "ERROR: duplicate key") {
log.Info("Admin account already created, nothing written to database")
} else if adminAccountErr != nil {
log.Fatal("Could not create admin account, err: " + adminAccountErr.Error())
}
}
func main() {
err := godotenv.Load()
if err != nil {
2021-01-05 17:51:24 +01:00
log.Warn("Error loading .env file, this could be ok if the env file does not exist")
2021-01-02 12:09:16 +01:00
}
// Add this line for logging filename and line number!
// log.SetReportCaller(true)
log.SetLevel(log.DebugLevel)
if os.Getenv("JWT_SHARED_SECRET") == "changeMe" {
log.Fatal("You must change JWT_SHARED_SECRET in .env")
}
if os.Getenv("ADMIN_API_KEY") == "changeMe" {
log.Fatal("You must change ADMIN_API_KEY in .env")
}
jwtKey := []byte(os.Getenv("JWT_SHARED_SECRET"))
2021-01-02 12:09:16 +01:00
dbPool, err := pgxpool.Connect(context.Background(), os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatal("Failed to open DB connection: ", err)
} else {
log.Info("Connected to PostgreSQL database")
}
defer dbPool.Close()
app := fiber.New()
Db := db.Db{DbPool: dbPool}
handlers := h.Handlers{Db: Db, JwtKey: jwtKey}
2021-01-02 12:09:16 +01:00
createAdminAccount(Db)
// Log all requests
app.Use(handlers.Log)
2021-01-02 12:09:16 +01:00
// Always require application/json
app.Use(handlers.RequireJSON)
app.Get("/", handlers.Hello)
app.Get("/account/:accountID", handlers.AccountGet)
app.Post("/account", handlers.AccountCreate)
app.Post("/auth/api-key", handlers.AccountAuthAPIKey)
app.Post("/auth/password", handlers.AccountAuthPassword)
2021-01-05 16:49:50 +01:00
app.Post("/renew-token", handlers.TokenRenew)
2021-01-02 12:09:16 +01:00
log.WithFields(log.Fields{"WEB_BIND_HOST": os.Getenv("WEB_BIND_HOST")}).Info("Trying to start web server")
if err := app.Listen(os.Getenv("WEB_BIND_HOST")); err != nil {
log.Fatal(err)
}
log.Info("Webb server closed, shutting down")
}