Using a more standard way to serve static files

This commit is contained in:
Jesús Espino 2020-10-18 02:46:25 +02:00
parent 53b051816c
commit 7c94602c11
4 changed files with 26 additions and 47 deletions

View file

@ -5,10 +5,10 @@
<meta charset="UTF-8">
<title><%= htmlWebpackPlugin.options.title %></title>
<link rel="stylesheet" href="/easymde.min.css">
<link rel="stylesheet" href="/main.css">
<link rel="stylesheet" href="/images.css">
<link rel="stylesheet" href="/colors.css">
<link rel="stylesheet" href="/static/easymde.min.css">
<link rel="stylesheet" href="/static/main.css">
<link rel="stylesheet" href="/static/images.css">
<link rel="stylesheet" href="/static/colors.css">
</head>
<body>

View file

@ -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)

View file

@ -23,6 +23,9 @@ export default function App() {
<Route path="/login">
<LoginPage />
</Route>
<Route path="/">
<BoardPage />
</Route>
<Route path="/board">
<BoardPage />
</Route>

View file

@ -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
}
}