Skip to content

Commit

Permalink
feat: init
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff committed Oct 12, 2024
0 parents commit 05429fc
Show file tree
Hide file tree
Showing 34 changed files with 922 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .browserslistrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
> 1%
last 2 versions
not dead
not ie 11
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# http://editorconfig.org
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
insert_final_newline = false
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_DOMAIN = http://192.168.3.17:3000/
1 change: 1 addition & 0 deletions .env.client
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_DOMAIN = http://192.168.3.17:3000/
11 changes: 11 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public
dist
*.d.ts
/src/assets
package.json
eslint.config.js
.prettierrc.js
commitlint.config.js
postcss.config.js
tailwind.config.js
stylelint.config.js
120 changes: 120 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
module.exports = {
root: true,
env: {
node: true
},
globals: {
// Ref sugar (take 2)
$: "readonly",
$$: "readonly",
$ref: "readonly",
$shallowRef: "readonly",
$computed: "readonly",

// index.d.ts
// global.d.ts
Fn: "readonly",
PromiseFn: "readonly",
RefType: "readonly",
LabelValueOptions: "readonly",
EmitType: "readonly",
TargetContext: "readonly",
ComponentElRef: "readonly",
ComponentRef: "readonly",
ElRef: "readonly",
global: "readonly",
ForDataType: "readonly",
ComponentRoutes: "readonly",

// script setup
defineProps: "readonly",
defineEmits: "readonly",
defineExpose: "readonly",
withDefaults: "readonly"
},
extends: [
"plugin:vue/vue3-essential",
"eslint:recommended",
"@vue/typescript/recommended",
"@vue/prettier",
"@vue/eslint-config-typescript"
],
parser: "vue-eslint-parser",
parserOptions: {
parser: "@typescript-eslint/parser",
ecmaVersion: 2020,
sourceType: "module",
jsxPragma: "React",
ecmaFeatures: {
jsx: true
}
},
overrides: [
{
files: ["*.ts", "*.vue"],
rules: {
"no-undef": "off"
}
},
{
files: ["*.vue"],
parser: "vue-eslint-parser",
parserOptions: {
parser: "@typescript-eslint/parser",
extraFileExtensions: [".vue"],
ecmaVersion: "latest",
ecmaFeatures: {
jsx: true
}
},
rules: {
"no-undef": "off"
}
}
],
rules: {
"vue/no-v-html": "off",
"vue/require-default-prop": "off",
"vue/require-explicit-emits": "off",
"vue/multi-word-component-names": "off",
"@typescript-eslint/no-explicit-any": "off", // any
"no-debugger": "off",
"@typescript-eslint/explicit-module-boundary-types": "off", // setup()
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"vue/html-self-closing": [
"error",
{
html: {
void: "always",
normal: "always",
component: "always"
},
svg: "always",
math: "always"
}
],
"@typescript-eslint/no-unused-vars": [
"error",
{
argsIgnorePattern: "^_",
varsIgnorePattern: "^_"
}
],
"no-unused-vars": [
"error",
{
argsIgnorePattern: "^_",
varsIgnorePattern: "^_"
}
],
"prettier/prettier": [
"error",
{
endOfLine: "auto"
}
]
}
};
34 changes: 34 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
.DS_Store
dist
dist-ssr
coverage
*.local

/cypress/videos/
/cypress/screenshots/

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

*.tsbuildinfo

# wrangler files
.wrangler
.dev.vars
8 changes: 8 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "https://json.schemastore.org/prettierrc",
"semi": true,
"tabWidth": 2,
"singleQuote": false,
"printWidth": 100,
"trailingComma": "none"
}
4 changes: 4 additions & 0 deletions .stylelintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/dist/*
/public/*
public/*
src/style/reset.scss
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# cloudflare-pages-demo

## Project Setup

```sh
npm install
```

### Compile and Hot-Reload for Development

```sh
npm run dev
```

### Run/Debug on Webstorm

Set Run/Debug Configuration -> npm -> Command: run, Scripts: dev

### Type-Check, Compile and Minify for Production

```sh
npm run build
```

### Lint with [ESLint](https://eslint.org/)

```sh
npm run lint
```

### Preview on Local Host

```sh
npm run preview
```

### Deploy

```sh
npm run deploy
```

## D1

### 查看数据库表

```sql
SELECT name FROM sqlite_master WHERE type='table';
```
30 changes: 30 additions & 0 deletions app/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// app/server.ts
import { Hono } from "hono";
import { cors } from "hono/cors";
import userRouters from "./user/api";

interface Env {
ASSETS: {
fetch: (request: Request) => Promise<Response>;
};
}

const app = new Hono();
app.use("/*", cors());

/*
API
*/
app.route("/api/user", userRouters);

app.get("/api/hello-world", async (c) => {
return c.text("Hello World!");
});

// 处理所有其他请求,返回 Vue 的打包好的静态资源
app.get("/*", async (c) => {
const env = c.env as Env;
return env.ASSETS.fetch(c.req.raw);
});

export default app;
Empty file added app/user/api.md
Empty file.
25 changes: 25 additions & 0 deletions app/user/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// api.ts
import { Hono } from "hono";
import { saveUser, getUserById } from "./db";
import type User from "./user";
import type { D1Database } from "@cloudflare/workers-types";

const api = new Hono<{ Bindings: { DB: D1Database } }>();

api.post("/save", async (c) => {
const user: User = await c.req.json();
await saveUser(c.env.DB, user);
return c.json({ success: true });
});

api.get("/detail", async (c) => {
const id = parseInt(c.req.query("id") as string, 10);
const user = await getUserById(c.env.DB, id);
if (user) {
return c.json(user);
} else {
return c.json({ error: "User not found" }, 404);
}
});

export default api;
55 changes: 55 additions & 0 deletions app/user/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// db.ts
import type { D1Database } from "@cloudflare/workers-types";
import type User from "./user";

export async function initializeUsersTable(DB: D1Database): Promise<void> {
const query = `
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
resolution VARCHAR(50),
local_language VARCHAR(10),
m1 VARCHAR(255),
oaid VARCHAR(255),
channel VARCHAR(50),
app_version VARCHAR(20),
app_name VARCHAR(100),
pkg_name VARCHAR(100),
device_id VARCHAR(100),
platform VARCHAR(20),
os_version VARCHAR(20),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
`;
await DB.prepare(query).run();
}

export async function saveUser(DB: D1Database, user: User): Promise<void> {
await initializeUsersTable(DB); // Ensure the table exists
const query = `
INSERT INTO users (
resolution, local_language, m1, oaid, channel, app_version, app_name, pkg_name, device_id, platform, os_version
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`;
const values = [
user.resolution,
user.local_language,
user.m1,
user.oaid,
user.channel,
user.app_version,
user.app_name,
user.pkg_name,
user.device_id,
user.platform,
user.os_version
];
await DB.prepare(query)
.bind(...values)
.run();
}

export async function getUserById(DB: D1Database, id: number): Promise<User | null> {
const query = `SELECT * FROM users WHERE id = ?`;
const result = await DB.prepare(query).bind(id).first<User>();
return result || null;
}
17 changes: 17 additions & 0 deletions app/user/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// user.ts

export default interface User {
id: number;
resolution: string;
local_language: string;
m1: string;
oaid: string;
channel: string;
app_version: string;
app_name: string;
pkg_name: string;
device_id: string;
platform: string;
os_version: string;
created_at: Date;
}
1 change: 1 addition & 0 deletions env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
Loading

0 comments on commit 05429fc

Please sign in to comment.