🐳 Adds dockerfile
This commit is contained in:
@@ -59,7 +59,7 @@ func makeGetReq(url string) ([]byte, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("Authorization", token)
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
||||||
res, err := http.DefaultClient.Do(req)
|
res, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -84,7 +84,7 @@ func makePostReq(url string, body any) ([]byte, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("Authorization", token)
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
res, err := http.DefaultClient.Do(req)
|
res, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
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() {
|
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'"
|
:color="locked ? 'primary' : 'secondary'"
|
||||||
variant="solid"
|
variant="solid"
|
||||||
class="flex items-center justify-center w-full h-10"
|
class="flex items-center justify-center w-full h-10"
|
||||||
@click="() => (locked = !locked)"
|
@click="toggleLock"
|
||||||
>
|
>
|
||||||
{{ locked ? "Unlock" : "Lock" }}
|
{{ locked ? "Unlock" : "Lock" }}
|
||||||
</UButton>
|
</UButton>
|
||||||
@@ -87,14 +87,44 @@
|
|||||||
|
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
const token = useToken()
|
const token = useToken()
|
||||||
|
|
||||||
const open = ref(true)
|
const open = ref(true)
|
||||||
const locked = ref(true)
|
const locked = ref(true)
|
||||||
const alert = ref(false)
|
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(() => {
|
onMounted(() => {
|
||||||
if (!token.value) {
|
if (!token.value) {
|
||||||
return navigateTo("/token")
|
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>
|
</script>
|
||||||
|
@@ -37,33 +37,17 @@ const toast = useToast()
|
|||||||
|
|
||||||
async function onSubmit(event: FormSubmitEvent<Schema>) {
|
async function onSubmit(event: FormSubmitEvent<Schema>) {
|
||||||
const received = event.data.token
|
const received = event.data.token
|
||||||
await $fetch("https://door.svitan.dev/open", {
|
await $fetch(useAPI("/open"), {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${received}`,
|
Authorization: `Bearer ${received}`,
|
||||||
},
|
},
|
||||||
async onRequestError({ error }) {
|
onRequestError: handleRequestError,
|
||||||
toast.add({
|
onResponse: handleResponse(() => {
|
||||||
title: "Error occurred",
|
toast.add({ title: "Token saved", color: "success" })
|
||||||
description: error.message,
|
token.value = received
|
||||||
color: "error",
|
navigateTo("/")
|
||||||
})
|
}),
|
||||||
},
|
|
||||||
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",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user