From 57e5fde82fbd2378dbf3231741736851e357d719 Mon Sep 17 00:00:00 2001 From: wagslane Date: Mon, 12 Jun 2023 16:06:12 -0600 Subject: [PATCH] small updates --- Dockerfile | 4 +++- handler_notes.go | 2 +- handler_user.go | 2 +- buildprod.sh => scripts/buildprod.sh | 0 scripts/migrateup.sh | 4 +++- static/index.html | 19 ++++++++++++++----- 6 files changed, 22 insertions(+), 9 deletions(-) rename buildprod.sh => scripts/buildprod.sh (100%) diff --git a/Dockerfile b/Dockerfile index 898d270124..2be3d18b81 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,6 @@ -FROM --platform=linux/amd64 debian:stretch-slim +FROM --platform=linux/amd64 debian:stable-slim + +RUN apt-get update && apt-get install -y ca-certificates ADD notely /usr/bin/notely diff --git a/handler_notes.go b/handler_notes.go index b3f6848593..69e1c0ef8d 100644 --- a/handler_notes.go +++ b/handler_notes.go @@ -49,5 +49,5 @@ func (cfg *apiConfig) handlerNotesCreate(w http.ResponseWriter, r *http.Request, respondWithError(w, http.StatusNotFound, "Couldn't get note") return } - respondWithJSON(w, http.StatusOK, databaseNoteToNote(note)) + respondWithJSON(w, http.StatusCreated, databaseNoteToNote(note)) } diff --git a/handler_user.go b/handler_user.go index 686a06655e..5ba1fc2718 100644 --- a/handler_user.go +++ b/handler_user.go @@ -51,7 +51,7 @@ func (cfg *apiConfig) handlerUsersCreate(w http.ResponseWriter, r *http.Request) return } - respondWithJSON(w, http.StatusOK, databaseUserToUser(user)) + respondWithJSON(w, http.StatusCreated, databaseUserToUser(user)) } func generateRandomSHA256Hash() (string, error) { diff --git a/buildprod.sh b/scripts/buildprod.sh similarity index 100% rename from buildprod.sh rename to scripts/buildprod.sh diff --git a/scripts/migrateup.sh b/scripts/migrateup.sh index 5ddc6b2c60..5012b04237 100755 --- a/scripts/migrateup.sh +++ b/scripts/migrateup.sh @@ -1,4 +1,6 @@ -source .env +if [ -f .env ]; then + source .env +fi cd sql/schema goose mysql $DATABASE_URL up diff --git a/static/index.html b/static/index.html index 1e94b5f7b3..5d4ad73c09 100644 --- a/static/index.html +++ b/static/index.html @@ -7,7 +7,7 @@ -

Notely

+

Welcome to Notely

@@ -37,7 +37,7 @@

Your Notes

return; } const noteContent = document.getElementById('newNoteContent').value; - const response = await fetch(`${API_BASE}/notes`, + const response = await fetchWithAlert(`${API_BASE}/notes`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `ApiKey ${currentUserAPIKey}` }, body: JSON.stringify({ note: noteContent }) @@ -47,7 +47,7 @@

Your Notes

} async function getUser() { - const response = await fetch(`${API_BASE}/users`, { headers: { 'Authorization': `ApiKey ${currentUserAPIKey}` } }); + const response = await fetchWithAlert(`${API_BASE}/users`, { headers: { 'Authorization': `ApiKey ${currentUserAPIKey}` } }); return await response.json(); } @@ -55,7 +55,7 @@

Your Notes

if (!currentUser) { return; } - const response = await fetch(`${API_BASE}/notes`, { headers: { 'Authorization': `ApiKey ${currentUserAPIKey}` } }); + const response = await fetchWithAlert(`${API_BASE}/notes`, { headers: { 'Authorization': `ApiKey ${currentUserAPIKey}` } }); const notes = await response.json(); const notesContainer = document.getElementById('notes'); notesContainer.innerHTML = ''; @@ -71,7 +71,7 @@

Your Notes

async function createUser() { const nameField = document.getElementById('nameField'); - const response = await fetch(`${API_BASE}/users`, { + const response = await fetchWithAlert(`${API_BASE}/users`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: nameField.value }) // using the value from nameField @@ -113,6 +113,15 @@

Your Notes

document.getElementById('greetingMessage').textContent = `Hello ${user.name}!`; } + async function fetchWithAlert(url, options) { + const response = await fetch(url, options); + if (response.status > 299) { + alert(`Error: ${response.status}`); + return; + } + return response; + } + login();