lipre

Stream text files for live (coding) representations
Log | Files | Refs

commit d5a5052724f8e59e6165fa7f30ee7b9c99661676
parent 8302332b3b7e9506ce543eecca112b34825e2b50
Author: Vetle Haflan <vetle@haflan.dev>
Date:   Tue, 26 Jan 2021 23:28:47 +0100

Improve URL scheme and centralize static files

Diffstat:
M.gitignore | 2+-
Mlipre.go | 32++++++++++++++++----------------
Alipre.py | 72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dpredir.py | 72------------------------------------------------------------------------
Rui/index.html -> ui/dist/index.html | 0
Mui/package.json | 2+-
Mui/src/App.vue | 4++--
Mui/webpack.config.js | 2+-
8 files changed, 93 insertions(+), 93 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,3 +1,3 @@ -ui/lipre.js node_modules/ +dist/ .lpignore diff --git a/lipre.go b/lipre.go @@ -70,19 +70,21 @@ var wsUpgrader = websocket.Upgrader{ WriteBufferSize: 1024, } -func indexHandler(w http.ResponseWriter, r *http.Request) { - htmlData, err := ioutil.ReadFile("ui/index.html") - if err != nil { - panic(err) +// fileHandler looks in ui/dist directory for static files matching the path +// Writes a 404 message if not found +func fileHandler(w http.ResponseWriter, r *http.Request) { + filePath := r.URL.Path + if filePath == "/" { + filePath = "/index.html" } - w.Write(htmlData) - return -} - -func jsHandler(w http.ResponseWriter, r *http.Request) { - htmlData, err := ioutil.ReadFile("ui/lipre.js") + // Check if the file exists among the static assets + // At time of writing, this is only true for index.html and lipre.js, + // but code splitting may be introduced and change that + htmlData, err := ioutil.ReadFile(fmt.Sprintf("ui/dist%v", filePath)) if err != nil { - panic(err) + w.WriteHeader(http.StatusNotFound) + w.Write([]byte("404 :(")) + return } w.Write(htmlData) return @@ -129,11 +131,9 @@ func viewHandler(w http.ResponseWriter, r *http.Request) { func main() { fmt.Println("Server starting") router := mux.NewRouter() - router.HandleFunc("/", indexHandler) - router.HandleFunc("/room/{roomCode}", indexHandler) - router.HandleFunc("/lipre.js", jsHandler) // Temporary solution? - router.HandleFunc("/pres/{roomCode}", presentHandler) - router.HandleFunc("/view/{roomCode}", viewHandler) + router.HandleFunc("/ws/pres/{roomCode}", presentHandler) + router.HandleFunc("/ws/view/{roomCode}", viewHandler) + router.PathPrefix("/").HandlerFunc(fileHandler) http.Handle("/", router) log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", 8088), nil)) } diff --git a/lipre.py b/lipre.py @@ -0,0 +1,72 @@ +#!/usr/bin/python3 + +from os import listdir +from os.path import isfile, basename +import pyinotify +import json +import re +import sys +import websocket + +ignorefilelist = [] + +def should_ignore(filename): + for to_ignore in ignorefilelist: + # Make a regex from the asterix + match = re.search(to_ignore.replace("*", ".*"), filename) + if match: + return True + return False + +def send_file(filename): + if should_ignore(filename): + return + if isfile(filename): + contents = open(filename).read() + else: + contents = None + fileobj = { + 'name': filename, + 'contents': contents + } + print(f'Sending {filename}') + ws.send(json.dumps(fileobj)) + + +program = sys.argv[0] +if len(sys.argv) <= 1: + print(f'Use: {program} <room code>') + exit(1) +room_code = sys.argv[1] + +HOST='localhost:8088' +IGNOREFILE='.lpignore' + +ws = websocket.WebSocket() +ws.connect(f'ws://{HOST}/ws/pres/{room_code}') + +if isfile(IGNOREFILE): + ignorefilelist = [fn for fn in open(IGNOREFILE).read().split('\n') if fn] + +# Initial file upload +filenames = [fn for fn in listdir() if isfile(fn)] +for fn in filenames: + send_file(fn) + +# Listen for changes +class EventHandler(pyinotify.ProcessEvent): + def process_IN_CREATE(self, event): + fn = basename(event.pathname) + send_file(fn) + def process_IN_DELETE(self, event): + fn = basename(event.pathname) + send_file(fn) + def process_IN_MODIFY(self, event): + self.process_IN_CREATE(event) + +wm = pyinotify.WatchManager() +handler = EventHandler() +notifier = pyinotify.Notifier(wm, handler) +mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE | pyinotify.IN_MODIFY +wm.add_watch('.', mask) +notifier.loop() diff --git a/predir.py b/predir.py @@ -1,72 +0,0 @@ -#!/usr/bin/python3 - -from os import listdir -from os.path import isfile, basename -import pyinotify -import json -import re -import sys -import websocket - -ignorefilelist = [] - -def should_ignore(filename): - for to_ignore in ignorefilelist: - # Make a regex from the asterix - match = re.search(to_ignore.replace("*", ".*"), filename) - if match: - return True - return False - -def send_file(filename): - if should_ignore(filename): - return - if isfile(filename): - contents = open(filename).read() - else: - contents = None - fileobj = { - 'name': filename, - 'contents': contents - } - print(f'Sending {filename}') - ws.send(json.dumps(fileobj)) - - -program = sys.argv[0] -if len(sys.argv) <= 1: - print(f'Use: {program} <room code>') - exit(1) -room_code = sys.argv[1] - -HOST='localhost:8088' -IGNOREFILE='.lpignore' - -ws = websocket.WebSocket() -ws.connect(f'ws://{HOST}/pres/{room_code}') - -if isfile(IGNOREFILE): - ignorefilelist = [fn for fn in open(IGNOREFILE).read().split('\n') if fn] - -# Initial file upload -filenames = [fn for fn in listdir() if isfile(fn)] -for fn in filenames: - send_file(fn) - -# Listen for changes -class EventHandler(pyinotify.ProcessEvent): - def process_IN_CREATE(self, event): - fn = basename(event.pathname) - send_file(fn) - def process_IN_DELETE(self, event): - fn = basename(event.pathname) - send_file(fn) - def process_IN_MODIFY(self, event): - self.process_IN_CREATE(event) - -wm = pyinotify.WatchManager() -handler = EventHandler() -notifier = pyinotify.Notifier(wm, handler) -mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE | pyinotify.IN_MODIFY -wm.add_watch('.', mask) -notifier.loop() diff --git a/ui/index.html b/ui/dist/index.html diff --git a/ui/package.json b/ui/package.json @@ -2,7 +2,7 @@ "name": "lipre", "version": "1.0.0", "scripts": { - "clean": "rimraf lipre.js", + "clean": "rimraf dist/*.js", "build": "npm run clean && webpack --config webpack.config.js --mode production", "build-dev": "npm run clean && webpack --config webpack.config.js --mode development --watch" }, diff --git a/ui/src/App.vue b/ui/src/App.vue @@ -45,13 +45,13 @@ export default { connect() { this.connected = null let qParams = new URLSearchParams(window.location.search) - let roomCode = qParams.get("room") + let roomCode = qParams.get("r") if (!roomCode) { //this.status = "No room specified" // TODO: Use? this.connected = false return } - let url = `ws://${document.location.host}/view/${roomCode}` + let url = `ws://${document.location.host}/ws/view/${roomCode}` this.ws = new WebSocket(url) this.ws.onclose = () => { this.connected = false diff --git a/ui/webpack.config.js b/ui/webpack.config.js @@ -5,7 +5,7 @@ const VuetifyLoaderPlugin = require('vuetify-loader/lib/plugin'); module.exports = { entry: "./src/app.js", output: { - path: __dirname, + path: path.resolve(__dirname, "./dist/"), filename: "lipre.js" }, module: {