Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2820823b36 | |||
| 7d7a8f1baf | |||
| e34e007f28 | |||
| 5b9338175e | |||
| 98b5f1575d | |||
| 1d5f775d33 |
@@ -4,9 +4,10 @@ import Login from "./components/Login";
|
|||||||
import ProductList from "./components/ProductList";
|
import ProductList from "./components/ProductList";
|
||||||
import ProductDetail from "./components/ProductDetail";
|
import ProductDetail from "./components/ProductDetail";
|
||||||
import SubmissionsPage from "./components/SubmissionsPage";
|
import SubmissionsPage from "./components/SubmissionsPage";
|
||||||
import { Inbox, LogOut, Package } from "lucide-react";
|
import ApiKeysPage from "./components/ApiKeysPage";
|
||||||
|
import { Inbox, KeyRound, LogOut, Package } from "lucide-react";
|
||||||
|
|
||||||
type Tab = "products" | "submissions";
|
type Tab = "products" | "submissions" | "keys";
|
||||||
type View = { name: "list" } | { name: "detail"; id: string };
|
type View = { name: "list" } | { name: "detail"; id: string };
|
||||||
|
|
||||||
export default function App() {
|
export default function App() {
|
||||||
@@ -99,6 +100,16 @@ export default function App() {
|
|||||||
</span>
|
</span>
|
||||||
)}
|
)}
|
||||||
</button>
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => setTab("keys")}
|
||||||
|
className={`px-3 py-1.5 rounded-md flex items-center gap-1.5 ${
|
||||||
|
tab === "keys"
|
||||||
|
? "bg-emerald-50 text-emerald-700"
|
||||||
|
: "text-gray-600 hover:bg-gray-100"
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
<KeyRound className="h-4 w-4" /> API 密钥
|
||||||
|
</button>
|
||||||
</nav>
|
</nav>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex items-center gap-4 text-sm text-gray-600">
|
<div className="flex items-center gap-4 text-sm text-gray-600">
|
||||||
@@ -112,7 +123,9 @@ export default function App() {
|
|||||||
</div>
|
</div>
|
||||||
</header>
|
</header>
|
||||||
<main className="flex-1 overflow-auto p-6">
|
<main className="flex-1 overflow-auto p-6">
|
||||||
{tab === "submissions" ? (
|
{tab === "keys" ? (
|
||||||
|
<ApiKeysPage />
|
||||||
|
) : tab === "submissions" ? (
|
||||||
<SubmissionsPage onPending={setPending} />
|
<SubmissionsPage onPending={setPending} />
|
||||||
) : view.name === "list" ? (
|
) : view.name === "list" ? (
|
||||||
<ProductList onOpen={(id) => setView({ name: "detail", id })} />
|
<ProductList onOpen={(id) => setView({ name: "detail", id })} />
|
||||||
|
|||||||
@@ -86,6 +86,20 @@ export const api = {
|
|||||||
request<{ status: string }>(`/products/${id}/msrp/${msrpId}`, {
|
request<{ status: string }>(`/products/${id}/msrp/${msrpId}`, {
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
}),
|
}),
|
||||||
|
addBarcode: (id: string, body: unknown) =>
|
||||||
|
request<import("./types").Barcode>(`/products/${id}/barcodes`, {
|
||||||
|
method: "POST",
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
}),
|
||||||
|
deleteBarcode: (id: string, barcodeId: string) =>
|
||||||
|
request<{ status: string }>(`/products/${id}/barcodes/${barcodeId}`, {
|
||||||
|
method: "DELETE",
|
||||||
|
}),
|
||||||
|
setPrimaryBarcode: (id: string, barcodeId: string) =>
|
||||||
|
request<import("./types").Barcode>(
|
||||||
|
`/products/${id}/barcodes/${barcodeId}/primary`,
|
||||||
|
{ method: "POST" },
|
||||||
|
),
|
||||||
listBrands: () =>
|
listBrands: () =>
|
||||||
request<{ items: import("./types").Brand[] }>("/brands"),
|
request<{ items: import("./types").Brand[] }>("/brands"),
|
||||||
listCategories: () =>
|
listCategories: () =>
|
||||||
@@ -109,4 +123,18 @@ export const api = {
|
|||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify({ note }),
|
body: JSON.stringify({ note }),
|
||||||
}),
|
}),
|
||||||
|
listApiKeys: () =>
|
||||||
|
request<{ items: import("./types").ApiKey[] }>("/keys"),
|
||||||
|
createApiKey: (body: {
|
||||||
|
name: string;
|
||||||
|
owner_email?: string;
|
||||||
|
tier?: string;
|
||||||
|
rate_limit_per_min?: number;
|
||||||
|
}) =>
|
||||||
|
request<{ key: string; item: import("./types").ApiKey; warning: string }>(
|
||||||
|
"/keys",
|
||||||
|
{ method: "POST", body: JSON.stringify(body) },
|
||||||
|
),
|
||||||
|
revokeApiKey: (id: string) =>
|
||||||
|
request<{ status: string }>(`/keys/${id}`, { method: "DELETE" }),
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,278 @@
|
|||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import { api, ApiError } from "../api";
|
||||||
|
import type { ApiKey } from "../types";
|
||||||
|
import { Copy, KeyRound, Plus, Trash2 } from "lucide-react";
|
||||||
|
|
||||||
|
const TIERS = [
|
||||||
|
{ key: "free", label: "免费 (free)", rate: 120 },
|
||||||
|
{ key: "partner", label: "合作方 (partner)", rate: 600 },
|
||||||
|
{ key: "internal", label: "内部 (internal)", rate: 6000 },
|
||||||
|
];
|
||||||
|
|
||||||
|
function tierLabel(tier: string): string {
|
||||||
|
return TIERS.find((t) => t.key === tier)?.label ?? tier;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function ApiKeysPage() {
|
||||||
|
const [rows, setRows] = useState<ApiKey[]>([]);
|
||||||
|
const [error, setError] = useState("");
|
||||||
|
const [creating, setCreating] = useState(false);
|
||||||
|
const [newKey, setNewKey] = useState<string | null>(null);
|
||||||
|
|
||||||
|
async function load() {
|
||||||
|
setError("");
|
||||||
|
try {
|
||||||
|
const res = await api.listApiKeys();
|
||||||
|
setRows(res.items);
|
||||||
|
} catch (e) {
|
||||||
|
setError(e instanceof ApiError ? e.message : "加载失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
load();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
async function revoke(id: string, name: string) {
|
||||||
|
if (!confirm(`确认吊销密钥「${name}」?使用该密钥的请求将立即被拒绝。`)) return;
|
||||||
|
try {
|
||||||
|
await api.revokeApiKey(id);
|
||||||
|
await load();
|
||||||
|
} catch (e) {
|
||||||
|
setError(e instanceof ApiError ? e.message : "操作失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="max-w-4xl">
|
||||||
|
<div className="flex items-center justify-between mb-4">
|
||||||
|
<div>
|
||||||
|
<h2 className="text-lg font-semibold text-gray-800 flex items-center gap-2">
|
||||||
|
<KeyRound className="h-5 w-5 text-emerald-600" /> API 密钥
|
||||||
|
</h2>
|
||||||
|
<p className="text-sm text-gray-500 mt-1">
|
||||||
|
公开 API 免费只读,匿名也可调用(低额度)。签发密钥可获得更高速率上限并归属用量。
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
onClick={() => setCreating(true)}
|
||||||
|
className="px-4 py-2 rounded-lg bg-emerald-600 text-white text-sm font-medium hover:bg-emerald-700 flex items-center gap-1.5"
|
||||||
|
>
|
||||||
|
<Plus className="h-4 w-4" /> 签发密钥
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{error && (
|
||||||
|
<div className="mb-3 bg-red-50 text-red-700 text-sm rounded px-4 py-2">{error}</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{newKey && (
|
||||||
|
<div className="mb-4 bg-amber-50 border border-amber-200 rounded-lg p-4">
|
||||||
|
<div className="text-sm font-medium text-amber-800 mb-1">
|
||||||
|
密钥已生成,请立即复制保存——它只显示这一次。
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<code className="flex-1 bg-white border rounded px-3 py-2 text-sm break-all">
|
||||||
|
{newKey}
|
||||||
|
</code>
|
||||||
|
<button
|
||||||
|
onClick={() => navigator.clipboard?.writeText(newKey)}
|
||||||
|
className="px-3 py-2 rounded border text-sm text-gray-600 hover:bg-gray-50 flex items-center gap-1"
|
||||||
|
>
|
||||||
|
<Copy className="h-4 w-4" /> 复制
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => setNewKey(null)}
|
||||||
|
className="px-3 py-2 rounded text-sm text-gray-500 hover:bg-gray-100"
|
||||||
|
>
|
||||||
|
我已保存
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{creating && (
|
||||||
|
<CreateKeyForm
|
||||||
|
onClose={() => setCreating(false)}
|
||||||
|
onCreated={(plaintext) => {
|
||||||
|
setCreating(false);
|
||||||
|
setNewKey(plaintext);
|
||||||
|
load();
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div className="bg-white border rounded-lg overflow-hidden">
|
||||||
|
<table className="w-full text-sm">
|
||||||
|
<thead className="bg-gray-50 text-gray-500 text-left">
|
||||||
|
<tr>
|
||||||
|
<th className="px-4 py-2 font-medium">名称</th>
|
||||||
|
<th className="px-4 py-2 font-medium">前缀</th>
|
||||||
|
<th className="px-4 py-2 font-medium">级别</th>
|
||||||
|
<th className="px-4 py-2 font-medium">速率/分钟</th>
|
||||||
|
<th className="px-4 py-2 font-medium">用量(今日/累计)</th>
|
||||||
|
<th className="px-4 py-2 font-medium">状态</th>
|
||||||
|
<th className="px-4 py-2 font-medium"></th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody className="divide-y">
|
||||||
|
{rows.length === 0 ? (
|
||||||
|
<tr>
|
||||||
|
<td colSpan={7} className="px-4 py-8 text-center text-gray-400">
|
||||||
|
暂无密钥
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
) : (
|
||||||
|
rows.map((k) => (
|
||||||
|
<tr key={k.id} className={k.revoked_at ? "opacity-50" : ""}>
|
||||||
|
<td className="px-4 py-2 text-gray-800">
|
||||||
|
{k.name}
|
||||||
|
{k.owner_email && (
|
||||||
|
<span className="block text-xs text-gray-400">{k.owner_email}</span>
|
||||||
|
)}
|
||||||
|
</td>
|
||||||
|
<td className="px-4 py-2 text-gray-500">
|
||||||
|
<code>{k.key_prefix}…</code>
|
||||||
|
</td>
|
||||||
|
<td className="px-4 py-2 text-gray-600">{tierLabel(k.tier)}</td>
|
||||||
|
<td className="px-4 py-2 text-gray-600">{k.rate_limit_per_min}</td>
|
||||||
|
<td className="px-4 py-2 text-gray-600">
|
||||||
|
{k.usage.today} / {k.usage.total}
|
||||||
|
</td>
|
||||||
|
<td className="px-4 py-2">
|
||||||
|
{k.revoked_at ? (
|
||||||
|
<span className="text-xs rounded px-2 py-0.5 bg-red-50 text-red-700">
|
||||||
|
已吊销
|
||||||
|
</span>
|
||||||
|
) : (
|
||||||
|
<span className="text-xs rounded px-2 py-0.5 bg-emerald-50 text-emerald-700">
|
||||||
|
启用中
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</td>
|
||||||
|
<td className="px-4 py-2 text-right">
|
||||||
|
{!k.revoked_at && (
|
||||||
|
<button
|
||||||
|
onClick={() => revoke(k.id, k.name)}
|
||||||
|
className="text-gray-400 hover:text-red-600"
|
||||||
|
title="吊销"
|
||||||
|
>
|
||||||
|
<Trash2 className="h-4 w-4" />
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function CreateKeyForm({
|
||||||
|
onClose,
|
||||||
|
onCreated,
|
||||||
|
}: {
|
||||||
|
onClose: () => void;
|
||||||
|
onCreated: (plaintext: string) => void;
|
||||||
|
}) {
|
||||||
|
const [name, setName] = useState("");
|
||||||
|
const [ownerEmail, setOwnerEmail] = useState("");
|
||||||
|
const [tier, setTier] = useState("free");
|
||||||
|
const [rate, setRate] = useState(120);
|
||||||
|
const [busy, setBusy] = useState(false);
|
||||||
|
const [error, setError] = useState("");
|
||||||
|
|
||||||
|
function pickTier(t: string) {
|
||||||
|
setTier(t);
|
||||||
|
const def = TIERS.find((x) => x.key === t);
|
||||||
|
if (def) setRate(def.rate);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function submit() {
|
||||||
|
if (!name.trim()) {
|
||||||
|
setError("名称不能为空");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setBusy(true);
|
||||||
|
setError("");
|
||||||
|
try {
|
||||||
|
const res = await api.createApiKey({
|
||||||
|
name: name.trim(),
|
||||||
|
owner_email: ownerEmail.trim() || undefined,
|
||||||
|
tier,
|
||||||
|
rate_limit_per_min: rate,
|
||||||
|
});
|
||||||
|
onCreated(res.key);
|
||||||
|
} catch (e) {
|
||||||
|
setError(e instanceof ApiError ? e.message : "创建失败");
|
||||||
|
} finally {
|
||||||
|
setBusy(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="mb-4 bg-white border rounded-lg p-5">
|
||||||
|
<h3 className="font-medium text-gray-700 mb-3">签发新密钥</h3>
|
||||||
|
{error && <div className="mb-3 bg-red-50 text-red-700 text-sm rounded px-3 py-2">{error}</div>}
|
||||||
|
<div className="grid grid-cols-2 gap-4">
|
||||||
|
<label className="block">
|
||||||
|
<span className="text-xs text-gray-500">名称 *</span>
|
||||||
|
<input
|
||||||
|
className="w-full border rounded-md px-3 py-2 text-sm mt-1"
|
||||||
|
value={name}
|
||||||
|
onChange={(e) => setName(e.target.value)}
|
||||||
|
placeholder="例如:我的 App / 合作方 X"
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<label className="block">
|
||||||
|
<span className="text-xs text-gray-500">负责人邮箱(可选)</span>
|
||||||
|
<input
|
||||||
|
className="w-full border rounded-md px-3 py-2 text-sm mt-1"
|
||||||
|
value={ownerEmail}
|
||||||
|
onChange={(e) => setOwnerEmail(e.target.value)}
|
||||||
|
placeholder="owner@example.com"
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<label className="block">
|
||||||
|
<span className="text-xs text-gray-500">级别</span>
|
||||||
|
<select
|
||||||
|
className="w-full border rounded-md px-3 py-2 text-sm mt-1 bg-white"
|
||||||
|
value={tier}
|
||||||
|
onChange={(e) => pickTier(e.target.value)}
|
||||||
|
>
|
||||||
|
{TIERS.map((t) => (
|
||||||
|
<option key={t.key} value={t.key}>
|
||||||
|
{t.label}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
<label className="block">
|
||||||
|
<span className="text-xs text-gray-500">速率上限(次/分钟)</span>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
min={1}
|
||||||
|
className="w-full border rounded-md px-3 py-2 text-sm mt-1"
|
||||||
|
value={rate}
|
||||||
|
onChange={(e) => setRate(Math.max(1, parseInt(e.target.value || "1", 10)))}
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
<div className="mt-4 flex gap-2">
|
||||||
|
<button
|
||||||
|
onClick={submit}
|
||||||
|
disabled={busy}
|
||||||
|
className="px-4 py-2 rounded bg-emerald-600 text-white text-sm hover:bg-emerald-700 disabled:opacity-60"
|
||||||
|
>
|
||||||
|
生成
|
||||||
|
</button>
|
||||||
|
<button onClick={onClose} className="px-4 py-2 rounded border text-sm text-gray-600">
|
||||||
|
取消
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -14,8 +14,16 @@ import {
|
|||||||
Trash2,
|
Trash2,
|
||||||
AlertCircle,
|
AlertCircle,
|
||||||
History,
|
History,
|
||||||
|
Star,
|
||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
|
|
||||||
|
const GTIN_TYPES = ["EAN13", "EAN8", "UPC", "ITF14", "GTIN14"];
|
||||||
|
const PACK_LEVELS: { value: string; label: string }[] = [
|
||||||
|
{ value: "each", label: "消费单元" },
|
||||||
|
{ value: "case", label: "箱" },
|
||||||
|
{ value: "pallet", label: "托盘" },
|
||||||
|
];
|
||||||
|
|
||||||
const NUTRIMENT_KEYS: { key: string; label: string }[] = [
|
const NUTRIMENT_KEYS: { key: string; label: string }[] = [
|
||||||
{ key: "energy_kcal", label: "能量 (kcal)" },
|
{ key: "energy_kcal", label: "能量 (kcal)" },
|
||||||
{ key: "energy_kj", label: "能量 (kJ)" },
|
{ key: "energy_kj", label: "能量 (kJ)" },
|
||||||
@@ -39,6 +47,9 @@ const ACTION_LABEL: Record<string, string> = {
|
|||||||
delete_image: "删除图片",
|
delete_image: "删除图片",
|
||||||
add_msrp: "新增建议零售价",
|
add_msrp: "新增建议零售价",
|
||||||
delete_msrp: "删除建议零售价",
|
delete_msrp: "删除建议零售价",
|
||||||
|
add_barcode: "新增条码",
|
||||||
|
delete_barcode: "删除条码",
|
||||||
|
set_primary_barcode: "设为主条码",
|
||||||
};
|
};
|
||||||
|
|
||||||
function Card({
|
function Card({
|
||||||
@@ -400,6 +411,7 @@ export default function ProductDetail({
|
|||||||
</div>
|
</div>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
|
<BarcodesCard product={d} onChange={reload} onError={setError} />
|
||||||
<ImagesCard
|
<ImagesCard
|
||||||
product={d}
|
product={d}
|
||||||
onChange={reload}
|
onChange={reload}
|
||||||
@@ -435,6 +447,158 @@ export default function ProductDetail({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function BarcodesCard({
|
||||||
|
product,
|
||||||
|
onChange,
|
||||||
|
onError,
|
||||||
|
}: {
|
||||||
|
product: Detail;
|
||||||
|
onChange: () => void;
|
||||||
|
onError: (m: string) => void;
|
||||||
|
}) {
|
||||||
|
const [gtin, setGtin] = useState("");
|
||||||
|
const [gtinType, setGtinType] = useState("EAN13");
|
||||||
|
const [packLevel, setPackLevel] = useState("each");
|
||||||
|
const [region, setRegion] = useState("");
|
||||||
|
const [busy, setBusy] = useState(false);
|
||||||
|
|
||||||
|
async function add() {
|
||||||
|
if (!gtin.trim()) return;
|
||||||
|
setBusy(true);
|
||||||
|
try {
|
||||||
|
await api.addBarcode(product.id, {
|
||||||
|
gtin: gtin.trim(),
|
||||||
|
gtin_type: gtinType,
|
||||||
|
pack_level: packLevel,
|
||||||
|
region: region.trim() || null,
|
||||||
|
is_primary: false,
|
||||||
|
});
|
||||||
|
setGtin("");
|
||||||
|
setRegion("");
|
||||||
|
onChange();
|
||||||
|
} catch (e) {
|
||||||
|
onError(e instanceof Error ? e.message : "添加失败");
|
||||||
|
} finally {
|
||||||
|
setBusy(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async function remove(barcodeId: string) {
|
||||||
|
try {
|
||||||
|
await api.deleteBarcode(product.id, barcodeId);
|
||||||
|
onChange();
|
||||||
|
} catch (e) {
|
||||||
|
onError(e instanceof Error ? e.message : "删除失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async function makePrimary(barcodeId: string) {
|
||||||
|
try {
|
||||||
|
await api.setPrimaryBarcode(product.id, barcodeId);
|
||||||
|
onChange();
|
||||||
|
} catch (e) {
|
||||||
|
onError(e instanceof Error ? e.message : "设置失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Card title="条码(一品多码,主条码镜像到 GTIN)">
|
||||||
|
<div className="mb-3 space-y-2">
|
||||||
|
{product.barcodes.length === 0 && (
|
||||||
|
<span className="text-sm text-gray-400">暂无条码</span>
|
||||||
|
)}
|
||||||
|
{product.barcodes.map((b) => (
|
||||||
|
<div
|
||||||
|
key={b.id}
|
||||||
|
className="flex items-center gap-3 rounded border border-gray-100 bg-gray-50 px-3 py-2 text-sm"
|
||||||
|
>
|
||||||
|
<button
|
||||||
|
onClick={() => !b.is_primary && makePrimary(b.id)}
|
||||||
|
title={b.is_primary ? "主条码" : "设为主条码"}
|
||||||
|
disabled={b.is_primary}
|
||||||
|
className={
|
||||||
|
b.is_primary
|
||||||
|
? "text-amber-500"
|
||||||
|
: "text-gray-300 hover:text-amber-500"
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<Star
|
||||||
|
className="h-4 w-4"
|
||||||
|
fill={b.is_primary ? "currentColor" : "none"}
|
||||||
|
/>
|
||||||
|
</button>
|
||||||
|
<span className="font-mono font-medium text-gray-800">
|
||||||
|
{b.gtin}
|
||||||
|
</span>
|
||||||
|
<span className="rounded bg-gray-200 px-1.5 py-0.5 text-[11px] text-gray-600">
|
||||||
|
{b.gtin_type}
|
||||||
|
</span>
|
||||||
|
<span className="text-gray-500">
|
||||||
|
{PACK_LEVELS.find((p) => p.value === b.pack_level)?.label ||
|
||||||
|
b.pack_level}
|
||||||
|
</span>
|
||||||
|
<span className="flex-1 text-gray-400">{b.region || ""}</span>
|
||||||
|
<button
|
||||||
|
onClick={() => remove(b.id)}
|
||||||
|
className="text-gray-400 hover:text-red-600"
|
||||||
|
>
|
||||||
|
<Trash2 className="h-4 w-4" />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
<div className="flex flex-wrap items-end gap-2">
|
||||||
|
<Field label="条码 (GTIN)">
|
||||||
|
<input
|
||||||
|
className="w-44 rounded border border-gray-300 px-3 py-2 text-sm"
|
||||||
|
value={gtin}
|
||||||
|
onChange={(e) => setGtin(e.target.value)}
|
||||||
|
placeholder="8/12/13/14 位"
|
||||||
|
/>
|
||||||
|
</Field>
|
||||||
|
<Field label="类型">
|
||||||
|
<select
|
||||||
|
className="rounded border border-gray-300 px-2 py-2 text-sm"
|
||||||
|
value={gtinType}
|
||||||
|
onChange={(e) => setGtinType(e.target.value)}
|
||||||
|
>
|
||||||
|
{GTIN_TYPES.map((t) => (
|
||||||
|
<option key={t} value={t}>
|
||||||
|
{t}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</Field>
|
||||||
|
<Field label="包装层级">
|
||||||
|
<select
|
||||||
|
className="rounded border border-gray-300 px-2 py-2 text-sm"
|
||||||
|
value={packLevel}
|
||||||
|
onChange={(e) => setPackLevel(e.target.value)}
|
||||||
|
>
|
||||||
|
{PACK_LEVELS.map((p) => (
|
||||||
|
<option key={p.value} value={p.value}>
|
||||||
|
{p.label}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</Field>
|
||||||
|
<Field label="地区(可选)">
|
||||||
|
<input
|
||||||
|
className="w-20 rounded border border-gray-300 px-3 py-2 text-sm"
|
||||||
|
value={region}
|
||||||
|
onChange={(e) => setRegion(e.target.value.toUpperCase())}
|
||||||
|
/>
|
||||||
|
</Field>
|
||||||
|
<button
|
||||||
|
onClick={add}
|
||||||
|
disabled={busy}
|
||||||
|
className="flex items-center gap-1 rounded bg-gray-700 px-3 py-2 text-sm text-white hover:bg-gray-800 disabled:opacity-60"
|
||||||
|
>
|
||||||
|
<Plus className="h-4 w-4" /> 添加
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function ImagesCard({
|
function ImagesCard({
|
||||||
product,
|
product,
|
||||||
onChange,
|
onChange,
|
||||||
|
|||||||
@@ -10,6 +10,15 @@ export interface ProductRow {
|
|||||||
updated_at: string;
|
updated_at: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface Barcode {
|
||||||
|
id: string;
|
||||||
|
gtin: string;
|
||||||
|
gtin_type: string;
|
||||||
|
pack_level: string;
|
||||||
|
region: string | null;
|
||||||
|
is_primary: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
export interface ProductImage {
|
export interface ProductImage {
|
||||||
id: string;
|
id: string;
|
||||||
url: string;
|
url: string;
|
||||||
@@ -47,6 +56,7 @@ export interface ProductDetail {
|
|||||||
nutrition_basis: string | null;
|
nutrition_basis: string | null;
|
||||||
serving_size: string | null;
|
serving_size: string | null;
|
||||||
nutri_score: string | null;
|
nutri_score: string | null;
|
||||||
|
barcodes: Barcode[];
|
||||||
images: ProductImage[];
|
images: ProductImage[];
|
||||||
msrp: MSRP[];
|
msrp: MSRP[];
|
||||||
missing: string[];
|
missing: string[];
|
||||||
@@ -127,6 +137,25 @@ export interface SubmissionDetail {
|
|||||||
existing_product?: ProductDetail;
|
existing_product?: ProductDetail;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ApiKeyUsage {
|
||||||
|
total: number;
|
||||||
|
today: number;
|
||||||
|
last_used_at?: number | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ApiKey {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
key_prefix: string;
|
||||||
|
owner_email: string | null;
|
||||||
|
tier: string;
|
||||||
|
rate_limit_per_min: number;
|
||||||
|
revoked_at: string | null;
|
||||||
|
created_by: string | null;
|
||||||
|
created_at: string;
|
||||||
|
usage: ApiKeyUsage;
|
||||||
|
}
|
||||||
|
|
||||||
export const FIELD_LABELS: Record<string, string> = {
|
export const FIELD_LABELS: Record<string, string> = {
|
||||||
name: "名称",
|
name: "名称",
|
||||||
gtin: "条码",
|
gtin: "条码",
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import (
|
|||||||
"github.com/baicai2026-baicai/goods/api/internal/adminstore"
|
"github.com/baicai2026-baicai/goods/api/internal/adminstore"
|
||||||
"github.com/baicai2026-baicai/goods/api/internal/adminweb"
|
"github.com/baicai2026-baicai/goods/api/internal/adminweb"
|
||||||
"github.com/baicai2026-baicai/goods/api/internal/auth"
|
"github.com/baicai2026-baicai/goods/api/internal/auth"
|
||||||
|
"github.com/baicai2026-baicai/goods/api/internal/ratelimit"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getenv(key, fallback string) string {
|
func getenv(key, fallback string) string {
|
||||||
@@ -69,7 +70,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
authn := auth.New(username, passwordHash, secret, 12*time.Hour)
|
authn := auth.New(username, passwordHash, secret, 12*time.Hour)
|
||||||
h := adminhandler.New(adminstore.New(pool), authn, basePath, adminweb.Dist())
|
usage := ratelimit.New(getenv("OPENGOODS_REDIS_URL", "redis://localhost:6379/0"))
|
||||||
|
h := adminhandler.New(adminstore.New(pool), authn, basePath, adminweb.Dist()).
|
||||||
|
WithUsage(usage)
|
||||||
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Addr: addr,
|
Addr: addr,
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"github.com/baicai2026-baicai/goods/api/internal/config"
|
"github.com/baicai2026-baicai/goods/api/internal/config"
|
||||||
"github.com/baicai2026-baicai/goods/api/internal/handler"
|
"github.com/baicai2026-baicai/goods/api/internal/handler"
|
||||||
"github.com/baicai2026-baicai/goods/api/internal/publicweb"
|
"github.com/baicai2026-baicai/goods/api/internal/publicweb"
|
||||||
|
"github.com/baicai2026-baicai/goods/api/internal/ratelimit"
|
||||||
"github.com/baicai2026-baicai/goods/api/internal/store"
|
"github.com/baicai2026-baicai/goods/api/internal/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -31,7 +32,12 @@ func main() {
|
|||||||
log.Printf("warning: database not reachable at startup: %v", err)
|
log.Printf("warning: database not reachable at startup: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
h := handler.New(store.New(pool), publicweb.Dist())
|
limiter := ratelimit.New(cfg.RedisURL)
|
||||||
|
if !limiter.Enabled() {
|
||||||
|
log.Print("warning: Redis not configured; public API rate limiting disabled")
|
||||||
|
}
|
||||||
|
h := handler.New(store.New(pool), publicweb.Dist()).
|
||||||
|
WithRateLimit(limiter, cfg.AnonRateLimitPerMin)
|
||||||
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Addr: cfg.Addr,
|
Addr: cfg.Addr,
|
||||||
|
|||||||
@@ -5,13 +5,17 @@ go 1.23.4
|
|||||||
require (
|
require (
|
||||||
github.com/go-chi/chi/v5 v5.1.0
|
github.com/go-chi/chi/v5 v5.1.0
|
||||||
github.com/jackc/pgx/v5 v5.7.2
|
github.com/jackc/pgx/v5 v5.7.2
|
||||||
|
github.com/redis/go-redis/v9 v9.18.0
|
||||||
golang.org/x/crypto v0.31.0
|
golang.org/x/crypto v0.31.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||||
|
go.uber.org/atomic v1.11.0 // indirect
|
||||||
golang.org/x/sync v0.10.0 // indirect
|
golang.org/x/sync v0.10.0 // indirect
|
||||||
golang.org/x/text v0.21.0 // indirect
|
golang.org/x/text v0.21.0 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
+16
@@ -1,6 +1,14 @@
|
|||||||
|
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
||||||
|
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
|
||||||
|
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
|
||||||
|
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||||
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
|
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
|
||||||
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
|
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
|
||||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||||
@@ -11,13 +19,21 @@ github.com/jackc/pgx/v5 v5.7.2 h1:mLoDLV6sonKlvjIEsV56SkWNCnuNv531l94GaIzO+XI=
|
|||||||
github.com/jackc/pgx/v5 v5.7.2/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ=
|
github.com/jackc/pgx/v5 v5.7.2/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ=
|
||||||
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
|
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
|
||||||
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/redis/go-redis/v9 v9.18.0 h1:pMkxYPkEbMPwRdenAzUNyFNrDgHx9U+DrBabWNfSRQs=
|
||||||
|
github.com/redis/go-redis/v9 v9.18.0/go.mod h1:k3ufPphLU5YXwNTUcCRXGxUoF1fqxnhFQmscfkCoDA0=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||||
|
github.com/zeebo/xxh3 v1.0.2 h1:xZmwmqxHZA8AI603jOQ0tMqmBr9lPeFwGg6d+xy9DC0=
|
||||||
|
github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA=
|
||||||
|
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
|
||||||
|
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
|
||||||
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
|
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
|
||||||
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
|
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
|
||||||
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
|
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
|
||||||
|
|||||||
@@ -0,0 +1,70 @@
|
|||||||
|
package adminhandler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
|
||||||
|
"github.com/baicai2026-baicai/goods/api/internal/adminstore"
|
||||||
|
"github.com/baicai2026-baicai/goods/api/internal/auth"
|
||||||
|
"github.com/baicai2026-baicai/goods/api/internal/ratelimit"
|
||||||
|
)
|
||||||
|
|
||||||
|
// apiKeyView is an issued key plus its usage counters.
|
||||||
|
type apiKeyView struct {
|
||||||
|
adminstore.APIKeyRow
|
||||||
|
Usage ratelimit.UsageStat `json:"usage"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListAPIKeys returns all issued keys with usage stats merged in.
|
||||||
|
func (h *Handler) ListAPIKeys(w http.ResponseWriter, r *http.Request) {
|
||||||
|
keys, err := h.store.ListAPIKeys(r.Context())
|
||||||
|
if h.handleErr(w, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
views := make([]apiKeyView, 0, len(keys))
|
||||||
|
for _, k := range keys {
|
||||||
|
v := apiKeyView{APIKeyRow: k}
|
||||||
|
if h.usage != nil {
|
||||||
|
v.Usage = h.usage.Usage(r.Context(), k.ID)
|
||||||
|
}
|
||||||
|
views = append(views, v)
|
||||||
|
}
|
||||||
|
writeJSON(w, http.StatusOK, map[string]any{"items": views})
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateAPIKey issues a new key and returns its plaintext exactly once.
|
||||||
|
func (h *Handler) CreateAPIKey(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var in adminstore.APIKeyInput
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
||||||
|
writeError(w, http.StatusBadRequest, "bad_request", "invalid body")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(in.Name) == "" {
|
||||||
|
writeError(w, http.StatusBadRequest, "bad_request", "名称不能为空")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if in.Tier != "" && in.Tier != "free" && in.Tier != "partner" && in.Tier != "internal" {
|
||||||
|
writeError(w, http.StatusBadRequest, "bad_request", "tier 取值无效")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
plaintext, row, err := h.store.CreateAPIKey(r.Context(), in, auth.UserFrom(r.Context()))
|
||||||
|
if h.handleErr(w, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeJSON(w, http.StatusCreated, map[string]any{
|
||||||
|
"key": plaintext,
|
||||||
|
"item": row,
|
||||||
|
"warning": "请立即复制保存此密钥,它只显示这一次,无法再次查看。",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// RevokeAPIKey disables a key. Subsequent requests with it are rejected.
|
||||||
|
func (h *Handler) RevokeAPIKey(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if err := h.store.RevokeAPIKey(r.Context(), chi.URLParam(r, "id")); h.handleErr(w, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeJSON(w, http.StatusOK, map[string]string{"status": "revoked"})
|
||||||
|
}
|
||||||
@@ -15,6 +15,8 @@ import (
|
|||||||
|
|
||||||
"github.com/baicai2026-baicai/goods/api/internal/adminstore"
|
"github.com/baicai2026-baicai/goods/api/internal/adminstore"
|
||||||
"github.com/baicai2026-baicai/goods/api/internal/auth"
|
"github.com/baicai2026-baicai/goods/api/internal/auth"
|
||||||
|
"github.com/baicai2026-baicai/goods/api/internal/gtin"
|
||||||
|
"github.com/baicai2026-baicai/goods/api/internal/ratelimit"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Handler holds the admin dependencies.
|
// Handler holds the admin dependencies.
|
||||||
@@ -24,6 +26,7 @@ type Handler struct {
|
|||||||
basePath string
|
basePath string
|
||||||
spa fs.FS
|
spa fs.FS
|
||||||
submitLimit *rateLimiter
|
submitLimit *rateLimiter
|
||||||
|
usage *ratelimit.Limiter
|
||||||
}
|
}
|
||||||
|
|
||||||
// New constructs an admin Handler. basePath is e.g. "/ping" (no trailing slash).
|
// New constructs an admin Handler. basePath is e.g. "/ping" (no trailing slash).
|
||||||
@@ -38,6 +41,13 @@ func New(store *adminstore.Store, authn *auth.Authenticator, basePath string, sp
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithUsage attaches a Redis-backed limiter used to read per-key usage counters
|
||||||
|
// for the API-key management view. Optional; without it usage shows as zero.
|
||||||
|
func (h *Handler) WithUsage(l *ratelimit.Limiter) *Handler {
|
||||||
|
h.usage = l
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
|
||||||
// Router builds the HTTP handler.
|
// Router builds the HTTP handler.
|
||||||
func (h *Handler) Router() http.Handler {
|
func (h *Handler) Router() http.Handler {
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
@@ -62,6 +72,9 @@ func (h *Handler) Router() http.Handler {
|
|||||||
r.Delete("/api/products/{id}/images/{imageID}", h.DeleteImage)
|
r.Delete("/api/products/{id}/images/{imageID}", h.DeleteImage)
|
||||||
r.Post("/api/products/{id}/msrp", h.AddMSRP)
|
r.Post("/api/products/{id}/msrp", h.AddMSRP)
|
||||||
r.Delete("/api/products/{id}/msrp/{msrpID}", h.DeleteMSRP)
|
r.Delete("/api/products/{id}/msrp/{msrpID}", h.DeleteMSRP)
|
||||||
|
r.Post("/api/products/{id}/barcodes", h.AddBarcode)
|
||||||
|
r.Delete("/api/products/{id}/barcodes/{barcodeID}", h.DeleteBarcode)
|
||||||
|
r.Post("/api/products/{id}/barcodes/{barcodeID}/primary", h.SetPrimaryBarcode)
|
||||||
r.Get("/api/brands", h.ListBrands)
|
r.Get("/api/brands", h.ListBrands)
|
||||||
r.Get("/api/categories", h.ListCategories)
|
r.Get("/api/categories", h.ListCategories)
|
||||||
|
|
||||||
@@ -69,6 +82,10 @@ func (h *Handler) Router() http.Handler {
|
|||||||
r.Get("/api/submissions/{id}", h.GetSubmission)
|
r.Get("/api/submissions/{id}", h.GetSubmission)
|
||||||
r.Post("/api/submissions/{id}/approve", h.ApproveSubmission)
|
r.Post("/api/submissions/{id}/approve", h.ApproveSubmission)
|
||||||
r.Post("/api/submissions/{id}/reject", h.RejectSubmission)
|
r.Post("/api/submissions/{id}/reject", h.RejectSubmission)
|
||||||
|
|
||||||
|
r.Get("/api/keys", h.ListAPIKeys)
|
||||||
|
r.Post("/api/keys", h.CreateAPIKey)
|
||||||
|
r.Delete("/api/keys/{id}", h.RevokeAPIKey)
|
||||||
})
|
})
|
||||||
|
|
||||||
r.Handle("/*", http.HandlerFunc(h.serveSPA))
|
r.Handle("/*", http.HandlerFunc(h.serveSPA))
|
||||||
@@ -231,6 +248,68 @@ func (h *Handler) DeleteMSRP(w http.ResponseWriter, r *http.Request) {
|
|||||||
writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
|
writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------- barcodes ----------
|
||||||
|
|
||||||
|
// AddBarcode validates and attaches a barcode to a product. A code already
|
||||||
|
// owned by another product yields 409 with the conflicting product so the
|
||||||
|
// operator can de-duplicate; an invalid GTIN yields 400.
|
||||||
|
func (h *Handler) AddBarcode(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var in adminstore.BarcodeInput
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
||||||
|
writeError(w, http.StatusBadRequest, "bad_request", "invalid body")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
b, err := h.store.AddBarcode(r.Context(), chi.URLParam(r, "id"), auth.UserFrom(r.Context()), in)
|
||||||
|
if h.handleBarcodeErr(w, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeJSON(w, http.StatusCreated, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteBarcode removes a barcode; a primary one is replaced automatically.
|
||||||
|
func (h *Handler) DeleteBarcode(w http.ResponseWriter, r *http.Request) {
|
||||||
|
err := h.store.DeleteBarcode(r.Context(), chi.URLParam(r, "id"), chi.URLParam(r, "barcodeID"), auth.UserFrom(r.Context()))
|
||||||
|
if h.handleErr(w, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetPrimaryBarcode marks one barcode primary and mirrors it to product.gtin.
|
||||||
|
func (h *Handler) SetPrimaryBarcode(w http.ResponseWriter, r *http.Request) {
|
||||||
|
b, err := h.store.SetPrimaryBarcode(r.Context(), chi.URLParam(r, "id"), chi.URLParam(r, "barcodeID"), auth.UserFrom(r.Context()))
|
||||||
|
if h.handleErr(w, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeJSON(w, http.StatusOK, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleBarcodeErr maps barcode-specific errors (GTIN validation, ownership
|
||||||
|
// conflict) to client-facing statuses, falling back to handleErr otherwise.
|
||||||
|
func (h *Handler) handleBarcodeErr(w http.ResponseWriter, err error) bool {
|
||||||
|
if err == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
var conflict *adminstore.ConflictError
|
||||||
|
if errors.As(err, &conflict) {
|
||||||
|
writeJSON(w, http.StatusConflict, map[string]any{
|
||||||
|
"error": map[string]string{"code": "barcode_conflict", "message": err.Error()},
|
||||||
|
"conflict": map[string]string{
|
||||||
|
"gtin": conflict.GTIN,
|
||||||
|
"product_id": conflict.ProductID,
|
||||||
|
"product_name": conflict.ProductName,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if errors.Is(err, gtin.ErrEmpty) || errors.Is(err, gtin.ErrFormat) ||
|
||||||
|
errors.Is(err, gtin.ErrCheck) || errors.Is(err, gtin.ErrRestricted) {
|
||||||
|
writeError(w, http.StatusBadRequest, "invalid_gtin", err.Error())
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return h.handleErr(w, err)
|
||||||
|
}
|
||||||
|
|
||||||
// ListBrands returns brand options.
|
// ListBrands returns brand options.
|
||||||
func (h *Handler) ListBrands(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) ListBrands(w http.ResponseWriter, r *http.Request) {
|
||||||
items, err := h.store.ListBrands(r.Context())
|
items, err := h.store.ListBrands(r.Context())
|
||||||
|
|||||||
@@ -162,6 +162,7 @@ type ProductDetail struct {
|
|||||||
NutritionBasis *string `json:"nutrition_basis"`
|
NutritionBasis *string `json:"nutrition_basis"`
|
||||||
ServingSize *string `json:"serving_size"`
|
ServingSize *string `json:"serving_size"`
|
||||||
NutriScore *string `json:"nutri_score"`
|
NutriScore *string `json:"nutri_score"`
|
||||||
|
Barcodes []Barcode `json:"barcodes"`
|
||||||
Images []ProductImage `json:"images"`
|
Images []ProductImage `json:"images"`
|
||||||
MSRP []MSRP `json:"msrp"`
|
MSRP []MSRP `json:"msrp"`
|
||||||
Missing []string `json:"missing"`
|
Missing []string `json:"missing"`
|
||||||
@@ -207,6 +208,12 @@ WHERE p.id = $1`, id).Scan(
|
|||||||
d.Additives = []string{}
|
d.Additives = []string{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bcs, err := s.listBarcodes(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
d.Barcodes = bcs
|
||||||
|
|
||||||
imgs, err := s.listImages(ctx, id)
|
imgs, err := s.listImages(ctx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -0,0 +1,130 @@
|
|||||||
|
package adminstore
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5/pgconn"
|
||||||
|
|
||||||
|
"github.com/baicai2026-baicai/goods/api/internal/apikey"
|
||||||
|
)
|
||||||
|
|
||||||
|
// APIKeyRow is an admin-facing view of an issued API key (never the secret).
|
||||||
|
type APIKeyRow struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
KeyPrefix string `json:"key_prefix"`
|
||||||
|
OwnerEmail *string `json:"owner_email"`
|
||||||
|
Tier string `json:"tier"`
|
||||||
|
RateLimitPerMin int `json:"rate_limit_per_min"`
|
||||||
|
RevokedAt *string `json:"revoked_at"`
|
||||||
|
CreatedBy *string `json:"created_by"`
|
||||||
|
CreatedAt string `json:"created_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// APIKeyInput holds the fields accepted when issuing a key.
|
||||||
|
type APIKeyInput struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
OwnerEmail string `json:"owner_email"`
|
||||||
|
Tier string `json:"tier"`
|
||||||
|
RateLimitPerMin int `json:"rate_limit_per_min"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateAPIKey issues a new key, returning the one-time plaintext alongside the
|
||||||
|
// stored row. Only the SHA-256 hash and a short display prefix are persisted.
|
||||||
|
func (s *Store) CreateAPIKey(ctx context.Context, in APIKeyInput, createdBy string) (plaintext string, row APIKeyRow, err error) {
|
||||||
|
tier := in.Tier
|
||||||
|
if tier == "" {
|
||||||
|
tier = "free"
|
||||||
|
}
|
||||||
|
rate := in.RateLimitPerMin
|
||||||
|
if rate <= 0 {
|
||||||
|
rate = 120
|
||||||
|
}
|
||||||
|
var owner *string
|
||||||
|
if e := strings.TrimSpace(in.OwnerEmail); e != "" {
|
||||||
|
owner = &e
|
||||||
|
}
|
||||||
|
|
||||||
|
key, hash, prefix, err := apikey.Generate()
|
||||||
|
if err != nil {
|
||||||
|
return "", row, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var revoked, created *time.Time
|
||||||
|
var createdByOut *string
|
||||||
|
err = s.pool.QueryRow(ctx, `
|
||||||
|
INSERT INTO api_key (name, key_prefix, key_hash, owner_email, tier, rate_limit_per_min, created_by)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||||
|
RETURNING id, name, key_prefix, owner_email, tier, rate_limit_per_min, revoked_at, created_by, created_at`,
|
||||||
|
strings.TrimSpace(in.Name), prefix, hash, owner, tier, rate, createdBy,
|
||||||
|
).Scan(&row.ID, &row.Name, &row.KeyPrefix, &row.OwnerEmail, &row.Tier,
|
||||||
|
&row.RateLimitPerMin, &revoked, &createdByOut, &created)
|
||||||
|
if err != nil {
|
||||||
|
return "", row, err
|
||||||
|
}
|
||||||
|
row.CreatedBy = createdByOut
|
||||||
|
if created != nil {
|
||||||
|
row.CreatedAt = created.Format(time.RFC3339)
|
||||||
|
}
|
||||||
|
return key, row, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListAPIKeys returns all keys (active first, newest first).
|
||||||
|
func (s *Store) ListAPIKeys(ctx context.Context) ([]APIKeyRow, error) {
|
||||||
|
rows, err := s.pool.Query(ctx, `
|
||||||
|
SELECT id, name, key_prefix, owner_email, tier, rate_limit_per_min, revoked_at, created_by, created_at
|
||||||
|
FROM api_key
|
||||||
|
ORDER BY (revoked_at IS NULL) DESC, created_at DESC`)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
out := []APIKeyRow{}
|
||||||
|
for rows.Next() {
|
||||||
|
var r APIKeyRow
|
||||||
|
var revoked, created *time.Time
|
||||||
|
if err := rows.Scan(&r.ID, &r.Name, &r.KeyPrefix, &r.OwnerEmail, &r.Tier,
|
||||||
|
&r.RateLimitPerMin, &revoked, &r.CreatedBy, &created); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if revoked != nil {
|
||||||
|
v := revoked.Format(time.RFC3339)
|
||||||
|
r.RevokedAt = &v
|
||||||
|
}
|
||||||
|
if created != nil {
|
||||||
|
r.CreatedAt = created.Format(time.RFC3339)
|
||||||
|
}
|
||||||
|
out = append(out, r)
|
||||||
|
}
|
||||||
|
return out, rows.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
// RevokeAPIKey marks a key revoked. Revoking an already-revoked or missing key
|
||||||
|
// returns ErrNotFound.
|
||||||
|
func (s *Store) RevokeAPIKey(ctx context.Context, id string) error {
|
||||||
|
tag, err := s.pool.Exec(ctx,
|
||||||
|
"UPDATE api_key SET revoked_at = now() WHERE id = $1 AND revoked_at IS NULL", id)
|
||||||
|
if err != nil {
|
||||||
|
if isInvalidUUID(err) {
|
||||||
|
return ErrNotFound
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if tag.RowsAffected() == 0 {
|
||||||
|
return ErrNotFound
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// isInvalidUUID reports whether err is a Postgres invalid-UUID-text error,
|
||||||
|
// which happens when a non-UUID id is supplied.
|
||||||
|
func isInvalidUUID(err error) bool {
|
||||||
|
var pgErr *pgconn.PgError
|
||||||
|
if errors.As(err, &pgErr) {
|
||||||
|
return pgErr.Code == "22P02"
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
@@ -0,0 +1,270 @@
|
|||||||
|
package adminstore
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5"
|
||||||
|
|
||||||
|
"github.com/baicai2026-baicai/goods/api/internal/gtin"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Barcode is one GS1 trade item number attached to a product.
|
||||||
|
type Barcode struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
GTIN string `json:"gtin"`
|
||||||
|
GTINType string `json:"gtin_type"`
|
||||||
|
PackLevel string `json:"pack_level"`
|
||||||
|
Region *string `json:"region"`
|
||||||
|
IsPrimary bool `json:"is_primary"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// BarcodeInput is the payload for attaching a barcode to a product.
|
||||||
|
type BarcodeInput struct {
|
||||||
|
GTIN string `json:"gtin"`
|
||||||
|
GTINType string `json:"gtin_type"`
|
||||||
|
PackLevel string `json:"pack_level"`
|
||||||
|
Region *string `json:"region"`
|
||||||
|
IsPrimary bool `json:"is_primary"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConflictError signals that a barcode is already attached to another product,
|
||||||
|
// so the operator must de-duplicate instead of creating a clash.
|
||||||
|
type ConflictError struct {
|
||||||
|
GTIN string
|
||||||
|
ProductID string
|
||||||
|
ProductName string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *ConflictError) Error() string { return "条码已被其他商品占用:" + e.GTIN }
|
||||||
|
|
||||||
|
func validPackLevel(p string) string {
|
||||||
|
switch p {
|
||||||
|
case "each", "case", "pallet":
|
||||||
|
return p
|
||||||
|
default:
|
||||||
|
return "each"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func validGTINType(t, normalized string) string {
|
||||||
|
switch t {
|
||||||
|
case "EAN8", "UPC", "EAN13", "ITF14", "GTIN14":
|
||||||
|
return t
|
||||||
|
default:
|
||||||
|
return gtin.InferType(normalized)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Store) listBarcodes(ctx context.Context, productID string) ([]Barcode, error) {
|
||||||
|
rows, err := s.pool.Query(ctx,
|
||||||
|
`SELECT id, gtin, gtin_type, pack_level, region, is_primary
|
||||||
|
FROM product_barcode WHERE product_id = $1
|
||||||
|
ORDER BY is_primary DESC, gtin`, productID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
out := []Barcode{}
|
||||||
|
for rows.Next() {
|
||||||
|
var b Barcode
|
||||||
|
if err := rows.Scan(&b.ID, &b.GTIN, &b.GTINType, &b.PackLevel, &b.Region, &b.IsPrimary); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out = append(out, b)
|
||||||
|
}
|
||||||
|
return out, rows.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
// barcodeOwner returns the product currently owning a barcode, if any.
|
||||||
|
func barcodeOwner(ctx context.Context, q pgx.Tx, code string) (productID, productName string, found bool, err error) {
|
||||||
|
err = q.QueryRow(ctx,
|
||||||
|
`SELECT pb.product_id, p.name FROM product_barcode pb
|
||||||
|
JOIN product p ON p.id = pb.product_id WHERE pb.gtin = $1`, code).
|
||||||
|
Scan(&productID, &productName)
|
||||||
|
if errors.Is(err, pgx.ErrNoRows) {
|
||||||
|
return "", "", false, nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return "", "", false, err
|
||||||
|
}
|
||||||
|
return productID, productName, true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddBarcode validates and attaches a barcode to a product, recording audit.
|
||||||
|
// A barcode already owned by another product yields a *ConflictError.
|
||||||
|
func (s *Store) AddBarcode(ctx context.Context, productID, actor string, in BarcodeInput) (*Barcode, error) {
|
||||||
|
code, err := gtin.Normalize(in.GTIN)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
tx, err := s.pool.Begin(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer tx.Rollback(ctx)
|
||||||
|
|
||||||
|
// Product must exist.
|
||||||
|
var exists bool
|
||||||
|
if err := tx.QueryRow(ctx, "SELECT EXISTS(SELECT 1 FROM product WHERE id=$1)", productID).Scan(&exists); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !exists {
|
||||||
|
return nil, ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
// Globally unique: a barcode owned by any product (this one included)
|
||||||
|
// is a conflict the operator must resolve by de-duplicating.
|
||||||
|
if owner, name, found, err := barcodeOwner(ctx, tx, code); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else if found {
|
||||||
|
return nil, &ConflictError{GTIN: code, ProductID: owner, ProductName: name}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make this the primary barcode when requested or when none exists yet.
|
||||||
|
makePrimary := in.IsPrimary
|
||||||
|
if !makePrimary {
|
||||||
|
var hasPrimary bool
|
||||||
|
if err := tx.QueryRow(ctx,
|
||||||
|
"SELECT EXISTS(SELECT 1 FROM product_barcode WHERE product_id=$1 AND is_primary)", productID).
|
||||||
|
Scan(&hasPrimary); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
makePrimary = !hasPrimary
|
||||||
|
}
|
||||||
|
if makePrimary {
|
||||||
|
if _, err := tx.Exec(ctx,
|
||||||
|
"UPDATE product_barcode SET is_primary=false WHERE product_id=$1 AND is_primary", productID); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
srcID, _ := s.manualSourceID(ctx, tx)
|
||||||
|
var srcArg any
|
||||||
|
if srcID != "" {
|
||||||
|
srcArg = srcID
|
||||||
|
}
|
||||||
|
|
||||||
|
var b Barcode
|
||||||
|
err = tx.QueryRow(ctx, `
|
||||||
|
INSERT INTO product_barcode (product_id, gtin, gtin_type, pack_level, region, is_primary, source_id)
|
||||||
|
VALUES ($1,$2,$3,$4,$5,$6,$7)
|
||||||
|
RETURNING id, gtin, gtin_type, pack_level, region, is_primary`,
|
||||||
|
productID, code, validGTINType(in.GTINType, code), validPackLevel(in.PackLevel),
|
||||||
|
in.Region, makePrimary, srcArg).
|
||||||
|
Scan(&b.ID, &b.GTIN, &b.GTINType, &b.PackLevel, &b.Region, &b.IsPrimary)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if makePrimary {
|
||||||
|
if _, err := tx.Exec(ctx, "UPDATE product SET gtin=$2 WHERE id=$1", productID, code); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if _, err := s.recomputeQualityTx(ctx, tx, productID); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := tx.Commit(ctx); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = s.writeAudit(ctx, actor, "add_barcode", "product", &productID, []string{"gtin"}, nil, b)
|
||||||
|
return &b, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteBarcode removes a barcode; if it was primary, another is promoted.
|
||||||
|
func (s *Store) DeleteBarcode(ctx context.Context, productID, barcodeID, actor string) error {
|
||||||
|
tx, err := s.pool.Begin(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer tx.Rollback(ctx)
|
||||||
|
|
||||||
|
var code string
|
||||||
|
var wasPrimary bool
|
||||||
|
err = tx.QueryRow(ctx,
|
||||||
|
"DELETE FROM product_barcode WHERE id=$1 AND product_id=$2 RETURNING gtin, is_primary",
|
||||||
|
barcodeID, productID).Scan(&code, &wasPrimary)
|
||||||
|
if errors.Is(err, pgx.ErrNoRows) {
|
||||||
|
return ErrNotFound
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if wasPrimary {
|
||||||
|
var newID, newGTIN string
|
||||||
|
e := tx.QueryRow(ctx,
|
||||||
|
"SELECT id, gtin FROM product_barcode WHERE product_id=$1 ORDER BY gtin LIMIT 1", productID).
|
||||||
|
Scan(&newID, &newGTIN)
|
||||||
|
if e == nil {
|
||||||
|
if _, err := tx.Exec(ctx, "UPDATE product_barcode SET is_primary=true WHERE id=$1", newID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := tx.Exec(ctx, "UPDATE product SET gtin=$2 WHERE id=$1", productID, newGTIN); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else if errors.Is(e, pgx.ErrNoRows) {
|
||||||
|
if _, err := tx.Exec(ctx, "UPDATE product SET gtin=NULL WHERE id=$1", productID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := s.recomputeQualityTx(ctx, tx, productID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := tx.Commit(ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_ = s.writeAudit(ctx, actor, "delete_barcode", "product", &productID, []string{"gtin"},
|
||||||
|
map[string]string{"gtin": code}, nil)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetPrimaryBarcode marks one barcode primary and mirrors it to product.gtin.
|
||||||
|
func (s *Store) SetPrimaryBarcode(ctx context.Context, productID, barcodeID, actor string) (*Barcode, error) {
|
||||||
|
tx, err := s.pool.Begin(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer tx.Rollback(ctx)
|
||||||
|
|
||||||
|
var code string
|
||||||
|
err = tx.QueryRow(ctx, "SELECT gtin FROM product_barcode WHERE id=$1 AND product_id=$2", barcodeID, productID).Scan(&code)
|
||||||
|
if errors.Is(err, pgx.ErrNoRows) {
|
||||||
|
return nil, ErrNotFound
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if _, err := tx.Exec(ctx, "UPDATE product_barcode SET is_primary=false WHERE product_id=$1 AND is_primary", productID); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if _, err := tx.Exec(ctx, "UPDATE product_barcode SET is_primary=true WHERE id=$1", barcodeID); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if _, err := tx.Exec(ctx, "UPDATE product SET gtin=$2 WHERE id=$1", productID, code); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := tx.Commit(ctx); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
_ = s.writeAudit(ctx, actor, "set_primary_barcode", "product", &productID, []string{"gtin"}, nil,
|
||||||
|
map[string]string{"gtin": code})
|
||||||
|
|
||||||
|
bcs, err := s.listBarcodes(ctx, productID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for i := range bcs {
|
||||||
|
if bcs[i].ID == barcodeID {
|
||||||
|
return &bcs[i], nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, ErrNotFound
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
// Package apikey handles generation and hashing of public-API keys.
|
||||||
|
//
|
||||||
|
// A key looks like "og_live_<random>". Only the SHA-256 hash is ever persisted;
|
||||||
|
// the plaintext is returned once at creation time and cannot be recovered.
|
||||||
|
package apikey
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/hex"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Prefix is the human-readable scheme prefix every key carries.
|
||||||
|
const Prefix = "og_live_"
|
||||||
|
|
||||||
|
// prefixLen is how many leading characters (including Prefix) are stored in
|
||||||
|
// api_key.key_prefix for identifying a key without revealing its secret.
|
||||||
|
const prefixLen = 12
|
||||||
|
|
||||||
|
// Generate returns a new random key (plaintext), its SHA-256 hash, and a short
|
||||||
|
// display prefix. The plaintext must be shown to the caller exactly once.
|
||||||
|
func Generate() (key, hash, displayPrefix string, err error) {
|
||||||
|
buf := make([]byte, 24)
|
||||||
|
if _, err = rand.Read(buf); err != nil {
|
||||||
|
return "", "", "", err
|
||||||
|
}
|
||||||
|
// URL-safe, no padding => stable, copy-pasteable token body.
|
||||||
|
body := base64.RawURLEncoding.EncodeToString(buf)
|
||||||
|
key = Prefix + body
|
||||||
|
hash = Hash(key)
|
||||||
|
displayPrefix = key
|
||||||
|
if len(displayPrefix) > prefixLen {
|
||||||
|
displayPrefix = displayPrefix[:prefixLen]
|
||||||
|
}
|
||||||
|
return key, hash, displayPrefix, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hash returns the hex-encoded SHA-256 of a key, used for storage and lookup.
|
||||||
|
func Hash(key string) string {
|
||||||
|
sum := sha256.Sum256([]byte(strings.TrimSpace(key)))
|
||||||
|
return hex.EncodeToString(sum[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
// Looks like a key issued by this service (cheap pre-check before hashing).
|
||||||
|
func IsWellFormed(key string) bool {
|
||||||
|
return strings.HasPrefix(key, Prefix) && len(key) > len(Prefix)+8
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package apikey
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestGenerate(t *testing.T) {
|
||||||
|
key, hash, prefix, err := Generate()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Generate: %v", err)
|
||||||
|
}
|
||||||
|
if !IsWellFormed(key) {
|
||||||
|
t.Fatalf("generated key not well-formed: %q", key)
|
||||||
|
}
|
||||||
|
if Hash(key) != hash {
|
||||||
|
t.Fatalf("Hash(key) != returned hash")
|
||||||
|
}
|
||||||
|
if len(prefix) != prefixLen || key[:prefixLen] != prefix {
|
||||||
|
t.Fatalf("prefix %q not a %d-char prefix of key %q", prefix, prefixLen, key)
|
||||||
|
}
|
||||||
|
if len(hash) != 64 {
|
||||||
|
t.Fatalf("hash not hex sha-256: %q", hash)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGenerateUnique(t *testing.T) {
|
||||||
|
seen := map[string]bool{}
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
k, _, _, err := Generate()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if seen[k] {
|
||||||
|
t.Fatalf("duplicate key generated: %q", k)
|
||||||
|
}
|
||||||
|
seen[k] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHashStableAndTrimmed(t *testing.T) {
|
||||||
|
if Hash("og_live_abc") != Hash(" og_live_abc ") {
|
||||||
|
t.Fatal("Hash should ignore surrounding whitespace")
|
||||||
|
}
|
||||||
|
if Hash("a") == Hash("b") {
|
||||||
|
t.Fatal("distinct inputs must hash differently")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsWellFormed(t *testing.T) {
|
||||||
|
cases := map[string]bool{
|
||||||
|
"og_live_abcdefghijkl": true, // body longer than 8 chars
|
||||||
|
"og_live_": false, // empty body
|
||||||
|
"og_live_abc": false, // body too short
|
||||||
|
"nope_abcdefghijkl": false, // wrong prefix
|
||||||
|
"": false,
|
||||||
|
}
|
||||||
|
for in, want := range cases {
|
||||||
|
if got := IsWellFormed(in); got != want {
|
||||||
|
t.Errorf("IsWellFormed(%q) = %v, want %v", in, got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,26 +2,38 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config holds runtime configuration for the OpenGoods API server.
|
// Config holds runtime configuration for the OpenGoods API server.
|
||||||
// Values are read from environment variables with sensible defaults so the
|
// Values are read from environment variables with sensible defaults so the
|
||||||
// server can boot in a local Docker Compose setup without extra configuration.
|
// server can boot in a local Docker Compose setup without extra configuration.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Addr string
|
Addr string
|
||||||
DatabaseURL string
|
DatabaseURL string
|
||||||
RedisURL string
|
RedisURL string
|
||||||
|
AnonRateLimitPerMin int
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load reads configuration from the environment.
|
// Load reads configuration from the environment.
|
||||||
func Load() Config {
|
func Load() Config {
|
||||||
return Config{
|
return Config{
|
||||||
Addr: getenv("OPENGOODS_ADDR", ":8080"),
|
Addr: getenv("OPENGOODS_ADDR", ":8080"),
|
||||||
DatabaseURL: getenv("OPENGOODS_DATABASE_URL", "postgres://opengoods:opengoods@localhost:5432/opengoods?sslmode=disable"),
|
DatabaseURL: getenv("OPENGOODS_DATABASE_URL", "postgres://opengoods:opengoods@localhost:5432/opengoods?sslmode=disable"),
|
||||||
RedisURL: getenv("OPENGOODS_REDIS_URL", "redis://localhost:6379/0"),
|
RedisURL: getenv("OPENGOODS_REDIS_URL", "redis://localhost:6379/0"),
|
||||||
|
AnonRateLimitPerMin: getenvInt("OPENGOODS_ANON_RATE_LIMIT_PER_MIN", 60),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getenvInt(key string, fallback int) int {
|
||||||
|
if v, ok := os.LookupEnv(key); ok && v != "" {
|
||||||
|
if n, err := strconv.Atoi(v); err == nil && n > 0 {
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fallback
|
||||||
|
}
|
||||||
|
|
||||||
func getenv(key, fallback string) string {
|
func getenv(key, fallback string) string {
|
||||||
if v, ok := os.LookupEnv(key); ok && v != "" {
|
if v, ok := os.LookupEnv(key); ok && v != "" {
|
||||||
return v
|
return v
|
||||||
|
|||||||
@@ -0,0 +1,110 @@
|
|||||||
|
// Package gtin validates and normalizes GS1 trade item numbers (GTIN-8/12/13/14).
|
||||||
|
// Only globally-unique GS1 codes are accepted: store-internal / variable-weight /
|
||||||
|
// coupon codes (which are not globally unique) are rejected on purpose.
|
||||||
|
package gtin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Validation errors.
|
||||||
|
var (
|
||||||
|
ErrEmpty = errors.New("条码不能为空")
|
||||||
|
ErrFormat = errors.New("条码必须为 8/12/13/14 位数字")
|
||||||
|
ErrCheck = errors.New("条码校验位不正确")
|
||||||
|
ErrRestricted = errors.New("店内码/变量重量码/优惠券码等非全球唯一码,不予收录")
|
||||||
|
)
|
||||||
|
|
||||||
|
// Normalize trims and validates a GTIN, returning the cleaned digit string.
|
||||||
|
// It enforces length, the GS1 mod-10 check digit, and rejects restricted
|
||||||
|
// (non-globally-unique) number ranges.
|
||||||
|
func Normalize(raw string) (string, error) {
|
||||||
|
s := strings.TrimSpace(raw)
|
||||||
|
if s == "" {
|
||||||
|
return "", ErrEmpty
|
||||||
|
}
|
||||||
|
for _, c := range s {
|
||||||
|
if c < '0' || c > '9' {
|
||||||
|
return "", ErrFormat
|
||||||
|
}
|
||||||
|
}
|
||||||
|
switch len(s) {
|
||||||
|
case 8, 12, 13, 14:
|
||||||
|
default:
|
||||||
|
return "", ErrFormat
|
||||||
|
}
|
||||||
|
if !validCheckDigit(s) {
|
||||||
|
return "", ErrCheck
|
||||||
|
}
|
||||||
|
if restricted(s) {
|
||||||
|
return "", ErrRestricted
|
||||||
|
}
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// InferType returns the conventional GTIN type label for a normalized code.
|
||||||
|
func InferType(s string) string {
|
||||||
|
switch len(s) {
|
||||||
|
case 8:
|
||||||
|
return "EAN8"
|
||||||
|
case 12:
|
||||||
|
return "UPC"
|
||||||
|
case 14:
|
||||||
|
return "GTIN14"
|
||||||
|
default:
|
||||||
|
return "EAN13"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// validCheckDigit verifies the trailing GS1 mod-10 check digit. The digit
|
||||||
|
// immediately left of the check digit carries weight 3, then weights alternate.
|
||||||
|
func validCheckDigit(s string) bool {
|
||||||
|
n := len(s)
|
||||||
|
sum := 0
|
||||||
|
for i := 0; i < n-1; i++ {
|
||||||
|
d := int(s[i] - '0')
|
||||||
|
if (n-1-i)%2 == 1 {
|
||||||
|
sum += d * 3
|
||||||
|
} else {
|
||||||
|
sum += d
|
||||||
|
}
|
||||||
|
}
|
||||||
|
check := (10 - (sum % 10)) % 10
|
||||||
|
return check == int(s[n-1]-'0')
|
||||||
|
}
|
||||||
|
|
||||||
|
// restricted reports whether a (length/check-digit valid) code falls in a
|
||||||
|
// number range reserved for non-globally-unique use.
|
||||||
|
func restricted(s string) bool {
|
||||||
|
switch len(s) {
|
||||||
|
case 13:
|
||||||
|
p2 := s[:2]
|
||||||
|
switch {
|
||||||
|
case s[0] == '2': // 20-29 restricted distribution / in-store
|
||||||
|
return true
|
||||||
|
case p2 == "02": // 020-029 variable-measure within a store
|
||||||
|
return true
|
||||||
|
case p2 == "04": // 040-049 restricted circulation within a company
|
||||||
|
return true
|
||||||
|
case p2 == "05": // 050-059 coupons
|
||||||
|
return true
|
||||||
|
case p2 == "98" || p2 == "99": // 980-989/99 coupons & refund receipts
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case 12: // UPC-A: leading number-system digit
|
||||||
|
switch s[0] {
|
||||||
|
case '2': // in-store / random weight
|
||||||
|
return true
|
||||||
|
case '4': // unrestricted in-store use
|
||||||
|
return true
|
||||||
|
case '5': // coupons
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case 8: // EAN-8: 0/2 prefixes reserved for in-store use
|
||||||
|
if s[0] == '0' || s[0] == '2' {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package gtin
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestNormalizeValid(t *testing.T) {
|
||||||
|
cases := []struct{ in, want, typ string }{
|
||||||
|
{" 5449000000996 ", "5449000000996", "EAN13"}, // Coca-Cola EAN-13
|
||||||
|
{"3017624010701", "3017624010701", "EAN13"}, // Nutella EAN-13
|
||||||
|
{"036000291452", "036000291452", "UPC"}, // UPC-A
|
||||||
|
{"96385074", "96385074", "EAN8"}, // EAN-8
|
||||||
|
{"00012345600012", "00012345600012", "GTIN14"},
|
||||||
|
{"6901234567892", "6901234567892", "EAN13"}, // China 690 prefix
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
got, err := Normalize(c.in)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Normalize(%q) unexpected error: %v", c.in, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if got != c.want {
|
||||||
|
t.Errorf("Normalize(%q) = %q, want %q", c.in, got, c.want)
|
||||||
|
}
|
||||||
|
if InferType(got) != c.typ {
|
||||||
|
t.Errorf("InferType(%q) = %q, want %q", got, InferType(got), c.typ)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNormalizeRejects(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
in string
|
||||||
|
want error
|
||||||
|
}{
|
||||||
|
{"", ErrEmpty},
|
||||||
|
{"12ab5678", ErrFormat},
|
||||||
|
{"12345", ErrFormat},
|
||||||
|
{"5449000000997", ErrCheck}, // bad check digit
|
||||||
|
{"2012345678903", ErrRestricted}, // 20-29 in-store EAN-13
|
||||||
|
{"0212345678909", ErrRestricted}, // 02x variable measure
|
||||||
|
{"212345678909", ErrRestricted}, // UPC number system 2
|
||||||
|
{"02345673", ErrRestricted}, // EAN-8 in-store
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
_, err := Normalize(c.in)
|
||||||
|
if err != c.want {
|
||||||
|
t.Errorf("Normalize(%q) error = %v, want %v", c.in, err, c.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,6 +15,7 @@ import (
|
|||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"github.com/go-chi/chi/v5/middleware"
|
"github.com/go-chi/chi/v5/middleware"
|
||||||
|
|
||||||
|
"github.com/baicai2026-baicai/goods/api/internal/ratelimit"
|
||||||
"github.com/baicai2026-baicai/goods/api/internal/store"
|
"github.com/baicai2026-baicai/goods/api/internal/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -24,17 +25,35 @@ const APIVersion = "v1"
|
|||||||
const (
|
const (
|
||||||
defaultPageSize = 20
|
defaultPageSize = 20
|
||||||
maxPageSize = 100
|
maxPageSize = 100
|
||||||
|
|
||||||
|
// defaultAnonLimit is the per-minute request budget for unauthenticated
|
||||||
|
// callers (identified by client IP) when none is configured.
|
||||||
|
defaultAnonLimit = 60
|
||||||
)
|
)
|
||||||
|
|
||||||
// Handler holds dependencies shared by the HTTP routes.
|
// Handler holds dependencies shared by the HTTP routes.
|
||||||
type Handler struct {
|
type Handler struct {
|
||||||
store *store.Store
|
store *store.Store
|
||||||
spa fs.FS
|
spa fs.FS
|
||||||
|
limiter *ratelimit.Limiter
|
||||||
|
anonLimit int
|
||||||
}
|
}
|
||||||
|
|
||||||
// New constructs a Handler backed by the given store. spa may be nil (JSON-only).
|
// New constructs a Handler backed by the given store. spa may be nil (JSON-only).
|
||||||
|
// Rate limiting is disabled until WithRateLimit is called.
|
||||||
func New(s *store.Store, spa fs.FS) *Handler {
|
func New(s *store.Store, spa fs.FS) *Handler {
|
||||||
return &Handler{store: s, spa: spa}
|
return &Handler{store: s, spa: spa, anonLimit: defaultAnonLimit}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithRateLimit attaches a Redis-backed limiter and the anonymous per-minute
|
||||||
|
// budget, enabling rate limiting + usage tracking on the public API routes.
|
||||||
|
// A non-positive anonPerMin keeps the default.
|
||||||
|
func (h *Handler) WithRateLimit(l *ratelimit.Limiter, anonPerMin int) *Handler {
|
||||||
|
h.limiter = l
|
||||||
|
if anonPerMin > 0 {
|
||||||
|
h.anonLimit = anonPerMin
|
||||||
|
}
|
||||||
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
// Router builds the top-level HTTP handler with middleware and routes mounted.
|
// Router builds the top-level HTTP handler with middleware and routes mounted.
|
||||||
@@ -47,6 +66,7 @@ func (h *Handler) Router() http.Handler {
|
|||||||
r.Get("/healthz", h.Healthz)
|
r.Get("/healthz", h.Healthz)
|
||||||
|
|
||||||
r.Route("/api/"+APIVersion, func(r chi.Router) {
|
r.Route("/api/"+APIVersion, func(r chi.Router) {
|
||||||
|
r.Use(h.rateLimit)
|
||||||
r.Route("/products", func(r chi.Router) {
|
r.Route("/products", func(r chi.Router) {
|
||||||
r.Get("/barcode/{gtin}", h.ProductByBarcode)
|
r.Get("/barcode/{gtin}", h.ProductByBarcode)
|
||||||
r.Get("/search", h.SearchProducts)
|
r.Get("/search", h.SearchProducts)
|
||||||
|
|||||||
@@ -0,0 +1,90 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/baicai2026-baicai/goods/api/internal/apikey"
|
||||||
|
"github.com/baicai2026-baicai/goods/api/internal/store"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ctxKey int
|
||||||
|
|
||||||
|
const apiKeyIDKey ctxKey = 0
|
||||||
|
|
||||||
|
// rateLimit authenticates an optional API key and enforces a per-minute budget
|
||||||
|
// on the public API. Anonymous callers are limited by client IP at a lower
|
||||||
|
// budget; a valid key raises the budget and attributes usage. An API key that
|
||||||
|
// is present but invalid or revoked is rejected with 401. Rate-limit headers
|
||||||
|
// are set on every response; over-budget callers get 429 + Retry-After.
|
||||||
|
func (h *Handler) rateLimit(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
id := "ip:" + clientIP(r)
|
||||||
|
limit := h.anonLimit
|
||||||
|
keyID := ""
|
||||||
|
|
||||||
|
if raw := presentedKey(r); raw != "" {
|
||||||
|
if !apikey.IsWellFormed(raw) {
|
||||||
|
writeError(w, r, http.StatusUnauthorized, "invalid_api_key", "API key 格式无效")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
k, err := h.store.APIKeyByHash(r.Context(), apikey.Hash(raw))
|
||||||
|
if errors.Is(err, store.ErrNotFound) {
|
||||||
|
writeError(w, r, http.StatusUnauthorized, "invalid_api_key", "API key 无效或已吊销")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, r, http.StatusInternalServerError, "internal_error", "internal server error")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
keyID = k.ID
|
||||||
|
limit = k.RateLimitPerMin
|
||||||
|
id = "key:" + k.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
res := h.limiter.Allow(r.Context(), id, limit, time.Minute)
|
||||||
|
w.Header().Set("X-RateLimit-Limit", strconv.Itoa(res.Limit))
|
||||||
|
w.Header().Set("X-RateLimit-Remaining", strconv.Itoa(res.Remaining))
|
||||||
|
w.Header().Set("X-RateLimit-Reset", strconv.FormatInt(res.ResetUnix, 10))
|
||||||
|
if !res.Allowed {
|
||||||
|
retry := res.ResetUnix - time.Now().Unix()
|
||||||
|
if retry < 1 {
|
||||||
|
retry = 1
|
||||||
|
}
|
||||||
|
w.Header().Set("Retry-After", strconv.FormatInt(retry, 10))
|
||||||
|
writeError(w, r, http.StatusTooManyRequests, "rate_limited", "请求过于频繁,请稍后再试")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if keyID != "" {
|
||||||
|
h.limiter.RecordUsage(r.Context(), keyID)
|
||||||
|
next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), apiKeyIDKey, keyID)))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// presentedKey extracts an API key from the X-API-Key header or a Bearer token.
|
||||||
|
func presentedKey(r *http.Request) string {
|
||||||
|
if v := strings.TrimSpace(r.Header.Get("X-API-Key")); v != "" {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
if v := r.Header.Get("Authorization"); strings.HasPrefix(v, "Bearer ") {
|
||||||
|
return strings.TrimSpace(strings.TrimPrefix(v, "Bearer "))
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// clientIP returns the caller IP, preferring chi's RealIP-normalized RemoteAddr.
|
||||||
|
func clientIP(r *http.Request) string {
|
||||||
|
if host, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
|
||||||
|
return host
|
||||||
|
}
|
||||||
|
return r.RemoteAddr
|
||||||
|
}
|
||||||
@@ -0,0 +1,120 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
|
||||||
|
"github.com/baicai2026-baicai/goods/api/internal/apikey"
|
||||||
|
"github.com/baicai2026-baicai/goods/api/internal/ratelimit"
|
||||||
|
"github.com/baicai2026-baicai/goods/api/internal/store"
|
||||||
|
)
|
||||||
|
|
||||||
|
// newRateLimitedHandler builds a handler backed by the test DB and a live Redis
|
||||||
|
// limiter, plus a freshly issued API key with the given per-minute limit. It
|
||||||
|
// skips when either backend is unavailable.
|
||||||
|
func newRateLimitedHandler(t *testing.T, keyLimit int) (h *Handler, plaintextKey string) {
|
||||||
|
t.Helper()
|
||||||
|
dsn := os.Getenv("OPENGOODS_DATABASE_URL")
|
||||||
|
if dsn == "" {
|
||||||
|
dsn = "postgres://opengoods:opengoods@localhost:5432/opengoods?sslmode=disable"
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
pool, err := pgxpool.New(ctx, dsn)
|
||||||
|
if err != nil {
|
||||||
|
t.Skipf("no database: %v", err)
|
||||||
|
}
|
||||||
|
if err := pool.Ping(ctx); err != nil {
|
||||||
|
pool.Close()
|
||||||
|
t.Skipf("database not reachable: %v", err)
|
||||||
|
}
|
||||||
|
var hasTable bool
|
||||||
|
if err := pool.QueryRow(ctx, "SELECT to_regclass('public.api_key') IS NOT NULL").Scan(&hasTable); err != nil || !hasTable {
|
||||||
|
pool.Close()
|
||||||
|
t.Skip("migrations not applied (api_key missing)")
|
||||||
|
}
|
||||||
|
|
||||||
|
redisURL := os.Getenv("OPENGOODS_REDIS_URL")
|
||||||
|
if redisURL == "" {
|
||||||
|
redisURL = "redis://localhost:6379/0"
|
||||||
|
}
|
||||||
|
limiter := ratelimit.New(redisURL)
|
||||||
|
pingCtx, pingCancel := context.WithTimeout(context.Background(), time.Second)
|
||||||
|
defer pingCancel()
|
||||||
|
if err := limiter.Ping(pingCtx); err != nil {
|
||||||
|
pool.Close()
|
||||||
|
t.Skipf("redis not reachable: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
key, hash, prefix, err := apikey.Generate()
|
||||||
|
if err != nil {
|
||||||
|
pool.Close()
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
name := fmt.Sprintf("test-key-%d", time.Now().UnixNano())
|
||||||
|
if _, err := pool.Exec(context.Background(),
|
||||||
|
`INSERT INTO api_key (name, key_prefix, key_hash, rate_limit_per_min) VALUES ($1,$2,$3,$4)`,
|
||||||
|
name, prefix, hash, keyLimit); err != nil {
|
||||||
|
pool.Close()
|
||||||
|
t.Fatalf("insert api_key: %v", err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() {
|
||||||
|
_, _ = pool.Exec(context.Background(), "DELETE FROM api_key WHERE key_hash=$1", hash)
|
||||||
|
pool.Close()
|
||||||
|
})
|
||||||
|
|
||||||
|
return New(store.New(pool), nil).WithRateLimit(limiter, 60), key
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRateLimitHeadersAndKeyAuth(t *testing.T) {
|
||||||
|
h, key := newRateLimitedHandler(t, 100)
|
||||||
|
req := httptest.NewRequest(http.MethodGet, "/api/"+APIVersion+"/categories", nil)
|
||||||
|
req.Header.Set("X-API-Key", key)
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
h.Router().ServeHTTP(rec, req)
|
||||||
|
|
||||||
|
if rec.Code != http.StatusOK {
|
||||||
|
t.Fatalf("status = %d, body = %s", rec.Code, rec.Body.String())
|
||||||
|
}
|
||||||
|
if got := rec.Header().Get("X-RateLimit-Limit"); got != "100" {
|
||||||
|
t.Fatalf("X-RateLimit-Limit = %q, want 100 (key limit)", got)
|
||||||
|
}
|
||||||
|
if rec.Header().Get("X-RateLimit-Remaining") == "" {
|
||||||
|
t.Fatal("missing X-RateLimit-Remaining header")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInvalidKeyRejected(t *testing.T) {
|
||||||
|
h, _ := newRateLimitedHandler(t, 100)
|
||||||
|
req := httptest.NewRequest(http.MethodGet, "/api/"+APIVersion+"/categories", nil)
|
||||||
|
req.Header.Set("X-API-Key", "og_live_thiskeydoesnotexist123456")
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
h.Router().ServeHTTP(rec, req)
|
||||||
|
if rec.Code != http.StatusUnauthorized {
|
||||||
|
t.Fatalf("status = %d, want 401; body = %s", rec.Code, rec.Body.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRateLimitExceeded(t *testing.T) {
|
||||||
|
h, key := newRateLimitedHandler(t, 1)
|
||||||
|
do := func() int {
|
||||||
|
req := httptest.NewRequest(http.MethodGet, "/api/"+APIVersion+"/categories", nil)
|
||||||
|
req.Header.Set("X-API-Key", key)
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
h.Router().ServeHTTP(rec, req)
|
||||||
|
return rec.Code
|
||||||
|
}
|
||||||
|
if code := do(); code != http.StatusOK {
|
||||||
|
t.Fatalf("first request status = %d, want 200", code)
|
||||||
|
}
|
||||||
|
if code := do(); code != http.StatusTooManyRequests {
|
||||||
|
t.Fatalf("second request status = %d, want 429", code)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,132 @@
|
|||||||
|
// Package ratelimit provides a Redis-backed fixed-window rate limiter and
|
||||||
|
// lightweight per-key usage counters for the public API.
|
||||||
|
//
|
||||||
|
// All state lives in Redis so it is shared across API replicas and visible to
|
||||||
|
// the admin console, and so the public server keeps its read-only contract
|
||||||
|
// against PostgreSQL. Every operation fails open: if Redis is unavailable the
|
||||||
|
// limiter allows the request rather than taking the API down.
|
||||||
|
package ratelimit
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Limiter throttles callers and records usage. A nil-backed Limiter (when Redis
|
||||||
|
// could not be configured) disables limiting and usage tracking.
|
||||||
|
type Limiter struct {
|
||||||
|
rdb *redis.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
// Result describes the outcome of an Allow check and the headers to surface.
|
||||||
|
type Result struct {
|
||||||
|
Allowed bool
|
||||||
|
Limit int
|
||||||
|
Remaining int
|
||||||
|
ResetUnix int64
|
||||||
|
}
|
||||||
|
|
||||||
|
// UsageStat is the aggregated usage for a single API key.
|
||||||
|
type UsageStat struct {
|
||||||
|
Total int64 `json:"total"`
|
||||||
|
Today int64 `json:"today"`
|
||||||
|
LastUsedAt *int64 `json:"last_used_at,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// New builds a Limiter from a redis:// URL. On a parse error it logs and returns
|
||||||
|
// a fail-open limiter (Redis disabled) so the server still boots.
|
||||||
|
func New(redisURL string) *Limiter {
|
||||||
|
opt, err := redis.ParseURL(redisURL)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("ratelimit: invalid redis url %q: %v (rate limiting disabled)", redisURL, err)
|
||||||
|
return &Limiter{}
|
||||||
|
}
|
||||||
|
return &Limiter{rdb: redis.NewClient(opt)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enabled reports whether a Redis backend is configured.
|
||||||
|
func (l *Limiter) Enabled() bool { return l != nil && l.rdb != nil }
|
||||||
|
|
||||||
|
// Ping verifies the Redis backend is reachable. Returns an error if disabled or
|
||||||
|
// unreachable.
|
||||||
|
func (l *Limiter) Ping(ctx context.Context) error {
|
||||||
|
if !l.Enabled() {
|
||||||
|
return redis.ErrClosed
|
||||||
|
}
|
||||||
|
return l.rdb.Ping(ctx).Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allow records a hit for id within a fixed window and reports whether the
|
||||||
|
// caller is under limit. Fails open (Allowed=true) on any Redis error.
|
||||||
|
func (l *Limiter) Allow(ctx context.Context, id string, limit int, window time.Duration) Result {
|
||||||
|
reset := func() int64 {
|
||||||
|
win := int64(window / time.Second)
|
||||||
|
if win < 1 {
|
||||||
|
win = 1
|
||||||
|
}
|
||||||
|
return (time.Now().Unix()/win + 1) * win
|
||||||
|
}
|
||||||
|
if !l.Enabled() {
|
||||||
|
return Result{Allowed: true, Limit: limit, Remaining: limit, ResetUnix: reset()}
|
||||||
|
}
|
||||||
|
win := int64(window / time.Second)
|
||||||
|
if win < 1 {
|
||||||
|
win = 1
|
||||||
|
}
|
||||||
|
bucket := time.Now().Unix() / win
|
||||||
|
key := fmt.Sprintf("rl:%s:%d", id, bucket)
|
||||||
|
|
||||||
|
n, err := l.rdb.Incr(ctx, key).Result()
|
||||||
|
if err != nil {
|
||||||
|
return Result{Allowed: true, Limit: limit, Remaining: limit, ResetUnix: (bucket + 1) * win}
|
||||||
|
}
|
||||||
|
if n == 1 {
|
||||||
|
l.rdb.Expire(ctx, key, time.Duration(win)*time.Second)
|
||||||
|
}
|
||||||
|
remaining := limit - int(n)
|
||||||
|
if remaining < 0 {
|
||||||
|
remaining = 0
|
||||||
|
}
|
||||||
|
return Result{
|
||||||
|
Allowed: int(n) <= limit,
|
||||||
|
Limit: limit,
|
||||||
|
Remaining: remaining,
|
||||||
|
ResetUnix: (bucket + 1) * win,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// RecordUsage increments total/daily counters and stamps last-used for a key.
|
||||||
|
// Best-effort: errors are ignored.
|
||||||
|
func (l *Limiter) RecordUsage(ctx context.Context, keyID string) {
|
||||||
|
if !l.Enabled() || keyID == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
now := time.Now()
|
||||||
|
day := now.Format("20060102")
|
||||||
|
pipe := l.rdb.Pipeline()
|
||||||
|
pipe.Incr(ctx, "usage:total:"+keyID)
|
||||||
|
dayKey := "usage:day:" + keyID + ":" + day
|
||||||
|
pipe.Incr(ctx, dayKey)
|
||||||
|
pipe.Expire(ctx, dayKey, 90*24*time.Hour)
|
||||||
|
pipe.Set(ctx, "usage:last:"+keyID, now.Unix(), 0)
|
||||||
|
_, _ = pipe.Exec(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Usage reads aggregated usage for a key. Returns a zero-value stat on error.
|
||||||
|
func (l *Limiter) Usage(ctx context.Context, keyID string) UsageStat {
|
||||||
|
var st UsageStat
|
||||||
|
if !l.Enabled() || keyID == "" {
|
||||||
|
return st
|
||||||
|
}
|
||||||
|
day := time.Now().Format("20060102")
|
||||||
|
st.Total, _ = l.rdb.Get(ctx, "usage:total:"+keyID).Int64()
|
||||||
|
st.Today, _ = l.rdb.Get(ctx, "usage:day:"+keyID+":"+day).Int64()
|
||||||
|
if v, err := l.rdb.Get(ctx, "usage:last:"+keyID).Int64(); err == nil {
|
||||||
|
st.LastUsedAt = &v
|
||||||
|
}
|
||||||
|
return st
|
||||||
|
}
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
package ratelimit
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestDisabledFailsOpen verifies that a Limiter without a Redis backend allows
|
||||||
|
// all requests and reports usage as zero rather than erroring.
|
||||||
|
func TestDisabledFailsOpen(t *testing.T) {
|
||||||
|
l := New("not-a-valid-url") // parse error => disabled
|
||||||
|
if l.Enabled() {
|
||||||
|
t.Fatal("expected limiter to be disabled for invalid url")
|
||||||
|
}
|
||||||
|
res := l.Allow(context.Background(), "x", 1, time.Minute)
|
||||||
|
if !res.Allowed || res.Remaining != 1 {
|
||||||
|
t.Fatalf("disabled limiter must fail open: %+v", res)
|
||||||
|
}
|
||||||
|
// Must not panic and must return zero usage.
|
||||||
|
l.RecordUsage(context.Background(), "k1")
|
||||||
|
if u := l.Usage(context.Background(), "k1"); u.Total != 0 {
|
||||||
|
t.Fatalf("disabled usage should be zero, got %+v", u)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestNilReceiverSafe ensures a nil *Limiter is safe to use (handler default).
|
||||||
|
func TestNilReceiverSafe(t *testing.T) {
|
||||||
|
var l *Limiter
|
||||||
|
if l.Enabled() {
|
||||||
|
t.Fatal("nil limiter must report disabled")
|
||||||
|
}
|
||||||
|
res := l.Allow(context.Background(), "x", 5, time.Minute)
|
||||||
|
if !res.Allowed {
|
||||||
|
t.Fatal("nil limiter must fail open")
|
||||||
|
}
|
||||||
|
l.RecordUsage(context.Background(), "k")
|
||||||
|
_ = l.Usage(context.Background(), "k")
|
||||||
|
}
|
||||||
|
|
||||||
|
func testLimiter(t *testing.T) *Limiter {
|
||||||
|
t.Helper()
|
||||||
|
url := os.Getenv("OPENGOODS_REDIS_URL")
|
||||||
|
if url == "" {
|
||||||
|
url = "redis://localhost:6379/0"
|
||||||
|
}
|
||||||
|
l := New(url)
|
||||||
|
if !l.Enabled() {
|
||||||
|
t.Skip("redis not configured")
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if err := l.rdb.Ping(ctx).Err(); err != nil {
|
||||||
|
t.Skipf("redis not reachable: %v", err)
|
||||||
|
}
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAllowFixedWindow(t *testing.T) {
|
||||||
|
l := testLimiter(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
id := fmt.Sprintf("test:%d", time.Now().UnixNano())
|
||||||
|
|
||||||
|
for i := 1; i <= 2; i++ {
|
||||||
|
if res := l.Allow(ctx, id, 2, time.Minute); !res.Allowed {
|
||||||
|
t.Fatalf("request %d should be allowed: %+v", i, res)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res := l.Allow(ctx, id, 2, time.Minute)
|
||||||
|
if res.Allowed {
|
||||||
|
t.Fatalf("3rd request over limit 2 should be denied: %+v", res)
|
||||||
|
}
|
||||||
|
if res.Remaining != 0 {
|
||||||
|
t.Fatalf("remaining should be 0 when over limit, got %d", res.Remaining)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRecordAndReadUsage(t *testing.T) {
|
||||||
|
l := testLimiter(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
key := fmt.Sprintf("usagekey:%d", time.Now().UnixNano())
|
||||||
|
|
||||||
|
l.RecordUsage(ctx, key)
|
||||||
|
l.RecordUsage(ctx, key)
|
||||||
|
u := l.Usage(ctx, key)
|
||||||
|
if u.Total != 2 || u.Today != 2 {
|
||||||
|
t.Fatalf("expected total=2 today=2, got %+v", u)
|
||||||
|
}
|
||||||
|
if u.LastUsedAt == nil {
|
||||||
|
t.Fatal("expected last-used timestamp to be set")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -29,6 +29,15 @@ func (s *Store) Ping(ctx context.Context) error {
|
|||||||
return s.pool.Ping(ctx)
|
return s.pool.Ping(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Barcode is one GS1 trade item number attached to a product.
|
||||||
|
type Barcode struct {
|
||||||
|
GTIN string `json:"gtin"`
|
||||||
|
GTINType string `json:"gtin_type"`
|
||||||
|
PackLevel string `json:"pack_level"`
|
||||||
|
Region *string `json:"region"`
|
||||||
|
IsPrimary bool `json:"is_primary"`
|
||||||
|
}
|
||||||
|
|
||||||
// Product is the full public view of a product.
|
// Product is the full public view of a product.
|
||||||
type Product struct {
|
type Product struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
@@ -41,6 +50,7 @@ type Product struct {
|
|||||||
NetContentUnit *string `json:"net_content_unit"`
|
NetContentUnit *string `json:"net_content_unit"`
|
||||||
CountryOfOrigin *string `json:"country_of_origin"`
|
CountryOfOrigin *string `json:"country_of_origin"`
|
||||||
QualityScore float64 `json:"quality_score"`
|
QualityScore float64 `json:"quality_score"`
|
||||||
|
Barcodes []Barcode `json:"barcodes"`
|
||||||
Nutriments map[string]any `json:"nutriments,omitempty"`
|
Nutriments map[string]any `json:"nutriments,omitempty"`
|
||||||
NutritionBasis *string `json:"nutrition_basis,omitempty"`
|
NutritionBasis *string `json:"nutrition_basis,omitempty"`
|
||||||
NutriScore *string `json:"nutri_score,omitempty"`
|
NutriScore *string `json:"nutri_score,omitempty"`
|
||||||
@@ -49,6 +59,27 @@ type Product struct {
|
|||||||
Additives []string `json:"additives,omitempty"`
|
Additives []string `json:"additives,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ProductBarcodes returns every barcode attached to a product, primary first.
|
||||||
|
func (s *Store) ProductBarcodes(ctx context.Context, productID string) ([]Barcode, error) {
|
||||||
|
rows, err := s.pool.Query(ctx,
|
||||||
|
`SELECT gtin, gtin_type, pack_level, region, is_primary
|
||||||
|
FROM product_barcode WHERE product_id = $1
|
||||||
|
ORDER BY is_primary DESC, gtin`, productID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
out := []Barcode{}
|
||||||
|
for rows.Next() {
|
||||||
|
var b Barcode
|
||||||
|
if err := rows.Scan(&b.GTIN, &b.GTINType, &b.PackLevel, &b.Region, &b.IsPrimary); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out = append(out, b)
|
||||||
|
}
|
||||||
|
return out, rows.Err()
|
||||||
|
}
|
||||||
|
|
||||||
// ProductSummary is a lightweight row used in search/listing responses.
|
// ProductSummary is a lightweight row used in search/listing responses.
|
||||||
type ProductSummary struct {
|
type ProductSummary struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
@@ -86,16 +117,35 @@ func scanProduct(row pgx.Row) (*Product, error) {
|
|||||||
return &p, nil
|
return &p, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProductByGTIN looks up an active product by its barcode.
|
// ProductByGTIN looks up an active product by any of its barcodes.
|
||||||
func (s *Store) ProductByGTIN(ctx context.Context, gtin string) (*Product, error) {
|
func (s *Store) ProductByGTIN(ctx context.Context, gtin string) (*Product, error) {
|
||||||
row := s.pool.QueryRow(ctx, productSelect+" WHERE p.gtin = $1 AND p.status = 'active'", gtin)
|
row := s.pool.QueryRow(ctx, productSelect+`
|
||||||
return scanProduct(row)
|
WHERE p.status = 'active'
|
||||||
|
AND (p.gtin = $1 OR EXISTS (
|
||||||
|
SELECT 1 FROM product_barcode pb
|
||||||
|
WHERE pb.product_id = p.id AND pb.gtin = $1))
|
||||||
|
LIMIT 1`, gtin)
|
||||||
|
p, err := scanProduct(row)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if p.Barcodes, err = s.ProductBarcodes(ctx, p.ID); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return p, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProductByID looks up a product by its UUID.
|
// ProductByID looks up a product by its UUID.
|
||||||
func (s *Store) ProductByID(ctx context.Context, id string) (*Product, error) {
|
func (s *Store) ProductByID(ctx context.Context, id string) (*Product, error) {
|
||||||
row := s.pool.QueryRow(ctx, productSelect+" WHERE p.id = $1", id)
|
row := s.pool.QueryRow(ctx, productSelect+" WHERE p.id = $1", id)
|
||||||
return scanProduct(row)
|
p, err := scanProduct(row)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if p.Barcodes, err = s.ProductBarcodes(ctx, p.ID); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return p, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SearchProducts performs a fuzzy name search with optional category subtree filter.
|
// SearchProducts performs a fuzzy name search with optional category subtree filter.
|
||||||
@@ -104,7 +154,9 @@ func (s *Store) SearchProducts(ctx context.Context, q, category string, limit, o
|
|||||||
where := "WHERE p.status = 'active'"
|
where := "WHERE p.status = 'active'"
|
||||||
if q != "" {
|
if q != "" {
|
||||||
args = append(args, q)
|
args = append(args, q)
|
||||||
where += " AND p.name ILIKE '%' || $1 || '%'"
|
where += ` AND (p.name ILIKE '%' || $1 || '%'
|
||||||
|
OR EXISTS (SELECT 1 FROM product_barcode pb
|
||||||
|
WHERE pb.product_id = p.id AND pb.gtin ILIKE '%' || $1 || '%'))`
|
||||||
}
|
}
|
||||||
if category != "" {
|
if category != "" {
|
||||||
args = append(args, category)
|
args = append(args, category)
|
||||||
@@ -253,6 +305,30 @@ func (s *Store) ListCategories(ctx context.Context) ([]Category, error) {
|
|||||||
return out, rows.Err()
|
return out, rows.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// APIKey is the minimal metadata the public API needs to authorize a caller.
|
||||||
|
type APIKey struct {
|
||||||
|
ID string
|
||||||
|
Name string
|
||||||
|
RateLimitPerMin int
|
||||||
|
}
|
||||||
|
|
||||||
|
// APIKeyByHash returns the active (non-revoked) key matching a SHA-256 hash,
|
||||||
|
// or ErrNotFound if no such active key exists.
|
||||||
|
func (s *Store) APIKeyByHash(ctx context.Context, hash string) (*APIKey, error) {
|
||||||
|
var k APIKey
|
||||||
|
err := s.pool.QueryRow(ctx,
|
||||||
|
`SELECT id, name, rate_limit_per_min
|
||||||
|
FROM api_key WHERE key_hash = $1 AND revoked_at IS NULL`, hash,
|
||||||
|
).Scan(&k.ID, &k.Name, &k.RateLimitPerMin)
|
||||||
|
if errors.Is(err, pgx.ErrNoRows) {
|
||||||
|
return nil, ErrNotFound
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &k, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Source describes a data source with its license and trust weight.
|
// Source describes a data source with its license and trust weight.
|
||||||
type Source struct {
|
type Source struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
DROP TABLE IF EXISTS product_barcode;
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
-- Multi-barcode support: one product can carry many GS1 barcodes
|
||||||
|
-- (consumer unit EAN-13/UPC, case ITF-14, regional re-labels, etc.).
|
||||||
|
-- product.gtin is kept as the denormalized "primary" barcode for
|
||||||
|
-- backward compatibility and is mirrored from the is_primary row here.
|
||||||
|
|
||||||
|
CREATE TABLE product_barcode (
|
||||||
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
product_id UUID NOT NULL REFERENCES product(id) ON DELETE CASCADE,
|
||||||
|
gtin VARCHAR(14) NOT NULL,
|
||||||
|
gtin_type VARCHAR(8) NOT NULL DEFAULT 'EAN13',
|
||||||
|
pack_level VARCHAR(8) NOT NULL DEFAULT 'each',
|
||||||
|
region VARCHAR(8),
|
||||||
|
is_primary BOOLEAN NOT NULL DEFAULT false,
|
||||||
|
source_id UUID REFERENCES source(id),
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||||
|
CONSTRAINT product_barcode_type_chk CHECK (gtin_type IN ('EAN8','UPC','EAN13','ITF14','GTIN14')),
|
||||||
|
CONSTRAINT product_barcode_pack_chk CHECK (pack_level IN ('each','case','pallet'))
|
||||||
|
);
|
||||||
|
|
||||||
|
-- A barcode is globally unique: one code maps to exactly one product.
|
||||||
|
CREATE UNIQUE INDEX idx_product_barcode_gtin ON product_barcode (gtin);
|
||||||
|
CREATE INDEX idx_product_barcode_product ON product_barcode (product_id);
|
||||||
|
-- At most one primary barcode per product.
|
||||||
|
CREATE UNIQUE INDEX idx_product_barcode_primary ON product_barcode (product_id) WHERE is_primary;
|
||||||
|
|
||||||
|
-- Backfill: lift each product's existing gtin into the new table as primary.
|
||||||
|
INSERT INTO product_barcode (product_id, gtin, gtin_type, pack_level, is_primary)
|
||||||
|
SELECT id, gtin,
|
||||||
|
CASE WHEN length(gtin) = 8 THEN 'EAN8'
|
||||||
|
WHEN length(gtin) = 12 THEN 'UPC'
|
||||||
|
WHEN length(gtin) = 14 THEN 'GTIN14'
|
||||||
|
ELSE 'EAN13' END,
|
||||||
|
'each', true
|
||||||
|
FROM product
|
||||||
|
WHERE gtin IS NOT NULL AND gtin <> ''
|
||||||
|
ON CONFLICT (gtin) DO NOTHING;
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
DROP TABLE IF EXISTS api_key;
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
-- API keys for the public read-only API. Keys grant higher rate limits and let
|
||||||
|
-- usage be attributed to a caller; the API itself stays free and read-only.
|
||||||
|
-- Only the SHA-256 hash of a key is stored; the plaintext is shown once at
|
||||||
|
-- creation time. Keys are issued/revoked from the admin console. The public
|
||||||
|
-- server only ever SELECTs from this table (request counting lives in Redis),
|
||||||
|
-- preserving its read-only contract against PostgreSQL.
|
||||||
|
CREATE TABLE IF NOT EXISTS api_key (
|
||||||
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
name TEXT NOT NULL,
|
||||||
|
key_prefix VARCHAR(20) NOT NULL, -- shown for identification, e.g. og_live_AbC1
|
||||||
|
key_hash TEXT NOT NULL UNIQUE, -- hex SHA-256 of the full key
|
||||||
|
owner_email TEXT,
|
||||||
|
tier VARCHAR(16) NOT NULL DEFAULT 'free',
|
||||||
|
rate_limit_per_min INT NOT NULL DEFAULT 120,
|
||||||
|
revoked_at TIMESTAMPTZ,
|
||||||
|
created_by TEXT,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||||
|
CONSTRAINT api_key_tier_chk CHECK (tier IN ('free', 'partner', 'internal')),
|
||||||
|
CONSTRAINT api_key_rate_chk CHECK (rate_limit_per_min > 0)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Fast lookup of active keys by their hash on every authenticated request.
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_api_key_active_hash
|
||||||
|
ON api_key (key_hash) WHERE revoked_at IS NULL;
|
||||||
@@ -60,6 +60,29 @@ export default function ProductView({ id, onBack }: { id: string; onBack: () =>
|
|||||||
<div className="mt-4">
|
<div className="mt-4">
|
||||||
<Row label="品牌" value={p.brand} />
|
<Row label="品牌" value={p.brand} />
|
||||||
<Row label="条码 (GTIN)" value={p.gtin} />
|
<Row label="条码 (GTIN)" value={p.gtin} />
|
||||||
|
{(() => {
|
||||||
|
const others = (p.barcodes || []).filter(
|
||||||
|
(b) => !b.is_primary && b.gtin !== p.gtin,
|
||||||
|
);
|
||||||
|
return others.length ? (
|
||||||
|
<Row
|
||||||
|
label="其他条码"
|
||||||
|
value={
|
||||||
|
<div className="flex flex-wrap gap-1.5">
|
||||||
|
{others.map((b) => (
|
||||||
|
<span
|
||||||
|
key={b.gtin}
|
||||||
|
className="font-mono text-xs bg-gray-100 text-gray-600 rounded px-1.5 py-0.5"
|
||||||
|
title={`${b.gtin_type} · ${b.pack_level}`}
|
||||||
|
>
|
||||||
|
{b.gtin}
|
||||||
|
</span>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
) : null;
|
||||||
|
})()}
|
||||||
<Row label="品类" value={p.category_path} />
|
<Row label="品类" value={p.category_path} />
|
||||||
<Row
|
<Row
|
||||||
label="净含量"
|
label="净含量"
|
||||||
|
|||||||
@@ -6,9 +6,18 @@ export interface ProductSummary {
|
|||||||
category_path: string | null;
|
category_path: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface Barcode {
|
||||||
|
gtin: string;
|
||||||
|
gtin_type: string;
|
||||||
|
pack_level: string;
|
||||||
|
region: string | null;
|
||||||
|
is_primary: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
export interface Product {
|
export interface Product {
|
||||||
id: string;
|
id: string;
|
||||||
gtin: string | null;
|
gtin: string | null;
|
||||||
|
barcodes?: Barcode[] | null;
|
||||||
name: string;
|
name: string;
|
||||||
brand: string | null;
|
brand: string | null;
|
||||||
category_path: string | null;
|
category_path: string | null;
|
||||||
|
|||||||
Reference in New Issue
Block a user