mirror of
https://github.com/hrfee/jfa-go.git
synced 2024-10-18 09:00:11 +00:00
Compare commits
No commits in common. "b3d7bc7704d7c6d20ba3ef20f5287cec625c9bd4" and "86bc26543a0a92d59d8303d1a94da7aafc786655" have entirely different histories.
b3d7bc7704
...
86bc26543a
@ -39,8 +39,9 @@ I chose to rewrite the python [jellyfin-accounts](https://github.com/hrfee/jelly
|
||||
|
||||
Available on the AUR as [jfa-go](https://aur.archlinux.org/packages/jfa-go/) or [jfa-go-git](https://aur.archlinux.org/packages/jfa-go-git/).
|
||||
|
||||
For other platforms, grab an archive from the release section for your platform (or nightly builds [here](https://builds.hrfee.pw/view/hrfee/jfa-go)), and extract the `jfa-go` executable to somewhere useful.
|
||||
* For \*nix/macOS users, `chmod +x jfa-go` then place it somewhere in your PATH like `/usr/bin`.
|
||||
For other platforms, grab an archive from the release section for your platform (or nightly builds [here](https://builds.hrfee.pw/view/hrfee/jfa-go)), and extract `jfa-go` and `data` to the same directory.
|
||||
* For linux users, you can place them inside `/opt/jfa-go` and then run
|
||||
`sudo ln -s /opt/jfa-go/jfa-go /usr/bin/jfa-go` to place it in your PATH.
|
||||
|
||||
Run the executable to start.
|
||||
|
||||
|
3
api.go
3
api.go
@ -1330,9 +1330,6 @@ func (app *appContext) GetLanguages(gc *gin.Context) {
|
||||
gc.JSON(200, resp)
|
||||
}
|
||||
|
||||
// @Summary Restarts the program. No response means success.
|
||||
// @Router /restart [post]
|
||||
// @tags Other
|
||||
func (app *appContext) restart(gc *gin.Context) {
|
||||
app.info.Println("Restarting...")
|
||||
err := app.Restart()
|
||||
|
@ -15,7 +15,7 @@ var emailEnabled = false
|
||||
func (app *appContext) GetPath(sect, key string) (fs.FS, string) {
|
||||
val := app.config.Section(sect).Key(key).MustString("")
|
||||
if strings.HasPrefix(val, "jfa-go:") {
|
||||
return localFS, strings.TrimPrefix(val, "jfa-go:")
|
||||
return localFS, "data/" + strings.TrimPrefix(val, "jfa-go:")
|
||||
}
|
||||
return app.systemFS, val
|
||||
}
|
||||
|
@ -867,7 +867,7 @@
|
||||
"requires_restart": true,
|
||||
"type": "text",
|
||||
"value": "",
|
||||
"description": "The path to a directory which following the same form as the internal 'lang/' directory. See GitHub for more info."
|
||||
"description": "The path to a directory CONTAINING a 'lang/' directory, which follow the same form as the internal one. See GitHub for more info."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
136
email.go
136
email.go
@ -153,28 +153,6 @@ func (emailer *Emailer) NewSMTP(server string, port int, username, password stri
|
||||
}
|
||||
}
|
||||
|
||||
func (emailer *Emailer) construct(app *appContext, section, keyFragment string, data map[string]interface{}) (html, text string, err error) {
|
||||
var tpl *template.Template
|
||||
for _, key := range []string{"html", "text"} {
|
||||
filesystem, fpath := app.GetPath(section, keyFragment+key)
|
||||
tpl, err = template.ParseFS(filesystem, fpath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var tplData bytes.Buffer
|
||||
err = tpl.Execute(&tplData, data)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if key == "html" {
|
||||
html = tplData.String()
|
||||
} else {
|
||||
text = tplData.String()
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (emailer *Emailer) constructConfirmation(code, username, key string, app *appContext) (*Email, error) {
|
||||
email := &Email{
|
||||
subject: app.config.Section("email_confirmation").Key("subject").MustString(emailer.lang.EmailConfirmation.get("title")),
|
||||
@ -182,8 +160,15 @@ func (emailer *Emailer) constructConfirmation(code, username, key string, app *a
|
||||
message := app.config.Section("email").Key("message").String()
|
||||
inviteLink := app.config.Section("invite_emails").Key("url_base").String()
|
||||
inviteLink = fmt.Sprintf("%s/%s?key=%s", inviteLink, code, key)
|
||||
var err error
|
||||
email.html, email.text, err = emailer.construct(app, "email_confirmation", "email_", map[string]interface{}{
|
||||
|
||||
for _, key := range []string{"html", "text"} {
|
||||
filesystem, fpath := app.GetPath("email_confirmation", "email_"+key)
|
||||
tpl, err := template.ParseFS(filesystem, fpath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tplData bytes.Buffer
|
||||
err = tpl.Execute(&tplData, map[string]string{
|
||||
"helloUser": emailer.lang.Strings.format("helloUser", username),
|
||||
"clickBelow": emailer.lang.EmailConfirmation.get("clickBelow"),
|
||||
"ifItWasNotYou": emailer.lang.Strings.get("ifItWasNotYou"),
|
||||
@ -194,6 +179,12 @@ func (emailer *Emailer) constructConfirmation(code, username, key string, app *a
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if key == "html" {
|
||||
email.html = tplData.String()
|
||||
} else {
|
||||
email.text = tplData.String()
|
||||
}
|
||||
}
|
||||
return email, nil
|
||||
}
|
||||
|
||||
@ -206,8 +197,15 @@ func (emailer *Emailer) constructInvite(code string, invite Invite, app *appCont
|
||||
message := app.config.Section("email").Key("message").String()
|
||||
inviteLink := app.config.Section("invite_emails").Key("url_base").String()
|
||||
inviteLink = fmt.Sprintf("%s/%s", inviteLink, code)
|
||||
var err error
|
||||
email.html, email.text, err = emailer.construct(app, "invite_emails", "email_", map[string]interface{}{
|
||||
|
||||
for _, key := range []string{"html", "text"} {
|
||||
filesystem, fpath := app.GetPath("invite_emails", "email_"+key)
|
||||
tpl, err := template.ParseFS(filesystem, fpath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tplData bytes.Buffer
|
||||
err = tpl.Execute(&tplData, map[string]string{
|
||||
"hello": emailer.lang.InviteEmail.get("hello"),
|
||||
"youHaveBeenInvited": emailer.lang.InviteEmail.get("youHaveBeenInvited"),
|
||||
"toJoin": emailer.lang.InviteEmail.get("toJoin"),
|
||||
@ -219,6 +217,12 @@ func (emailer *Emailer) constructInvite(code string, invite Invite, app *appCont
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if key == "html" {
|
||||
email.html = tplData.String()
|
||||
} else {
|
||||
email.text = tplData.String()
|
||||
}
|
||||
}
|
||||
return email, nil
|
||||
}
|
||||
|
||||
@ -227,8 +231,14 @@ func (emailer *Emailer) constructExpiry(code string, invite Invite, app *appCont
|
||||
subject: emailer.lang.InviteExpiry.get("title"),
|
||||
}
|
||||
expiry := app.formatDatetime(invite.ValidTill)
|
||||
var err error
|
||||
email.html, email.text, err = emailer.construct(app, "notifications", "expiry_", map[string]interface{}{
|
||||
for _, key := range []string{"html", "text"} {
|
||||
filesystem, fpath := app.GetPath("notifications", "expiry_"+key)
|
||||
tpl, err := template.ParseFS(filesystem, fpath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tplData bytes.Buffer
|
||||
err = tpl.Execute(&tplData, map[string]string{
|
||||
"inviteExpired": emailer.lang.InviteExpiry.get("inviteExpired"),
|
||||
"expiredAt": emailer.lang.InviteExpiry.format("expiredAt", "\""+code+"\"", expiry),
|
||||
"notificationNotice": emailer.lang.InviteExpiry.get("notificationNotice"),
|
||||
@ -236,6 +246,12 @@ func (emailer *Emailer) constructExpiry(code string, invite Invite, app *appCont
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if key == "html" {
|
||||
email.html = tplData.String()
|
||||
} else {
|
||||
email.text = tplData.String()
|
||||
}
|
||||
}
|
||||
return email, nil
|
||||
}
|
||||
|
||||
@ -250,8 +266,14 @@ func (emailer *Emailer) constructCreated(code, username, address string, invite
|
||||
} else {
|
||||
tplAddress = address
|
||||
}
|
||||
var err error
|
||||
email.html, email.text, err = emailer.construct(app, "notifications", "created_", map[string]interface{}{
|
||||
for _, key := range []string{"html", "text"} {
|
||||
filesystem, fpath := app.GetPath("notifications", "created_"+key)
|
||||
tpl, err := template.ParseFS(filesystem, fpath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tplData bytes.Buffer
|
||||
err = tpl.Execute(&tplData, map[string]string{
|
||||
"aUserWasCreated": emailer.lang.UserCreated.format("aUserWasCreated", "\""+code+"\""),
|
||||
"name": emailer.lang.Strings.get("name"),
|
||||
"address": emailer.lang.Strings.get("emailAddress"),
|
||||
@ -264,6 +286,12 @@ func (emailer *Emailer) constructCreated(code, username, address string, invite
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if key == "html" {
|
||||
email.html = tplData.String()
|
||||
} else {
|
||||
email.text = tplData.String()
|
||||
}
|
||||
}
|
||||
return email, nil
|
||||
}
|
||||
|
||||
@ -273,8 +301,14 @@ func (emailer *Emailer) constructReset(pwr PasswordReset, app *appContext) (*Ema
|
||||
}
|
||||
d, t, expiresIn := emailer.formatExpiry(pwr.Expiry, true, app.datePattern, app.timePattern)
|
||||
message := app.config.Section("email").Key("message").String()
|
||||
var err error
|
||||
email.html, email.text, err = emailer.construct(app, "password_resets", "email_", map[string]interface{}{
|
||||
for _, key := range []string{"html", "text"} {
|
||||
filesystem, fpath := app.GetPath("password_resets", "email_"+key)
|
||||
tpl, err := template.ParseFS(filesystem, fpath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tplData bytes.Buffer
|
||||
err = tpl.Execute(&tplData, map[string]string{
|
||||
"helloUser": emailer.lang.Strings.format("helloUser", pwr.Username),
|
||||
"someoneHasRequestedReset": emailer.lang.PasswordReset.get("someoneHasRequestedReset"),
|
||||
"ifItWasYou": emailer.lang.PasswordReset.get("ifItWasYou"),
|
||||
@ -287,6 +321,12 @@ func (emailer *Emailer) constructReset(pwr PasswordReset, app *appContext) (*Ema
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if key == "html" {
|
||||
email.html = tplData.String()
|
||||
} else {
|
||||
email.text = tplData.String()
|
||||
}
|
||||
}
|
||||
return email, nil
|
||||
}
|
||||
|
||||
@ -294,8 +334,14 @@ func (emailer *Emailer) constructDeleted(reason string, app *appContext) (*Email
|
||||
email := &Email{
|
||||
subject: app.config.Section("deletion").Key("subject").MustString(emailer.lang.UserDeleted.get("title")),
|
||||
}
|
||||
var err error
|
||||
email.html, email.text, err = emailer.construct(app, "deletion", "email_", map[string]interface{}{
|
||||
for _, key := range []string{"html", "text"} {
|
||||
filesystem, fpath := app.GetPath("deletion", "email_"+key)
|
||||
tpl, err := template.ParseFS(filesystem, fpath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tplData bytes.Buffer
|
||||
err = tpl.Execute(&tplData, map[string]string{
|
||||
"yourAccountWasDeleted": emailer.lang.UserDeleted.get("yourAccountWasDeleted"),
|
||||
"reason": emailer.lang.UserDeleted.get("reason"),
|
||||
"reasonVal": reason,
|
||||
@ -303,6 +349,12 @@ func (emailer *Emailer) constructDeleted(reason string, app *appContext) (*Email
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if key == "html" {
|
||||
email.html = tplData.String()
|
||||
} else {
|
||||
email.text = tplData.String()
|
||||
}
|
||||
}
|
||||
return email, nil
|
||||
}
|
||||
|
||||
@ -310,8 +362,14 @@ func (emailer *Emailer) constructWelcome(username string, app *appContext) (*Ema
|
||||
email := &Email{
|
||||
subject: app.config.Section("welcome_email").Key("subject").MustString(emailer.lang.WelcomeEmail.get("title")),
|
||||
}
|
||||
var err error
|
||||
email.html, email.text, err = emailer.construct(app, "welcome_email", "email_", map[string]interface{}{
|
||||
for _, key := range []string{"html", "text"} {
|
||||
filesystem, fpath := app.GetPath("welcome_email", "email_"+key)
|
||||
tpl, err := template.ParseFS(filesystem, fpath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tplData bytes.Buffer
|
||||
err = tpl.Execute(&tplData, map[string]string{
|
||||
"welcome": emailer.lang.WelcomeEmail.get("welcome"),
|
||||
"youCanLoginWith": emailer.lang.WelcomeEmail.get("youCanLoginWith"),
|
||||
"jellyfinURL": emailer.lang.WelcomeEmail.get("jellyfinURL"),
|
||||
@ -323,6 +381,12 @@ func (emailer *Emailer) constructWelcome(username string, app *appContext) (*Ema
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if key == "html" {
|
||||
email.html = tplData.String()
|
||||
} else {
|
||||
email.text = tplData.String()
|
||||
}
|
||||
}
|
||||
return email, nil
|
||||
}
|
||||
|
||||
|
27
embed.py
27
embed.py
@ -14,31 +14,16 @@ with open("embed.go", "w") as f:
|
||||
f.write("""package main
|
||||
import (
|
||||
"embed"
|
||||
"io/fs"
|
||||
"log"
|
||||
)
|
||||
|
||||
//go:embed data data/html data/web data/web/css data/web/js
|
||||
var loFS embed.FS
|
||||
var localFS embed.FS
|
||||
|
||||
//go:embed lang/common lang/admin lang/email lang/form lang/setup
|
||||
var laFS embed.FS
|
||||
var langFS embed.FS
|
||||
|
||||
var langFS rewriteFS
|
||||
var localFS rewriteFS
|
||||
|
||||
type rewriteFS struct {
|
||||
fs embed.FS
|
||||
prefix string
|
||||
}
|
||||
|
||||
func (l rewriteFS) Open(name string) (fs.File, error) { return l.fs.Open(l.prefix + name) }
|
||||
func (l rewriteFS) ReadDir(name string) ([]fs.DirEntry, error) { return l.fs.ReadDir(l.prefix + name) }
|
||||
func (l rewriteFS) ReadFile(name string) ([]byte, error) { return l.fs.ReadFile(l.prefix + name) }
|
||||
|
||||
func loadFilesystems() {
|
||||
langFS = rewriteFS{laFS, "lang/"}
|
||||
localFS = rewriteFS{loFS, "data/"}
|
||||
func loadLocalFS() {
|
||||
log.Println("Using internal storage")
|
||||
}""")
|
||||
elif EMBED in falses:
|
||||
@ -53,9 +38,9 @@ import (
|
||||
var localFS fs.FS
|
||||
var langFS fs.FS
|
||||
|
||||
func loadFilesystems() {
|
||||
func loadLocalFS() {
|
||||
log.Println("Using external storage")
|
||||
executable, _ := os.Executable()
|
||||
localFS = os.DirFS(filepath.Join(filepath.Dir(executable), "data"))
|
||||
langFS = os.DirFS(filepath.Join(filepath.Dir(executable), "data", "lang"))
|
||||
localFS = os.DirFS(filepath.Dir(executable))
|
||||
langFS = os.DirFS(filepath.Join(filepath.Dir(executable), "data"))
|
||||
}""")
|
||||
|
180
main.go
180
main.go
@ -7,6 +7,7 @@ import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"io/fs"
|
||||
"log"
|
||||
@ -21,27 +22,20 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-contrib/pprof"
|
||||
"github.com/gin-contrib/static"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/hrfee/jfa-go/common"
|
||||
_ "github.com/hrfee/jfa-go/docs"
|
||||
"github.com/hrfee/jfa-go/mediabrowser"
|
||||
"github.com/hrfee/jfa-go/ombi"
|
||||
"github.com/lithammer/shortuuid/v3"
|
||||
"github.com/logrusorgru/aurora/v3"
|
||||
swaggerFiles "github.com/swaggo/files"
|
||||
ginSwagger "github.com/swaggo/gin-swagger"
|
||||
"gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
var (
|
||||
PLATFORM string = runtime.GOOS
|
||||
SOCK string = "jfa-go.sock"
|
||||
SRV *http.Server
|
||||
RESTART chan bool
|
||||
DATA, CONFIG, HOST *string
|
||||
PORT *int
|
||||
DEBUG *bool
|
||||
TEST bool
|
||||
SWAGGER *bool
|
||||
)
|
||||
|
||||
var serverTypes = map[string]string{
|
||||
"jellyfin": "Jellyfin",
|
||||
"emby": "Emby (experimental)",
|
||||
@ -87,6 +81,31 @@ type appContext struct {
|
||||
URLBase string
|
||||
}
|
||||
|
||||
func (app *appContext) loadHTML(router *gin.Engine) {
|
||||
customPath := app.config.Section("files").Key("html_templates").MustString("")
|
||||
templatePath := "data/html"
|
||||
htmlFiles, err := fs.ReadDir(localFS, templatePath)
|
||||
if err != nil {
|
||||
app.err.Fatalf("Couldn't access template directory: \"%s\"", templatePath)
|
||||
return
|
||||
}
|
||||
loadFiles := make([]string, len(htmlFiles))
|
||||
for i, f := range htmlFiles {
|
||||
if _, err := os.Stat(filepath.Join(customPath, f.Name())); os.IsNotExist(err) {
|
||||
app.debug.Printf("Using default \"%s\"", f.Name())
|
||||
loadFiles[i] = filepath.Join(templatePath, f.Name())
|
||||
} else {
|
||||
app.info.Printf("Using custom \"%s\"", f.Name())
|
||||
loadFiles[i] = filepath.Join(filepath.Join(customPath, f.Name()))
|
||||
}
|
||||
}
|
||||
tmpl, err := template.ParseFS(localFS, loadFiles...)
|
||||
if err != nil {
|
||||
app.err.Fatalf("Failed to load templates: %v", err)
|
||||
}
|
||||
router.SetHTMLTemplate(tmpl)
|
||||
}
|
||||
|
||||
func generateSecret(length int) (string, error) {
|
||||
bytes := make([]byte, length)
|
||||
_, err := rand.Read(bytes)
|
||||
@ -96,6 +115,46 @@ func generateSecret(length int) (string, error) {
|
||||
return base64.URLEncoding.EncodeToString(bytes), err
|
||||
}
|
||||
|
||||
func setGinLogger(router *gin.Engine, debugMode bool) {
|
||||
if debugMode {
|
||||
router.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
|
||||
return fmt.Sprintf("[GIN/DEBUG] %s: %s(%s) => %d in %s; %s\n",
|
||||
param.TimeStamp.Format("15:04:05"),
|
||||
param.Method,
|
||||
param.Path,
|
||||
param.StatusCode,
|
||||
param.Latency,
|
||||
func() string {
|
||||
if param.ErrorMessage != "" {
|
||||
return "Error: " + param.ErrorMessage
|
||||
}
|
||||
return ""
|
||||
}(),
|
||||
)
|
||||
}))
|
||||
} else {
|
||||
router.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
|
||||
return fmt.Sprintf("[GIN] %s(%s) => %d\n",
|
||||
param.Method,
|
||||
param.Path,
|
||||
param.StatusCode,
|
||||
)
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
PLATFORM string = runtime.GOOS
|
||||
SOCK string = "jfa-go.sock"
|
||||
SRV *http.Server
|
||||
RESTART chan bool
|
||||
DATA, CONFIG, HOST *string
|
||||
PORT *int
|
||||
DEBUG *bool
|
||||
TEST bool
|
||||
SWAGGER *bool
|
||||
)
|
||||
|
||||
func test(app *appContext) {
|
||||
fmt.Printf("\n\n----\n\n")
|
||||
settings := map[string]interface{}{
|
||||
@ -132,16 +191,18 @@ func start(asDaemon, firstCall bool) {
|
||||
app := new(appContext)
|
||||
|
||||
/*
|
||||
set default config and data paths
|
||||
data: Contains invites.json, emails.json, user_profile.json, etc.
|
||||
config: config.ini. Usually in data, but can be changed via -config.
|
||||
localFS is jfa-go's internal data. On external builds, the directory is named "data" and placed next to the executable.
|
||||
set default config, data and local paths
|
||||
also, confusing naming here. data_path is not the internal 'data' directory, rather the users .config/jfa-go folder.
|
||||
localFS/data is the internal 'data' directory.
|
||||
*/
|
||||
userConfigDir, _ := os.UserConfigDir()
|
||||
app.dataPath = filepath.Join(userConfigDir, "jfa-go")
|
||||
app.configPath = filepath.Join(app.dataPath, "config.ini")
|
||||
// executable, _ := os.Executable()
|
||||
// localFS = os.DirFS(filepath.Join(filepath.Dir(executable), "data"))
|
||||
// langFS = os.DirFS(filepath.Join(filepath.Dir(executable), "data", "lang"))
|
||||
app.systemFS = os.DirFS("/")
|
||||
// gin-static doesn't just take a plain http.FileSystem, so we implement it's ServeFileSystem. See static.go.
|
||||
// wfs := os.DirFS(filepath.Join(filepath.Dir(executable), "data", "web"))
|
||||
app.webFS = httpFS{
|
||||
hfs: http.FS(localFS),
|
||||
fs: localFS,
|
||||
@ -202,7 +263,7 @@ func start(asDaemon, firstCall bool) {
|
||||
}
|
||||
if _, err := os.Stat(app.configPath); os.IsNotExist(err) {
|
||||
firstRun = true
|
||||
dConfig, err := fs.ReadFile(localFS, "config-default.ini")
|
||||
dConfig, err := fs.ReadFile(localFS, "data/config-default.ini")
|
||||
if err != nil {
|
||||
app.err.Fatalf("Couldn't find default config file")
|
||||
}
|
||||
@ -277,10 +338,10 @@ func start(asDaemon, firstCall bool) {
|
||||
}()
|
||||
}
|
||||
|
||||
app.storage.lang.CommonPath = "common"
|
||||
app.storage.lang.FormPath = "form"
|
||||
app.storage.lang.AdminPath = "admin"
|
||||
app.storage.lang.EmailPath = "email"
|
||||
app.storage.lang.CommonPath = "lang/common"
|
||||
app.storage.lang.FormPath = "lang/form"
|
||||
app.storage.lang.AdminPath = "lang/admin"
|
||||
app.storage.lang.EmailPath = "lang/email"
|
||||
externalLang := app.config.Section("files").Key("lang_files").MustString("")
|
||||
var err error
|
||||
if externalLang == "" {
|
||||
@ -366,7 +427,7 @@ func start(asDaemon, firstCall bool) {
|
||||
|
||||
}
|
||||
|
||||
app.configBasePath = "config-base.json"
|
||||
app.configBasePath = "data/config-base.json"
|
||||
configBase, _ := fs.ReadFile(localFS, app.configBasePath)
|
||||
json.Unmarshal(configBase, &app.configBase)
|
||||
|
||||
@ -515,23 +576,80 @@ func start(asDaemon, firstCall bool) {
|
||||
} else {
|
||||
debugMode = false
|
||||
address = "0.0.0.0:8056"
|
||||
app.storage.lang.SetupPath = "setup"
|
||||
app.storage.lang.SetupPath = "lang/setup"
|
||||
err := app.storage.loadLangSetup(langFS)
|
||||
if err != nil {
|
||||
app.info.Fatalf("Failed to load language files: %+v\n", err)
|
||||
}
|
||||
}
|
||||
app.info.Println("Initializing router")
|
||||
router := app.loadRouter(address, debugMode)
|
||||
app.info.Println("Loading routes")
|
||||
if debugMode {
|
||||
gin.SetMode(gin.DebugMode)
|
||||
} else {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
router := gin.New()
|
||||
|
||||
setGinLogger(router, debugMode)
|
||||
|
||||
router.Use(gin.Recovery())
|
||||
app.loadHTML(router)
|
||||
router.Use(static.Serve("/", app.webFS))
|
||||
router.NoRoute(app.NoRouteHandler)
|
||||
if debugMode {
|
||||
app.debug.Println("Loading pprof")
|
||||
pprof.Register(router)
|
||||
}
|
||||
router.GET("/lang/:page", app.GetLanguages)
|
||||
if !firstRun {
|
||||
app.loadRoutes(router)
|
||||
router.GET("/", app.AdminPage)
|
||||
router.GET("/accounts", app.AdminPage)
|
||||
router.GET("/settings", app.AdminPage)
|
||||
router.GET("/lang/:page/:file", app.ServeLang)
|
||||
router.GET("/token/login", app.getTokenLogin)
|
||||
router.GET("/token/refresh", app.getTokenRefresh)
|
||||
router.POST("/newUser", app.NewUser)
|
||||
router.Use(static.Serve("/invite/", app.webFS))
|
||||
router.GET("/invite/:invCode", app.InviteProxy)
|
||||
if *SWAGGER {
|
||||
app.info.Print(aurora.Magenta("\n\nWARNING: Swagger should not be used on a public instance.\n\n"))
|
||||
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||||
}
|
||||
api := router.Group("/", app.webAuth())
|
||||
router.POST("/logout", app.Logout)
|
||||
api.DELETE("/users", app.DeleteUser)
|
||||
api.GET("/users", app.GetUsers)
|
||||
api.POST("/users", app.NewUserAdmin)
|
||||
api.POST("/invites", app.GenerateInvite)
|
||||
api.GET("/invites", app.GetInvites)
|
||||
api.DELETE("/invites", app.DeleteInvite)
|
||||
api.POST("/invites/profile", app.SetProfile)
|
||||
api.GET("/profiles", app.GetProfiles)
|
||||
api.POST("/profiles/default", app.SetDefaultProfile)
|
||||
api.POST("/profiles", app.CreateProfile)
|
||||
api.DELETE("/profiles", app.DeleteProfile)
|
||||
api.POST("/invites/notify", app.SetNotify)
|
||||
api.POST("/users/emails", app.ModifyEmails)
|
||||
// api.POST("/setDefaults", app.SetDefaults)
|
||||
api.POST("/users/settings", app.ApplySettings)
|
||||
api.GET("/config", app.GetConfig)
|
||||
api.POST("/config", app.ModifyConfig)
|
||||
api.POST("/restart", app.restart)
|
||||
if app.config.Section("ombi").Key("enabled").MustBool(false) {
|
||||
api.GET("/ombi/users", app.OmbiUsers)
|
||||
api.POST("/ombi/defaults", app.SetOmbiDefaults)
|
||||
}
|
||||
app.info.Printf("Starting router @ %s", address)
|
||||
} else {
|
||||
app.loadSetup(router)
|
||||
router.GET("/", app.ServeSetup)
|
||||
router.POST("/jellyfin/test", app.TestJF)
|
||||
router.POST("/config", app.ModifyConfig)
|
||||
app.info.Printf("Loading setup @ %s", address)
|
||||
}
|
||||
SRV = &http.Server{
|
||||
Addr: address,
|
||||
Handler: router,
|
||||
}
|
||||
go func() {
|
||||
if app.config.Section("advanced").Key("tls").MustBool(false) {
|
||||
cert := app.config.Section("advanced").Key("tls_cert").MustString("")
|
||||
@ -553,9 +671,9 @@ func start(asDaemon, firstCall bool) {
|
||||
}
|
||||
}()
|
||||
for range RESTART {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
cntx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
if err := SRV.Shutdown(ctx); err != nil {
|
||||
if err := SRV.Shutdown(cntx); err != nil {
|
||||
app.err.Fatalf("Server shutdown error: %s", err)
|
||||
}
|
||||
return
|
||||
@ -634,7 +752,7 @@ func main() {
|
||||
if flagPassed("test") {
|
||||
TEST = true
|
||||
}
|
||||
loadFilesystems()
|
||||
loadLocalFS()
|
||||
if flagPassed("start") {
|
||||
args := []string{}
|
||||
for i, f := range os.Args {
|
||||
|
142
router.go
142
router.go
@ -1,142 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gin-contrib/pprof"
|
||||
"github.com/gin-contrib/static"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/logrusorgru/aurora/v3"
|
||||
swaggerFiles "github.com/swaggo/files"
|
||||
ginSwagger "github.com/swaggo/gin-swagger"
|
||||
)
|
||||
|
||||
// loads HTML templates. If [files]/html_templates is set, alternative files inside the directory are loaded in place of the internal templates.
|
||||
func (app *appContext) loadHTML(router *gin.Engine) {
|
||||
customPath := app.config.Section("files").Key("html_templates").MustString("")
|
||||
templatePath := "html"
|
||||
htmlFiles, err := fs.ReadDir(localFS, templatePath)
|
||||
if err != nil {
|
||||
app.err.Fatalf("Couldn't access template directory: \"%s\"", templatePath)
|
||||
return
|
||||
}
|
||||
loadFiles := make([]string, len(htmlFiles))
|
||||
for i, f := range htmlFiles {
|
||||
if _, err := os.Stat(filepath.Join(customPath, f.Name())); os.IsNotExist(err) {
|
||||
app.debug.Printf("Using default \"%s\"", f.Name())
|
||||
loadFiles[i] = filepath.Join(templatePath, f.Name())
|
||||
} else {
|
||||
app.info.Printf("Using custom \"%s\"", f.Name())
|
||||
loadFiles[i] = filepath.Join(filepath.Join(customPath, f.Name()))
|
||||
}
|
||||
}
|
||||
tmpl, err := template.ParseFS(localFS, loadFiles...)
|
||||
if err != nil {
|
||||
app.err.Fatalf("Failed to load templates: %v", err)
|
||||
}
|
||||
router.SetHTMLTemplate(tmpl)
|
||||
}
|
||||
|
||||
// sets gin logger.
|
||||
func setGinLogger(router *gin.Engine, debugMode bool) {
|
||||
if debugMode {
|
||||
router.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
|
||||
return fmt.Sprintf("[GIN/DEBUG] %s: %s(%s) => %d in %s; %s\n",
|
||||
param.TimeStamp.Format("15:04:05"),
|
||||
param.Method,
|
||||
param.Path,
|
||||
param.StatusCode,
|
||||
param.Latency,
|
||||
func() string {
|
||||
if param.ErrorMessage != "" {
|
||||
return "Error: " + param.ErrorMessage
|
||||
}
|
||||
return ""
|
||||
}(),
|
||||
)
|
||||
}))
|
||||
} else {
|
||||
router.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
|
||||
return fmt.Sprintf("[GIN] %s(%s) => %d\n",
|
||||
param.Method,
|
||||
param.Path,
|
||||
param.StatusCode,
|
||||
)
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
func (app *appContext) loadRouter(address string, debug bool) *gin.Engine {
|
||||
if debug {
|
||||
gin.SetMode(gin.DebugMode)
|
||||
} else {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
router := gin.New()
|
||||
|
||||
setGinLogger(router, debug)
|
||||
|
||||
router.Use(gin.Recovery())
|
||||
app.loadHTML(router)
|
||||
router.Use(static.Serve("/", app.webFS))
|
||||
router.NoRoute(app.NoRouteHandler)
|
||||
if debug {
|
||||
app.debug.Println("Loading pprof")
|
||||
pprof.Register(router)
|
||||
}
|
||||
SRV = &http.Server{
|
||||
Addr: address,
|
||||
Handler: router,
|
||||
}
|
||||
return router
|
||||
}
|
||||
|
||||
func (app *appContext) loadRoutes(router *gin.Engine) {
|
||||
router.GET("/", app.AdminPage)
|
||||
router.GET("/accounts", app.AdminPage)
|
||||
router.GET("/settings", app.AdminPage)
|
||||
router.GET("/lang/:page/:file", app.ServeLang)
|
||||
router.GET("/token/login", app.getTokenLogin)
|
||||
router.GET("/token/refresh", app.getTokenRefresh)
|
||||
router.POST("/newUser", app.NewUser)
|
||||
router.Use(static.Serve("/invite/", app.webFS))
|
||||
router.GET("/invite/:invCode", app.InviteProxy)
|
||||
if *SWAGGER {
|
||||
app.info.Print(aurora.Magenta("\n\nWARNING: Swagger should not be used on a public instance.\n\n"))
|
||||
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||||
}
|
||||
api := router.Group("/", app.webAuth())
|
||||
router.POST("/logout", app.Logout)
|
||||
api.DELETE("/users", app.DeleteUser)
|
||||
api.GET("/users", app.GetUsers)
|
||||
api.POST("/users", app.NewUserAdmin)
|
||||
api.POST("/invites", app.GenerateInvite)
|
||||
api.GET("/invites", app.GetInvites)
|
||||
api.DELETE("/invites", app.DeleteInvite)
|
||||
api.POST("/invites/profile", app.SetProfile)
|
||||
api.GET("/profiles", app.GetProfiles)
|
||||
api.POST("/profiles/default", app.SetDefaultProfile)
|
||||
api.POST("/profiles", app.CreateProfile)
|
||||
api.DELETE("/profiles", app.DeleteProfile)
|
||||
api.POST("/invites/notify", app.SetNotify)
|
||||
api.POST("/users/emails", app.ModifyEmails)
|
||||
api.POST("/users/settings", app.ApplySettings)
|
||||
api.GET("/config", app.GetConfig)
|
||||
api.POST("/config", app.ModifyConfig)
|
||||
api.POST("/restart", app.restart)
|
||||
if app.config.Section("ombi").Key("enabled").MustBool(false) {
|
||||
api.GET("/ombi/users", app.OmbiUsers)
|
||||
api.POST("/ombi/defaults", app.SetOmbiDefaults)
|
||||
}
|
||||
}
|
||||
|
||||
func (app *appContext) loadSetup(router *gin.Engine) {
|
||||
router.GET("/", app.ServeSetup)
|
||||
router.POST("/jellyfin/test", app.TestJF)
|
||||
router.POST("/config", app.ModifyConfig)
|
||||
}
|
@ -14,12 +14,12 @@ type httpFS struct {
|
||||
}
|
||||
|
||||
func (f httpFS) Open(name string) (http.File, error) {
|
||||
return f.hfs.Open("web" + name)
|
||||
return f.hfs.Open("data/web" + name)
|
||||
}
|
||||
|
||||
func (f httpFS) Exists(prefix string, filepath string) bool {
|
||||
if p := strings.TrimPrefix(filepath, prefix); len(p) < len(filepath) {
|
||||
stats, err := fs.Stat(f.fs, "web/"+p)
|
||||
stats, err := fs.Stat(f.fs, "data/web/"+p)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
26
views.go
26
views.go
@ -43,19 +43,14 @@ func (app *appContext) pushResources(gc *gin.Context, admin bool) {
|
||||
gc.Header("Link", cssHeader)
|
||||
}
|
||||
|
||||
func (app *appContext) getLang(gc *gin.Context, chosen string) string {
|
||||
lang := gc.Query("lang")
|
||||
if lang == "" {
|
||||
lang = chosen
|
||||
} else if _, ok := app.storage.lang.Admin[lang]; !ok {
|
||||
lang = chosen
|
||||
}
|
||||
return lang
|
||||
}
|
||||
|
||||
func (app *appContext) AdminPage(gc *gin.Context) {
|
||||
app.pushResources(gc, true)
|
||||
lang := app.getLang(gc, app.storage.lang.chosenAdminLang)
|
||||
lang := gc.Query("lang")
|
||||
if lang == "" {
|
||||
lang = app.storage.lang.chosenAdminLang
|
||||
} else if _, ok := app.storage.lang.Admin[lang]; !ok {
|
||||
lang = app.storage.lang.chosenAdminLang
|
||||
}
|
||||
emailEnabled, _ := app.config.Section("invite_emails").Key("enabled").Bool()
|
||||
notificationsEnabled, _ := app.config.Section("notifications").Key("enabled").Bool()
|
||||
ombiEnabled := app.config.Section("ombi").Key("enabled").MustBool(false)
|
||||
@ -78,7 +73,12 @@ func (app *appContext) AdminPage(gc *gin.Context) {
|
||||
func (app *appContext) InviteProxy(gc *gin.Context) {
|
||||
app.pushResources(gc, false)
|
||||
code := gc.Param("invCode")
|
||||
lang := app.getLang(gc, app.storage.lang.chosenFormLang)
|
||||
lang := gc.Query("lang")
|
||||
if lang == "" {
|
||||
lang = app.storage.lang.chosenFormLang
|
||||
} else if _, ok := app.storage.lang.Form[lang]; !ok {
|
||||
lang = app.storage.lang.chosenFormLang
|
||||
}
|
||||
/* Don't actually check if the invite is valid, just if it exists, just so the page loads quicker. Invite is actually checked on submit anyway. */
|
||||
// if app.checkInvite(code, false, "") {
|
||||
inv, ok := app.storage.invites[code]
|
||||
@ -89,7 +89,7 @@ func (app *appContext) InviteProxy(gc *gin.Context) {
|
||||
})
|
||||
return
|
||||
}
|
||||
if key := gc.Query("key"); key != "" && app.config.Section("email_confirmation").Key("enabled").MustBool(false) {
|
||||
if key := gc.Query("key"); key != "" {
|
||||
validKey := false
|
||||
keyIndex := -1
|
||||
for i, k := range inv.Keys {
|
||||
|
Loading…
Reference in New Issue
Block a user