| 25 | } |
| 26 | |
| 27 | func (a *App) loadRoutes() (http.Handler, error) { |
| 28 | tmpl, err := template.New("").ParseFS(a.files, "templates/*") |
| 29 | if err != nil { |
| 30 | return nil, fmt.Errorf("failed to parse templates: %w", err) |
| 31 | } |
| 32 | |
| 33 | static, err := fs.Sub(a.files, "static") |
| 34 | if err != nil { |
| 35 | return nil, fmt.Errorf("failed to subdir static: %w", err) |
| 36 | } |
| 37 | |
| 38 | // Create a new router |
| 39 | router := http.NewServeMux() |
| 40 | |
| 41 | // This is the static fileserver. |
| 42 | router.Handle("GET /static/", http.StripPrefix("/static", http.FileServerFS(static))) |
| 43 | |
| 44 | a.loadPages(router, tmpl) |
| 45 | |
| 46 | // Create a middleware chain from the Chain function of the |
| 47 | // middleware package |
| 48 | chain := middleware.Chain( |
| 49 | middleware.Logging(a.logger), |
| 50 | ) |
| 51 | |
| 52 | return chain(router), nil |
| 53 | } |