Swap direct access to the DB on the API server with an data abstraction layer. Implement each API type converter separately and revert changes to the auto-generated server implementation types. Add error return to DAL methods. Implement `Transactions`. Add tools.go with oapi-codegen and mockgen. https://www.jvt.me/posts/2022/06/15/go-tools-dependency-management/ Update go packages. Issues #5, #12
49 lines
925 B
Go
49 lines
925 B
Go
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
|
|
"git.rosemyrtle.work/personal-finance/server/internal/api"
|
|
"git.rosemyrtle.work/personal-finance/server/internal/dal"
|
|
|
|
_ "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
|
|
}
|
|
|
|
// 1. Database
|
|
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)
|
|
}
|
|
|
|
// 2. Data Access Layer
|
|
dal := dal.DalImpl{Db: db}
|
|
|
|
// 3. HTTP server
|
|
e := echo.New()
|
|
handlers := api.ServerImpl{Dal: &dal}
|
|
api.RegisterHandlers(e, &handlers)
|
|
|
|
e.Logger.Fatal(e.Start(":9000"))
|
|
}
|