Replace packr with go embed (#1751)

* Embed performer images
* Embed schema migrations
* Update dependencies
* Embed UI
* Remove remaining packr references
This commit is contained in:
WithoutPants
2021-09-22 13:08:34 +10:00
committed by GitHub
parent f292238e7f
commit 56111433a1
429 changed files with 39923 additions and 23061 deletions

View File

@@ -1,6 +1,7 @@
package api
import (
"embed"
"fmt"
"html/template"
"net/http"
@@ -10,15 +11,24 @@ import (
"github.com/stashapp/stash/pkg/session"
)
const loginRootDir = "ui/login"
const returnURLParam = "returnURL"
func getLoginPage(loginUIBox embed.FS) []byte {
data, err := loginUIBox.ReadFile(loginRootDir + "/login.html")
if err != nil {
panic(err)
}
return data
}
type loginTemplateData struct {
URL string
Error string
}
func redirectToLogin(w http.ResponseWriter, returnURL string, loginError string) {
data, _ := loginUIBox.Find("login.html")
func redirectToLogin(loginUIBox embed.FS, w http.ResponseWriter, returnURL string, loginError string) {
data := getLoginPage(loginUIBox)
templ, err := template.New("Login").Parse(string(data))
if err != nil {
http.Error(w, fmt.Sprintf("error: %s", err), http.StatusInternalServerError)
@@ -31,42 +41,48 @@ func redirectToLogin(w http.ResponseWriter, returnURL string, loginError string)
}
}
func getLoginHandler(w http.ResponseWriter, r *http.Request) {
if !config.GetInstance().HasCredentials() {
http.Redirect(w, r, "/", http.StatusFound)
return
}
func getLoginHandler(loginUIBox embed.FS) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if !config.GetInstance().HasCredentials() {
http.Redirect(w, r, "/", http.StatusFound)
return
}
redirectToLogin(w, r.URL.Query().Get(returnURLParam), "")
redirectToLogin(loginUIBox, w, r.URL.Query().Get(returnURLParam), "")
}
}
func handleLogin(w http.ResponseWriter, r *http.Request) {
url := r.FormValue(returnURLParam)
if url == "" {
url = "/"
}
func handleLogin(loginUIBox embed.FS) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
url := r.FormValue(returnURLParam)
if url == "" {
url = "/"
}
err := manager.GetInstance().SessionStore.Login(w, r)
if err == session.ErrInvalidCredentials {
// redirect back to the login page with an error
redirectToLogin(w, url, "Username or password is invalid")
return
}
err := manager.GetInstance().SessionStore.Login(w, r)
if err == session.ErrInvalidCredentials {
// redirect back to the login page with an error
redirectToLogin(loginUIBox, w, url, "Username or password is invalid")
return
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, url, http.StatusFound)
http.Redirect(w, r, url, http.StatusFound)
}
}
func handleLogout(w http.ResponseWriter, r *http.Request) {
if err := manager.GetInstance().SessionStore.Logout(w, r); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
func handleLogout(loginUIBox embed.FS) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := manager.GetInstance().SessionStore.Logout(w, r); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// redirect to the login page if credentials are required
getLoginHandler(w, r)
// redirect to the login page if credentials are required
getLoginHandler(loginUIBox)(w, r)
}
}