Skip to content

Commit

Permalink
fix: Actually serve embedded static assets
Browse files Browse the repository at this point in the history
  • Loading branch information
cluttrdev committed Feb 27, 2023
1 parent 9c5a2dc commit 5d8fc74
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.1.1] - 2023-02-27

### Fixed

- File server now uses embedded static assets

## [0.1.0] - 2023-02-26

Initial version, providing a working proof-of-concept.

[0.1.1]: https://github.com/cluttrdev/showdown/compare/v0.1.1...v0.1.0
[0.1.0]: https://github.com/cluttrdev/showdown/releases/tag/v0.1.0


24 changes: 10 additions & 14 deletions cmd/showdown/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,33 @@ package main

import (
"embed"
"io/fs"
"log"
"net/http"

"golang.org/x/net/websocket"
)

//go:generate cp -r ../../web/ ./assets
//go:embed assets/*
//go:generate cp -r ../../web/static/ ./assets
//go:embed assets/index.html
//go:embed assets/js/client.js
var assets embed.FS

func (app *Application) routes() *http.ServeMux {
mux := http.NewServeMux()

fileServer := http.FileServer(http.Dir("./web/static/"))
mux.Handle("/static/", http.StripPrefix("/static/", fileServer))
sub, err := fs.Sub(assets, "assets")
if err != nil {
panic(err)
}

mux.HandleFunc("/", app.root)
fileServer := http.FileServer(http.FS(sub))
mux.Handle("/", fileServer)
mux.Handle("/ws", websocket.Handler(app.socket))

return mux
}

func (app *Application) root(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}

http.ServeFile(w, r, "./web/index.html")
}

func (app *Application) socket(ws *websocket.Conn) {
if err := app.sendTitle(ws, app.file); err != nil {
log.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion web/index.html → web/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8">
<title></title>

<script src="/static/js/client.js" defer></script>
<script src="js/client.js" defer></script>
</head>
<body>
<main class="markdown-content">
Expand Down

0 comments on commit 5d8fc74

Please sign in to comment.