From 44fe52fc5ab91ed0dc06943d12067443ea3f03a1 Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Tue, 17 Jan 2023 17:03:28 -0800 Subject: [PATCH] Make the public dir live inside data to make volume mounting easier --- config/constants.go | 3 +++ router/router.go | 2 +- test/automated/api/publicstatic.test.js | 31 ++++++++++++------------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/config/constants.go b/config/constants.go index 9bacbc093..2db026ded 100644 --- a/config/constants.go +++ b/config/constants.go @@ -27,4 +27,7 @@ var ( // CustomEmojiPath is the emoji directory. CustomEmojiPath = filepath.Join(DataDirectory, "emoji") + + // PublicFilesPath is the optional directory for hosting public files. + PublicFilesPath = filepath.Join(DataDirectory, "public") ) diff --git a/router/router.go b/router/router.go index c13ef1719..559448575 100644 --- a/router/router.go +++ b/router/router.go @@ -386,7 +386,7 @@ func Start() error { }) // Optional public static files - http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("./public")))) + http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir(config.PublicFilesPath)))) port := config.WebServerPort ip := config.WebServerIP diff --git a/test/automated/api/publicstatic.test.js b/test/automated/api/publicstatic.test.js index 189618cf9..e1e80fcce 100644 --- a/test/automated/api/publicstatic.test.js +++ b/test/automated/api/publicstatic.test.js @@ -4,7 +4,7 @@ const fs = require('fs'); const path = require('path'); const randomString = require('./lib/rand').randomString; -const publicPath = path.resolve(__dirname, '../../../public'); +const publicPath = path.resolve(__dirname, '../../../data/public'); const filename = randomString() + '.txt'; const fileContent = randomString(); @@ -15,12 +15,12 @@ test('random public static file does not exist', async (done) => { }); test('public directory is writable', async (done) => { - try { writeFileToPublic(); } catch (err) { if (err) { - if (err.code === "ENOENT") { // path does not exist + if (err.code === 'ENOENT') { + // path does not exist fs.mkdirSync(publicPath); writeFileToPublic(); } else { @@ -33,25 +33,24 @@ test('public directory is writable', async (done) => { }); test('public static file is accessible', async (done) => { - - request.get('/public/' + filename).expect(200).then((res) => { - expect(res.text).toEqual(fileContent); - done(); - }); - + request + .get('/public/' + filename) + .expect(200) + .then((res) => { + expect(res.text).toEqual(fileContent); + done(); + }); }); test('public static file is persistent and not locked', async (done) => { - fs.unlink(path.join(publicPath, filename), (err) => { - if (err) { throw err; } + if (err) { + throw err; + } }); done(); }); function writeFileToPublic() { - fs.writeFileSync( - path.join(publicPath, filename), - fileContent - ); - } \ No newline at end of file + fs.writeFileSync(path.join(publicPath, filename), fileContent); +}