#10 customize base url for view and api via env variables #12

Merged
tim merged 1 commits from 10-customize-base-url into master 2024-07-27 19:50:42 +00:00
3 changed files with 13 additions and 4 deletions

View File

@@ -1,12 +1,20 @@
package middleware package middleware
import ( import (
"log"
"net/http" "net/http"
"os"
) )
func EnableCors(next http.Handler) http.Handler { func EnableCors(next http.Handler) http.Handler {
var frontent_url = os.Getenv("FRONTEND_URL")
if frontent_url == "" {
log.Fatal("FRONTEND_URL is not set")
}
log.Println("FRONTEND_URL is", frontent_url)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:5173") w.Header().Set("Access-Control-Allow-Origin", frontent_url)
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, DELETE") w.Header().Set("Access-Control-Allow-Methods", "GET, POST, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Authorization") w.Header().Set("Access-Control-Allow-Headers", "Authorization")

View File

@@ -3,7 +3,7 @@
"version": "0.0.2", "version": "0.0.2",
"private": true, "private": true,
"scripts": { "scripts": {
"dev": "vite dev", "dev": "PUBLIC_BASE_API_URL=http://localhost:8080 vite dev",
"build": "vite build", "build": "vite build",
"preview": "vite preview", "preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json", "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",

View File

@@ -1,4 +1,5 @@
<script lang="ts"> <script lang="ts">
import { PUBLIC_BASE_API_URL } from '$env/static/public';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import type { Auth } from 'firebase/auth'; import type { Auth } from 'firebase/auth';
import { goto } from '$app/navigation'; import { goto } from '$app/navigation';
@@ -17,7 +18,7 @@
const formData = new FormData(form); const formData = new FormData(form);
try { try {
const response = await fetch('http://localhost:8080/workout', { const response = await fetch(PUBLIC_BASE_API_URL + '/workout', {
method: 'POST', method: 'POST',
headers: { headers: {
Authorization: 'Bearer ' + (await auth?.currentUser?.getIdToken()) Authorization: 'Bearer ' + (await auth?.currentUser?.getIdToken())
@@ -49,7 +50,7 @@
async function fetchWorkouts() { async function fetchWorkouts() {
try { try {
const response = await fetch('http://localhost:8080/workout', { const response = await fetch(PUBLIC_BASE_API_URL + '/workout', {
headers: { headers: {
Authorization: 'Bearer ' + (await auth?.currentUser?.getIdToken()) Authorization: 'Bearer ' + (await auth?.currentUser?.getIdToken())
}, },