-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (57 loc) · 1.45 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"embed"
"html/template"
"io/fs"
"log"
"net/http"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
//go:embed all:ui/tmpl
var TmplFiles embed.FS
//go:embed all:ui/static
var StaticFiles embed.FS
func main() {
app := &app{
Templates: template.Must(template.ParseFS(TmplFiles, "ui/tmpl/*.html")),
}
r := chi.NewRouter()
// Middlewares
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(middleware.Heartbeat("/healthcheck"))
r.Use(middleware.Timeout(3 * time.Second))
r.Get("/", app.index)
r.Get("/favicon.ico", app.favicon)
// Serve remaining static files
sub, err := fs.Sub(StaticFiles, "ui")
if err != nil {
panic(err)
}
fs := http.FileServer(http.FS(sub))
//r.Mount("/static", http.StripPrefix("ui/static", fs))
r.Mount("/static", fs)
// HTTPS server struct utilizing application
srv := &http.Server{
Addr: ":8080",
Handler: r,
}
// Do the thing
err = srv.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
}
type app struct {
Templates *template.Template
}
func (app *app) index(w http.ResponseWriter, r *http.Request) {
app.Templates.ExecuteTemplate(w, "index.html", nil)
}
func (app *app) favicon(w http.ResponseWriter, r *http.Request) {
http.ServeFileFS(w, r, StaticFiles, "static/media/favicon.ico")
}