From 7c94602c11d9b2ad0f206d795fefdcb35a16d062 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Sun, 18 Oct 2020 02:46:25 +0200 Subject: [PATCH] Using a more standard way to serve static files --- html-templates/page.ejs | 8 ++++---- server/web/webserver.go | 45 +++++++++-------------------------------- src/client/app.tsx | 3 +++ webpack.common.js | 17 ++++++++-------- 4 files changed, 26 insertions(+), 47 deletions(-) diff --git a/html-templates/page.ejs b/html-templates/page.ejs index 4723fc27c..4c05b65e1 100644 --- a/html-templates/page.ejs +++ b/html-templates/page.ejs @@ -5,10 +5,10 @@ <%= htmlWebpackPlugin.options.title %> - - - - + + + + diff --git a/server/web/webserver.go b/server/web/webserver.go index b431d1a8e..5f641ef37 100644 --- a/server/web/webserver.go +++ b/server/web/webserver.go @@ -5,6 +5,7 @@ import ( "log" "net/http" "os" + "path" "path/filepath" "github.com/gorilla/mux" @@ -31,17 +32,6 @@ func NewWebServer(rootPath string, port int, ssl bool) *WebServer { ssl: ssl, } - // Static files - ws.handleDefault(r, "/") - ws.handleStaticFile(r, "/login", "index.html", "text/html; charset=utf-8") - ws.handleStaticFile(r, "/board", "index.html", "text/html; charset=utf-8") - ws.handleStaticFile(r, "/main.js", "main.js", "text/javascript; charset=utf-8") - ws.handleStaticFile(r, "/boardPage.js", "boardPage.js", "text/javascript; charset=utf-8") - ws.handleStaticFile(r, "/favicon.ico", "static/favicon.svg", "image/svg+xml; charset=utf-8") - ws.handleStaticFile(r, "/easymde.min.css", "static/easymde.min.css", "text/css") - ws.handleStaticFile(r, "/main.css", "static/main.css", "text/css") - ws.handleStaticFile(r, "/colors.css", "static/colors.css", "text/css") - ws.handleStaticFile(r, "/images.css", "static/images.css", "text/css") return ws } @@ -49,7 +39,16 @@ func (ws *WebServer) AddRoutes(rs RoutedService) { rs.RegisterRoutes(ws.router) } +func (ws *WebServer) registerRoutes() { + ws.router.PathPrefix("/static").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(filepath.Join(ws.rootPath, "static"))))) + ws.router.PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/html; charset=utf-8") + http.ServeFile(w, r, path.Join(ws.rootPath, "index.html")) + }) +} + func (ws *WebServer) Start() error { + ws.registerRoutes() http.Handle("/", ws.router) urlPort := fmt.Sprintf(`:%d`, ws.port) @@ -70,30 +69,6 @@ func (ws *WebServer) Start() error { return nil } -// ---------------------------------------------------------------------------------------------------- -// HTTP handlers - -func (ws *WebServer) serveWebFile(w http.ResponseWriter, r *http.Request, relativeFilePath string) { - folderPath := ws.rootPath - filePath := filepath.Join(folderPath, relativeFilePath) - http.ServeFile(w, r, filePath) -} - -func (ws *WebServer) handleStaticFile(r *mux.Router, requestPath string, filePath string, contentType string) { - r.HandleFunc(requestPath, func(w http.ResponseWriter, r *http.Request) { - log.Printf("handleStaticFile: %s", requestPath) - w.Header().Set("Content-Type", contentType) - ws.serveWebFile(w, r, filePath) - }) -} - -func (ws *WebServer) handleDefault(r *mux.Router, requestPath string) { - r.HandleFunc(requestPath, func(w http.ResponseWriter, r *http.Request) { - log.Printf("handleDefault") - http.Redirect(w, r, "/board", http.StatusFound) - }) -} - // FileExists returns true if a file exists at the path func fileExists(path string) bool { _, err := os.Stat(path) diff --git a/src/client/app.tsx b/src/client/app.tsx index e0c781904..e125e9688 100644 --- a/src/client/app.tsx +++ b/src/client/app.tsx @@ -23,6 +23,9 @@ export default function App() { + + + diff --git a/webpack.common.js b/webpack.common.js index 3b622b122..7d02467e9 100644 --- a/webpack.common.js +++ b/webpack.common.js @@ -26,12 +26,12 @@ function makeCommonConfig() { loader: "file-loader", }, { - test: /\.s[ac]ss$/i, - use: [ - 'style-loader', - 'css-loader', - 'sass-loader', - ], + test: /\.s[ac]ss$/i, + use: [ + 'style-loader', + 'css-loader', + 'sass-loader', + ], }, { test: /\.(tsx?|js|jsx|html)$/, @@ -60,14 +60,15 @@ function makeCommonConfig() { title: "OCTO", chunks: ["main"], template: "html-templates/page.ejs", - filename: 'index.html' + filename: 'index.html', + publicPath: '/' }), ], entry: { main: "./src/client/main.tsx", }, output: { - filename: "[name].js", + filename: "static/[name].js", path: outpath } }