Dockerize and cleanup

This commit is contained in:
Neale Pickett 2018-09-20 00:18:04 +00:00
parent 7ff206cbda
commit bc6b373659
4 changed files with 64 additions and 20 deletions

View File

@ -1,14 +1,8 @@
FROM neale/eris FROM alpine AS builder
RUN apk --no-cache add go libc-dev
RUN apk --no-cache add lua5.1 lua5.2 lua5.3 COPY src /src
RUN ln -s lua5.2 /usr/bin/lua RUN go build -o /mothd /src/*.go
# Install MOTH. This could be less obtuse.
COPY www /moth/www/
COPY bin /moth/bin/
COPY src/moth-init /moth/init
RUN ln -s ../state/puzzles.json /moth/www/puzzles.json && \
ln -s ../state/points.json /moth/www/points.json
CMD ["/moth/init"]
FROM alpine
COPY --from=builder /mothd /mothd
ENTRYPOINT [ "/mothd" ]

View File

@ -125,9 +125,9 @@ func (ctx *Instance) Maintenance(maintenanceInterval time.Duration) {
ctx.Tidy() ctx.Tidy()
select { select {
case <-ctx.update: case <-ctx.update:
log.Print("Forced update") // log.Print("Forced update")
case <-time.After(maintenanceInterval): case <-time.After(maintenanceInterval):
log.Print("Housekeeping...") // log.Print("Housekeeping...")
} }
} }
} }

View File

@ -47,7 +47,7 @@ func main() {
) )
listen := flag.String( listen := flag.String(
"listen", "listen",
":80", ":8000",
"[host]:port to bind and listen", "[host]:port to bind and listen",
) )
flag.Parse() flag.Parse()

View File

@ -289,7 +289,7 @@ document.addEventListener("DOMContentLoaded", init);
) )
} }
func staticPuzzles(w http.ResponseWriter) { func staticPuzzleList(w http.ResponseWriter) {
ShowHtml( ShowHtml(
w, Success, w, Success,
"Open Puzzles", "Open Puzzles",
@ -328,12 +328,12 @@ function render(obj) {
l.appendChild(i); l.appendChild(i);
if (points === 0) { if (points === 0) {
i.textContent = ""; i.textContent = "";
} else { } else {
var a = document.createElement('a'); var a = document.createElement('a');
i.appendChild(a); i.appendChild(a);
a.textContent = points; a.textContent = points;
a.href = cat + "/" + id + "/index.html"; a.href = "puzzle.html?cat=" + cat + "&points=" + points + "&pid=" + id;
} }
} }
@ -359,6 +359,54 @@ document.addEventListener("DOMContentLoaded", init);
) )
} }
func staticPuzzle(w http.ResponseWriter) {
ShowHtml(
w, Success,
"Open Puzzles",
`
<section>
<div id="body">Loading...</div>
</section>
<form action="answer" method="post">
<input type="hidden" name="cat">
<input type="hidden" name="points">
Team ID: <input type="text" name="id"> <br>
Answer: <input type="text" name="answer"> <br>
<input type="submit" value="Submit">
</form>
<script>
function render(obj) {
let body = document.getElementById("body");
body.innerHTML = obj.body;
console.log("XXX: Munge relative URLs (src= and href=) in body")
}
function init() {
let params = new URLSearchParams(window.location.search);
let categoryName = params.get("cat");
let points = params.get("points");
let puzzleId = params.get("pid");
let fn = "content/" + categoryName + "/" + puzzleId + "/puzzle.json";
fetch(fn)
.then(function(resp) {
return resp.json();
}).then(function(obj) {
render(obj);
}).catch(function(err) {
console.log("Error", err);
});
document.querySelector("body > h1").innerText = categoryName + " " + points
document.querySelector("input[name=cat]").value = categoryName;
document.querySelector("input[name=points]").value = points;
}
document.addEventListener("DOMContentLoaded", init);
</script>
`,
)
}
func tryServeFile(w http.ResponseWriter, req *http.Request, path string) bool { func tryServeFile(w http.ResponseWriter, req *http.Request, path string) bool {
f, err := os.Open(path) f, err := os.Open(path)
if err != nil { if err != nil {
@ -398,7 +446,9 @@ func ServeStatic(w http.ResponseWriter, req *http.Request, resourcesDir string)
case "/scoreboard.html": case "/scoreboard.html":
staticScoreboard(w) staticScoreboard(w)
case "/puzzle-list.html": case "/puzzle-list.html":
staticPuzzles(w) staticPuzzleList(w)
case "/puzzle.html":
staticPuzzle(w)
default: default:
http.NotFound(w, req) http.NotFound(w, req)
} }