🐳 Adds dockerfile
This commit is contained in:
@@ -59,7 +59,7 @@ func makeGetReq(url string) ([]byte, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", token)
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -84,7 +84,7 @@ func makePostReq(url string, body any) ([]byte, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", token)
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
|
10
web/.dockerignore
Normal file
10
web/.dockerignore
Normal file
@@ -0,0 +1,10 @@
|
||||
.idea/
|
||||
.vscode/
|
||||
|
||||
node_modules/
|
||||
.output/
|
||||
.data/
|
||||
.nuxt/
|
||||
.cache/
|
||||
|
||||
.env*
|
20
web/Dockerfile
Normal file
20
web/Dockerfile
Normal file
@@ -0,0 +1,20 @@
|
||||
FROM oven/bun:1 AS build
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY package.json bun.lock .
|
||||
RUN bun install --frozen-lockfile --production
|
||||
|
||||
COPY . .
|
||||
RUN bun run build
|
||||
|
||||
FROM oven/bun:1
|
||||
|
||||
WORKDIR /app
|
||||
COPY --from=build /app/.output .
|
||||
|
||||
ENV PORT 3000
|
||||
ENV HOST 0.0.0.0
|
||||
|
||||
EXPOSE 3000
|
||||
CMD ["bun", "/app/server/index.mjs"]
|
39
web/composables/useAPI.ts
Normal file
39
web/composables/useAPI.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import type { FetchResponse } from "ofetch"
|
||||
|
||||
export function useAPI(route: string = "/") {
|
||||
return `https://api.door.svitan.dev${route}`
|
||||
}
|
||||
|
||||
export async function handleRequestError({ error }: { error: Error }) {
|
||||
const toast = useToast()
|
||||
toast.add({
|
||||
title: "Error occurred",
|
||||
description: error.message,
|
||||
color: "error",
|
||||
})
|
||||
}
|
||||
|
||||
export function handleResponse<T>(
|
||||
success: (response: FetchResponse<T>) => Promise<void> | void
|
||||
) {
|
||||
const token = useToken()
|
||||
const toast = useToast()
|
||||
|
||||
return async function ({ response }: { response: FetchResponse<T> }) {
|
||||
console.log(response)
|
||||
// console.log(await response.text())
|
||||
if (response.status === 200) {
|
||||
await success(response.clone())
|
||||
} else if (response.status === 401) {
|
||||
toast.add({ title: "Token not valid", color: "error" })
|
||||
token.value = ""
|
||||
navigateTo("/token")
|
||||
} else {
|
||||
toast.add({
|
||||
title: "Error occurred",
|
||||
description: await response.text(),
|
||||
color: "error",
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,3 +1,10 @@
|
||||
export function useToken() {
|
||||
return useCookie<string | undefined>("token");
|
||||
return useCookie<string | undefined>("token")
|
||||
}
|
||||
|
||||
export function useHeaders() {
|
||||
const token = useToken()
|
||||
return {
|
||||
Authorization: `Bearer ${token.value}`
|
||||
}
|
||||
}
|
||||
|
@@ -47,7 +47,7 @@
|
||||
:color="locked ? 'primary' : 'secondary'"
|
||||
variant="solid"
|
||||
class="flex items-center justify-center w-full h-10"
|
||||
@click="() => (locked = !locked)"
|
||||
@click="toggleLock"
|
||||
>
|
||||
{{ locked ? "Unlock" : "Lock" }}
|
||||
</UButton>
|
||||
@@ -87,14 +87,44 @@
|
||||
|
||||
<script lang="ts" setup>
|
||||
const token = useToken()
|
||||
|
||||
const open = ref(true)
|
||||
const locked = ref(true)
|
||||
const alert = ref(false)
|
||||
|
||||
const toast = useToast()
|
||||
|
||||
async function toggleLock() {
|
||||
const desired = !locked.value
|
||||
await $fetch(useAPI("/lock"), {
|
||||
method: "POST",
|
||||
headers: useHeaders(),
|
||||
body: {
|
||||
locked: desired,
|
||||
},
|
||||
onRequestError: handleRequestError,
|
||||
onResponse: handleResponse(() => {
|
||||
toast.add({
|
||||
title: `The door was ${desired ? "locked" : "unlocked"}`,
|
||||
color: "success",
|
||||
})
|
||||
locked.value = desired
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
if (!token.value) {
|
||||
return navigateTo("/token")
|
||||
}
|
||||
console.log(token.value)
|
||||
|
||||
$fetch(useAPI("/lock"), {
|
||||
method: "GET",
|
||||
headers: useHeaders(),
|
||||
onRequestError: handleRequestError,
|
||||
onResponse: handleResponse(async (response) => {
|
||||
console.log("god: ", await response.json())
|
||||
}),
|
||||
})
|
||||
})
|
||||
</script>
|
||||
|
@@ -37,33 +37,17 @@ const toast = useToast()
|
||||
|
||||
async function onSubmit(event: FormSubmitEvent<Schema>) {
|
||||
const received = event.data.token
|
||||
await $fetch("https://door.svitan.dev/open", {
|
||||
await $fetch(useAPI("/open"), {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${received}`,
|
||||
},
|
||||
async onRequestError({ error }) {
|
||||
toast.add({
|
||||
title: "Error occurred",
|
||||
description: error.message,
|
||||
color: "error",
|
||||
})
|
||||
},
|
||||
async onResponse({ response }) {
|
||||
if (response.status === 200) {
|
||||
toast.add({ title: "Token saved", color: "success" })
|
||||
token.value = received
|
||||
navigateTo("/")
|
||||
} else if (response.status === 401) {
|
||||
toast.add({ title: "Token not valid", color: "error" })
|
||||
} else {
|
||||
toast.add({
|
||||
title: "Error occurred",
|
||||
description: await response.text(),
|
||||
color: "error",
|
||||
})
|
||||
}
|
||||
},
|
||||
onRequestError: handleRequestError,
|
||||
onResponse: handleResponse(() => {
|
||||
toast.add({ title: "Token saved", color: "success" })
|
||||
token.value = received
|
||||
navigateTo("/")
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user