Luís Murta 86036afa4e
API server online and DB connection established
Introduces the api-server/main.go, which creates the echo server,
connects it to the implemented handlers and runs it.

Fix the SQL query to retrieved transactions w/ categories.

Issues #6 and #7
2024-03-23 16:51:07 +00:00

43 lines
773 B
Go

package main
import (
"database/sql"
"fmt"
"log"
"os"
"git.rosemyrtle.work/personal-finance/server/internal/api"
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/joho/godotenv"
"github.com/labstack/echo/v4"
)
func main() {
if err := godotenv.Load(); err != nil {
log.Fatal("error loading .env file")
return
}
database := fmt.Sprintf("postgres://%s:%s@%s/%s",
os.Getenv("PG_USER"),
os.Getenv("PG_PASSWORD"),
os.Getenv("PG_URL"),
os.Getenv("PG_DB"))
db, err := sql.Open("pgx", database)
if err != nil {
log.Fatalf("unable to connect to database: %v\n", err)
}
if err := db.Ping(); err != nil {
log.Fatal(err)
}
e := echo.New()
handlers := api.ServerImpl{Db: db}
api.RegisterHandlers(e, &handlers)
e.Logger.Fatal(e.Start(":9000"))
}