-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
54 lines (42 loc) · 1.4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// @author: Brian Wojtczak
// @copyright: 2024 by Brian Wojtczak
// @license: BSD-style license found in the LICENSE file
package main
import (
"embed"
"github.com/k42-software/go-altcha/http" // altcha
"log"
"net/http"
)
//go:embed index.html
//go:embed protected.html
var files embed.FS
var fileServer = http.FileServer(http.FS(files))
// Simple middleware to disable browser caching
func noCache(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "private, no-cache, no-store, must-revalidate")
next.ServeHTTP(w, r)
})
}
func main() {
// Serve the javascript file, which contains the altcha code.
http.HandleFunc("/altcha.js", altcha.ServeJavascript)
http.HandleFunc("/altcha.js.license", altcha.ServeJavascript)
http.HandleFunc("/altcha.min.js", altcha.ServeJavascript)
http.HandleFunc("/altcha.min.js.license", altcha.ServeJavascript)
// Serve the protected file, but only if they get the challenge correct!
http.Handle(
"/protected.html",
// This middleware protects the request
altcha.ProtectForm(
// This is only run for successful requests.
fileServer,
),
)
// Serve the index file, which contains the HTML form.
http.Handle("/", fileServer)
// Start the server
log.Println("Running on http://localhost:3003/")
log.Println(http.ListenAndServe(":3003", noCache(http.DefaultServeMux)))
}