# PrestaProxy

Go reverse proxy in front of PrestaShop `1.7.3` with:

- `Echo` as the HTTP server
- native Go PrestaShop cookie decode/encode
- `templ` for HTML rendering
- Bun for JS and Tailwind builds
- `GORM` with SQLite for app-local state

## Current scope

- Go owns `GET /product/:slug`
- all other routes proxy to the upstream PrestaShop instance
- product data, customer data, and cart summary are read from the PrestaShop database
- session state is derived from the live PrestaShop cookie

## Requirements

- Go
- Bun
- `templ`
- access to the PrestaShop database
- PrestaShop cookie key, or access to the PrestaShop install root

## Configuration

The service now loads `.env` automatically from the project root at startup.

Important variables:

- `PRESTASHOP_PROXY_TARGET`: upstream PrestaShop origin, required
- `PRESTASHOP_COOKIE_NAME`: optional explicit cookie-name override. If omitted, the app derives the standard `PrestaShop-...` name from PrestaShop version and normalized host, and still falls back to prefix matching on reads.
- `PRESTASHOP_COOKIE_KEY`: Defuse/PrestaShop cookie key, required unless bootstrap from install root is used
- `DB_USER`, `DB_PASS`, `DB_NAME`, `DB_HOST`, `DB_PORT`: preferred split MariaDB settings
- `DB_PREFIX`: PrestaShop table prefix
- `PRESTASHOP_DB_DSN`: optional full DSN override
- `PRESTASHOP_PROJECT_ROOT`: optional path to an existing PrestaShop install for bootstrap
- `ROUTE_OWNERSHIP_CONFIG`: route prefix currently handled by Go

Example MariaDB setup:

```env
PRESTASHOP_PROXY_TARGET=http://localhost
PRESTASHOP_COOKIE_KEY=def00000...
PRESTASHOP_DB_DIALECT=mariadb
DB_USER=presta
DB_PASS=presta
DB_NAME=presta
DB_PREFIX=ps_
DB_HOST=mariadb
DB_PORT=3306
```

If `PRESTASHOP_DB_DSN` is set, it takes precedence over the split DB settings.

## Install

```bash
bun install
templ generate
go mod tidy
```

## Build assets

```bash
bun run build
```

Available Bun scripts:

- `bun run build:js`
- `bun run build:css`
- `bun run build:manifest`
- `bun run build`
- `bun run dev`

## Run

```bash
go run ./cmd/proxy
```

Default listen address is `:8080`.

## Health endpoints

- `GET /healthz`
- `GET /readyz`

## Cookie support

Native cookie logic lives in [internal/prestashop/cookie/codec.go](/home/marek/coding/test/pp/internal/prestashop/cookie/codec.go:1).

What it does now:

- decrypts live PrestaShop cookies in Go
- parses plaintext key/value fields
- re-encodes cookies in Go using the same key format

Current limitation:

- it can round-trip and re-encode cookie content, but it does not yet recompute higher-level PrestaShop semantic fields like `checksum` for arbitrary mutations

## Tests

```bash
go test ./...
```
