This commit is contained in:
2024-04-22 00:21:35 +07:00
8 changed files with 93 additions and 4 deletions

3
.dockerignore Normal file
View File

@@ -0,0 +1,3 @@
node_modules
.DS_Store
dist

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
node_modules
.DS_Store
dist

21
Dockerfile Normal file
View File

@@ -0,0 +1,21 @@
FROM node:20-slim AS base
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable
COPY . /app
WORKDIR /app
FROM base AS prod-deps
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile
FROM base AS build
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
ARG VUE_APP_API_URL
RUN pnpm run build
FROM base AS app
COPY --from=prod-deps /app/node_modules /app/node_modules
COPY --from=build /app/dist /app/dist
RUN pnpm install -g serve
EXPOSE 3000
EXPOSE 3001
CMD [ "serve", "-s", "dist" ]

View File

@@ -1,5 +1,4 @@
export const CONFIG = {
// api_url: "http://localhost:3000",
api_url: "http://178.128.18.156:3000",
// api_url: "http://178.128.18.156:3000",
api_url: process.env.VUE_APP_API_URL || "http://localhost:3000",
};

View File

@@ -4,6 +4,7 @@ import CheckSurvey from "@/views/CheckSurvey.vue";
import QuestionList from "@/views/QuestionList.vue";
import RegisterForm from "@/views/RegisterForm.vue";
import SearchUser from "@/views/SearchUser.vue";
import UploadImage from "@/views/UploadImage.vue";
const routes = [
{
path: "/",
@@ -26,6 +27,10 @@ const routes = [
path: "/search",
component: SearchUser,
},
{
path: "/upload",
component: UploadImage,
},
];
const router = createRouter({
@@ -33,4 +38,5 @@ const router = createRouter({
routes,
});
export default router;
export default router;

8
src/utils/fileUpload.js Normal file
View File

@@ -0,0 +1,8 @@
export function createFormData(request, file) {
const formData = new FormData();
Object.entries(request.formData).forEach(([key, value]) => {
formData.append(key, value);
});
formData.append("file", file, file.name);
return formData;
}

View File

@@ -5,6 +5,12 @@ export const client = createTRPCProxyClient({
links: [
httpBatchLink({
url: CONFIG.api_url,
headers: () => {
let token = localStorage.getItem("token");
return {
Authorization: token ? `Bearer ${token}` : "",
};
},
}),
],
});

45
src/views/UploadImage.vue Normal file
View File

@@ -0,0 +1,45 @@
<template>
<div>
<input
type="file"
@change="onFileChanged($event)"
accept="image/*"
capture
/>
<button @click="uploadImage()">Upload</button>
</div>
</template>
<script>
import { client } from "@/utils/trpc";
import { createFormData } from "@/utils/fileUpload";
export default {
data: () => ({
file: null,
}),
methods: {
onFileChanged(event) {
const file = event.target.files[0];
this.file = file;
},
async uploadImage() {
if (!this.file) {
return;
}
let request = await client.user.requestChangeImage.mutate({
imageName: this.file.name,
contentType: this.file.type,
});
let formData = createFormData(request, this.file);
await fetch(request.postURL, {
method: "POST",
body: formData,
}).then(() => {
client.user.confirmChangeImage.mutate();
});
console.log("done");
},
},
};
</script>
<style></style>