2021-05-02 22:49:16 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"goauthentik.io/internal/config"
|
2021-05-18 08:15:52 +00:00
|
|
|
"goauthentik.io/internal/constants"
|
2021-05-02 22:49:16 +00:00
|
|
|
staticWeb "goauthentik.io/web"
|
2021-07-13 09:06:51 +00:00
|
|
|
staticDocs "goauthentik.io/website"
|
2021-05-02 22:49:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (ws *WebServer) configureStatic() {
|
2021-05-18 08:15:52 +00:00
|
|
|
statRouter := ws.lh.NewRoute().Subrouter()
|
2021-07-03 19:43:37 +00:00
|
|
|
// Media files, always local
|
|
|
|
fs := http.FileServer(http.Dir(config.G.Paths.Media))
|
2021-06-21 19:21:35 +00:00
|
|
|
if config.G.Debug || config.G.Web.LoadLocalFiles {
|
2021-05-02 22:49:16 +00:00
|
|
|
ws.log.Debug("Using local static files")
|
2021-07-03 19:43:37 +00:00
|
|
|
statRouter.PathPrefix("/static/dist").Handler(http.StripPrefix("/static/dist", http.FileServer(http.Dir("./web/dist"))))
|
|
|
|
statRouter.PathPrefix("/static/authentik").Handler(http.StripPrefix("/static/authentik", http.FileServer(http.Dir("./web/authentik"))))
|
2021-07-13 17:09:16 +00:00
|
|
|
statRouter.PathPrefix("/help").Handler(http.StripPrefix("/help", http.FileServer(http.Dir("./website/help"))))
|
2021-05-02 22:49:16 +00:00
|
|
|
} else {
|
2021-05-18 08:15:52 +00:00
|
|
|
statRouter.Use(ws.staticHeaderMiddleware)
|
|
|
|
ws.log.Debug("Using packaged static files with aggressive caching")
|
2021-07-03 19:43:37 +00:00
|
|
|
statRouter.PathPrefix("/static/dist").Handler(http.StripPrefix("/static", http.FileServer(http.FS(staticWeb.StaticDist))))
|
|
|
|
statRouter.PathPrefix("/static/authentik").Handler(http.StripPrefix("/static", http.FileServer(http.FS(staticWeb.StaticAuthentik))))
|
2021-07-13 17:09:16 +00:00
|
|
|
statRouter.PathPrefix("/help").Handler(http.FileServer(http.FS(staticDocs.Help)))
|
2021-05-02 22:49:16 +00:00
|
|
|
}
|
2021-07-13 17:09:16 +00:00
|
|
|
statRouter.PathPrefix("/media").Handler(http.StripPrefix("/media", fs))
|
2021-05-02 22:49:16 +00:00
|
|
|
ws.lh.Path("/robots.txt").HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
rw.Header()["Content-Type"] = []string{"text/plain"}
|
|
|
|
rw.WriteHeader(200)
|
2021-07-18 14:12:57 +00:00
|
|
|
_, err := rw.Write(staticWeb.RobotsTxt)
|
|
|
|
if err != nil {
|
|
|
|
ws.log.WithError(err).Warning("failed to write response")
|
|
|
|
}
|
2021-05-02 22:49:16 +00:00
|
|
|
})
|
|
|
|
ws.lh.Path("/.well-known/security.txt").HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
rw.Header()["Content-Type"] = []string{"text/plain"}
|
|
|
|
rw.WriteHeader(200)
|
2021-07-18 14:12:57 +00:00
|
|
|
_, err := rw.Write(staticWeb.SecurityTxt)
|
|
|
|
if err != nil {
|
|
|
|
ws.log.WithError(err).Warning("failed to write response")
|
|
|
|
}
|
2021-05-02 22:49:16 +00:00
|
|
|
})
|
|
|
|
}
|
2021-05-18 08:15:52 +00:00
|
|
|
|
|
|
|
func (ws *WebServer) staticHeaderMiddleware(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Cache-Control", "\"public, no-transform\"")
|
|
|
|
w.Header().Set("X-authentik-version", constants.VERSION)
|
|
|
|
w.Header().Set("Vary", "X-authentik-version")
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|