2023-09-03 00:23:48 +02:00
|
|
|
package frontend
|
|
|
|
|
|
|
|
import (
|
2023-09-03 04:11:56 +02:00
|
|
|
"embed"
|
2023-09-04 17:32:45 +02:00
|
|
|
"encoding/json"
|
2023-09-03 00:23:48 +02:00
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2023-09-03 04:11:56 +02:00
|
|
|
"path/filepath"
|
2023-09-03 00:23:48 +02:00
|
|
|
|
|
|
|
"git.sleepycat.moe/sam/mercury/frontend"
|
|
|
|
"git.sleepycat.moe/sam/mercury/internal/database"
|
2023-09-04 17:32:45 +02:00
|
|
|
"git.sleepycat.moe/sam/mercury/web/api"
|
2023-09-03 00:23:48 +02:00
|
|
|
"git.sleepycat.moe/sam/mercury/web/app"
|
2023-09-03 04:11:56 +02:00
|
|
|
"github.com/go-chi/chi/v5"
|
2023-09-03 00:23:48 +02:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
vueglue "github.com/torenware/vite-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed app.html
|
|
|
|
var htmlTemplate string
|
|
|
|
|
2023-09-03 04:11:56 +02:00
|
|
|
//go:embed assets
|
|
|
|
var assets embed.FS
|
|
|
|
|
2023-09-03 00:23:48 +02:00
|
|
|
type Frontend struct {
|
|
|
|
*app.App
|
|
|
|
glue *vueglue.VueGlue
|
|
|
|
urlPrefix string
|
|
|
|
fileServer http.Handler
|
|
|
|
tmpl *template.Template
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(app *app.App) *Frontend {
|
|
|
|
fe := &Frontend{
|
|
|
|
App: app,
|
|
|
|
}
|
|
|
|
|
2023-09-04 03:33:13 +02:00
|
|
|
if app.AppConfig.Core.Dev {
|
2023-09-03 00:23:48 +02:00
|
|
|
glue, err := vueglue.NewVueGlue(&vueglue.ViteConfig{
|
|
|
|
Environment: "development",
|
|
|
|
AssetsPath: "frontend",
|
2023-09-15 16:33:08 +02:00
|
|
|
EntryPoint: "src/main.tsx",
|
|
|
|
Platform: "vue",
|
2023-09-03 00:23:48 +02:00
|
|
|
FS: os.DirFS("frontend"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("Creating vite glue")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fe.glue = glue
|
|
|
|
fe.urlPrefix = "/src/*"
|
|
|
|
} else {
|
|
|
|
glue, err := vueglue.NewVueGlue(&vueglue.ViteConfig{
|
|
|
|
Environment: "production",
|
|
|
|
URLPrefix: "/assets/",
|
|
|
|
AssetsPath: "dist",
|
2023-09-15 16:33:08 +02:00
|
|
|
EntryPoint: "src/main.tsx",
|
|
|
|
Platform: "vue",
|
2023-09-03 00:23:48 +02:00
|
|
|
FS: frontend.Embed,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("Creating vite glue")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fe.glue = glue
|
|
|
|
fe.urlPrefix = "/assets/*"
|
|
|
|
}
|
|
|
|
|
|
|
|
fileServer, err := fe.glue.FileServer()
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("Creating vite file server")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fe.fileServer = fileServer
|
|
|
|
|
|
|
|
tmpl, err := template.New("app").Parse(htmlTemplate)
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("Parsing frontend HTML template")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fe.tmpl = tmpl
|
|
|
|
|
|
|
|
return fe
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *Frontend) AssetsPath() string { return app.urlPrefix }
|
|
|
|
|
|
|
|
func (app *Frontend) ServeAssets(w http.ResponseWriter, r *http.Request) {
|
|
|
|
app.fileServer.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *Frontend) ServeFrontend(w http.ResponseWriter, r *http.Request) {
|
2023-09-04 17:32:45 +02:00
|
|
|
_, ok := app.TokenFromContext(r.Context())
|
|
|
|
if !ok {
|
|
|
|
http.Redirect(w, r, "/auth/login", http.StatusSeeOther)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
app.serveFrontend(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *Frontend) ServeUser(w http.ResponseWriter, r *http.Request) {
|
|
|
|
_, ok := app.TokenFromContext(r.Context())
|
|
|
|
if !ok {
|
|
|
|
username := chi.URLParam(r, "username")
|
|
|
|
http.Redirect(w, r, "/@"+username, http.StatusSeeOther)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
app.serveFrontend(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *Frontend) ServeStatus(w http.ResponseWriter, r *http.Request) {
|
|
|
|
_, ok := app.TokenFromContext(r.Context())
|
|
|
|
if !ok {
|
|
|
|
username := chi.URLParam(r, "username")
|
|
|
|
postID := chi.URLParam(r, "postID")
|
|
|
|
http.Redirect(w, r, "/@"+username+"/posts/"+postID, http.StatusSeeOther)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
app.serveFrontend(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *Frontend) serveFrontend(w http.ResponseWriter, r *http.Request) {
|
|
|
|
token, ok := app.TokenFromContext(r.Context())
|
|
|
|
if !ok {
|
|
|
|
log.Error().Msg("app.serveFrontend was called without a token being set")
|
|
|
|
http.Redirect(w, r, "/auth/login", http.StatusSeeOther)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
acct, err := app.Account().ByID(r.Context(), token.UserID)
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("fetching account")
|
|
|
|
app.ErrorTemplate(w, r, "Internal server error", "An internal server error occurred. Please try again later.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(api.DBAccountToSelfAccount(acct))
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("marshaling account json")
|
|
|
|
app.ErrorTemplate(w, r, "Internal server error", "An internal server error occurred. Please try again later.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-03 00:23:48 +02:00
|
|
|
w.Header().Set("Content-Type", "text/html")
|
2023-09-04 17:32:45 +02:00
|
|
|
err = app.tmpl.Execute(w, map[string]any{
|
|
|
|
"Config": database.DefaultConfig,
|
|
|
|
"Vue": app.glue,
|
|
|
|
"AccountData": template.HTML(b),
|
2023-09-03 00:23:48 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("executing frontend template")
|
|
|
|
}
|
|
|
|
}
|
2023-09-03 04:11:56 +02:00
|
|
|
|
|
|
|
func (app *Frontend) ServeStaticAssets(w http.ResponseWriter, r *http.Request) {
|
2023-09-04 03:33:13 +02:00
|
|
|
if app.AppConfig.Core.Dev {
|
2023-09-03 04:11:56 +02:00
|
|
|
// TODO: this is unsafe
|
|
|
|
path := filepath.Join("web/frontend/assets/", chi.URLParam(r, "*"))
|
|
|
|
http.ServeFile(w, r, path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = assets
|
|
|
|
}
|