pain and suffering

This commit is contained in:
Stepan Usatiuk
2023-12-30 20:43:21 +01:00
parent d7cd581bff
commit 65e50d9e02
5 changed files with 149 additions and 87 deletions

View File

@@ -8,6 +8,7 @@
"start": "parcel", "start": "parcel",
"build": "parcel build" "build": "parcel build"
}, },
"publicUrl": "/app",
"browserslist": "> 0.5%, last 2 versions, not dead", "browserslist": "> 0.5%, last 2 versions, not dead",
"dependencies": { "dependencies": {
"jwt-decode": "^4.0.0", "jwt-decode": "^4.0.0",

View File

@@ -39,95 +39,98 @@ import { Chat } from "./Chat";
import { ChatEdit } from "./ChatEdit"; import { ChatEdit } from "./ChatEdit";
import { Haters } from "./Haters"; import { Haters } from "./Haters";
const router = createBrowserRouter([ const router = createBrowserRouter(
{ [
path: "/", {
loader: async () => { path: "/",
if (getToken() == null) { loader: async () => {
return redirect("/login"); if (getToken() == null) {
} else { return redirect("/login");
return redirect("/home"); } else {
} return redirect("/home");
}
},
}, },
}, {
{ path: "/home",
path: "/home", loader: homeLoader,
loader: homeLoader, action: homeAction,
action: homeAction, element: <Home />,
element: <Home />, children: [
children: [ { path: "feed", element: <Feed />, loader: feedLoader },
{ path: "feed", element: <Feed />, loader: feedLoader }, // { path: "messages", element: <Messages /> },
// { path: "messages", element: <Messages /> }, {
{ path: "messages/chats",
path: "messages/chats", element: <Chats />,
element: <Chats />, loader: chatListLoader,
loader: chatListLoader, },
}, {
{ path: "messages/chats/new",
path: "messages/chats/new", element: <ChatCreate />,
element: <ChatCreate />, loader: newChatLoader,
loader: newChatLoader, action: newChatAction,
action: newChatAction, },
}, {
{ path: "messages/chat/:id",
path: "messages/chat/:id", element: <Chat />,
element: <Chat />, loader: chatLoader,
loader: chatLoader, action: chatAction,
action: chatAction, },
}, {
{ path: "messages/chat/:id/edit",
path: "messages/chat/:id/edit", element: <ChatEdit />,
element: <ChatEdit />, loader: editChatLoader,
loader: editChatLoader, action: editChatAction,
action: editChatAction, },
}, {
{ path: "users",
path: "users", element: <UserList />,
element: <UserList />, loader: userListLoader,
loader: userListLoader, action: userListAction,
action: userListAction, },
}, {
{ path: "profile",
path: "profile", loader: profileLoader,
loader: profileLoader, action: profileSelfAction,
action: profileSelfAction, element: <Profile self={true} />,
element: <Profile self={true} />, },
}, {
{ path: "profile/:username",
path: "profile/:username", loader: profileLoader,
loader: profileLoader, // action: profileSelfAction,
// action: profileSelfAction, element: <Profile self={false} />,
element: <Profile self={false} />, },
}, {
{ path: "haters",
path: "haters", element: <Haters />,
element: <Haters />, },
}, ],
],
},
{
path: "/login",
element: <Login />,
loader: async () => {
if (getToken()) {
return redirect("/");
}
return null;
}, },
action: loginAction, {
}, path: "/login",
{ element: <Login />,
path: "/signup", loader: async () => {
element: <Signup />, if (getToken()) {
loader: async () => { return redirect("/");
if (getToken()) { }
return redirect("/"); return null;
} },
return null; action: loginAction,
}, },
action: signupAction, {
}, path: "/signup",
]); element: <Signup />,
loader: async () => {
if (getToken()) {
return redirect("/");
}
return null;
},
action: signupAction,
},
],
{ basename: "/app" },
);
export function App() { export function App() {
return ( return (

View File

@@ -3,7 +3,14 @@
import { jwtDecode } from "jwt-decode"; import { jwtDecode } from "jwt-decode";
import { isError } from "./dto"; import { isError } from "./dto";
const apiRoot: string = "http://localhost:8080"; declare const process: {
env: {
NODE_ENV: string;
};
};
const apiRoot: string =
process.env.NODE_ENV == "production" ? "/" : "http://localhost:8080";
let token: string | null; let token: string | null;

View File

@@ -0,0 +1,50 @@
package com.usatiuk.tjv.y.server;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.CacheControl;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.ResourceResolver;
import org.springframework.web.servlet.resource.ResourceResolverChain;
import java.time.Duration;
import java.util.List;
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
static class AppResourceResolver implements ResourceResolver {
@Override
public Resource resolveResource(HttpServletRequest request, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) {
ClassPathResource res = new ClassPathResource("/app/" + requestPath);
if (res.exists()) return res;
ClassPathResource indexRes = new ClassPathResource("/app/index.html");
if (indexRes.exists()) return indexRes;
return null;
}
@Override
public String resolveUrlPath(String resourcePath, List<? extends Resource> locations, ResourceResolverChain chain) {
return null;
}
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/app/", "/app/index.html");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/app/**")
.setCacheControl(CacheControl.maxAge(Duration.ofDays(365)))
.resourceChain(true)
.addResolver(new AppResourceResolver());
}
}

View File

@@ -59,6 +59,7 @@ public class WebSecurityConfig {
.authorizeHttpRequests((authorize) -> authorize .authorizeHttpRequests((authorize) -> authorize
.requestMatchers(mvc.pattern(HttpMethod.POST, "/person")).permitAll() .requestMatchers(mvc.pattern(HttpMethod.POST, "/person")).permitAll()
.requestMatchers(mvc.pattern(HttpMethod.POST, "/token")).permitAll() .requestMatchers(mvc.pattern(HttpMethod.POST, "/token")).permitAll()
.requestMatchers(mvc.pattern("/app/**")).permitAll()
.requestMatchers(mvc.pattern("/swagger-ui*/**")).permitAll() .requestMatchers(mvc.pattern("/swagger-ui*/**")).permitAll()
.requestMatchers(mvc.pattern("/v3/**")).permitAll() .requestMatchers(mvc.pattern("/v3/**")).permitAll()
.requestMatchers(mvc.pattern("/error")).permitAll() .requestMatchers(mvc.pattern("/error")).permitAll()