Copyright (C) 2021 Toranoana Inc. All Rights Reserved.
upstash の Redis を oak_session で使う
import { Application, Context, Router } from
"https://deno.land/x/oak/mod.ts";
import { RedisStore, Session } from
"https://deno.land/x/oak_sessions/mod.ts";
import { Redis } from "https://deno.land/x/upstash_redis/mod.ts";
const redis = new Redis({
url: Deno.env.get("UPSTASH_URL")!,
token: Deno.env.get("UPSTASH_TOKEN")!,
automaticDeserialization: false, // <== ポイント
});
const router = new Router();
router.get("/", async (context: Context) => {
const name = await context.state.session.get("name");
context.response.body = `
${!name ? "" : "name=" + name}
submit
`;
});
router.post("/", async (context: Context) => {
const form = await context.request.body({ type: "form" }).value;
const name = form.get("name");
if (!!name) context.state.session.set("name", name);
context.response.redirect("/");
});
type AppState = {
session: Session;
};
const app = new Application();
const store = new RedisStore(redis);
app.use(Session.initMiddleware(store));
app.use(router.routes());
app.use(router.allowedMethods());
await app.listen({ port: 8080 });
upstash_redisは、デフォルトで、jsonの自動デシリアライズが
ONになっているので この機能を停止させる
デシリアライズは、oak_session に任せる!