diff --git a/.github/workflows/_buildx.yml b/.github/workflows/_buildx.yml
index 4677faceb..224c674e1 100644
--- a/.github/workflows/_buildx.yml
+++ b/.github/workflows/_buildx.yml
@@ -23,10 +23,10 @@ jobs:
mv binaries/shiori_linux_amd64_v1 binaries/shiori_linux_amd64
gzip -d -S binaries/.gz__ -r .
chmod 755 binaries/shiori_linux_*/shiori
- - name: Buildx
- working-directory: .github/workflows/docker
+ - name: Prepare master push tags
+ if: ${{ startsWith(github.ref, 'refs/tags/v') }}
run: |
- echo "${{ secrets.GITHUB_TOKEN }}" | docker login -u "${{ github.repository_owner }}" --password-stdin ghcr.io
+ echo "tag_flags=--tag ${{ github.ref }}" >> $GITHUB_ENV
REPO=ghcr.io/${{ github.repository }}
TAG=$(git describe --tags)
if [ -z "$(git tag --points-at HEAD)" ]
@@ -35,5 +35,19 @@ jobs:
else
TAG2="latest"
fi
+ echo "tag_flags=--tag $REPO:$TAG --tag $REPO:$TAG2" >> $GITHUB_ENV
+
+ - name: Prepare pull request tags
+ if: github.event_name == 'pull_request'
+ run: |
+ echo "tag_flags=--tag ${{ github.ref }}" >> $GITHUB_ENV
+ REPO=ghcr.io/${{ github.repository }}
+ echo "tag_flags=--tag $REPO:pr-${{ github.event.pull_request.number }}" >> $GITHUB_ENV
+
+ - name: Buildx
+ working-directory: .github/workflows/docker
+ run: |
+ set -x
+ echo "${{ secrets.GITHUB_TOKEN }}" | docker login -u "${{ github.repository_owner }}" --password-stdin ghcr.io
docker buildx create --use --name builder
- docker buildx build -f Dockerfile.ci --platform=linux/amd64,arm64,linux/arm/v7 --push --output=type=registry --tag $REPO:$TAG --tag $REPO:$TAG2 .
+ docker buildx build -f Dockerfile.ci --platform=linux/amd64,arm64,linux/arm/v7 --push ${{ env.tag_flags }} .
diff --git a/.github/workflows/_delete-registry-tag.yml b/.github/workflows/_delete-registry-tag.yml
new file mode 100644
index 000000000..665090ea9
--- /dev/null
+++ b/.github/workflows/_delete-registry-tag.yml
@@ -0,0 +1,14 @@
+name: Delete registry tag
+
+on: workflow_call
+
+jobs:
+ purge-image:
+ name: Delete tag
+ runs-on: ubuntu-latest
+ steps:
+ - uses: chipkent/action-cleanup-package@1316a66015b82d745b57acbb6c570f2bb1d108f9 # v1.0.3
+ with:
+ package-name: ${{ github.event.repository.name }}
+ tag: ${{ env.TAG_NAME }}
+ github-token: ${{ secrets.GITHUB_TOKEN }}
diff --git a/.github/workflows/docker/Dockerfile.ci b/.github/workflows/docker/Dockerfile.ci
index 44120f63c..31c206aff 100644
--- a/.github/workflows/docker/Dockerfile.ci
+++ b/.github/workflows/docker/Dockerfile.ci
@@ -8,4 +8,4 @@ WORKDIR /shiori
EXPOSE 8080
ENV SHIORI_DIR=/shiori
ENTRYPOINT ["/usr/bin/shiori"]
-CMD ["serve"]
\ No newline at end of file
+CMD ["server"]
diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml
index cf9213027..5818fa905 100644
--- a/.github/workflows/pull_request.yml
+++ b/.github/workflows/pull_request.yml
@@ -2,7 +2,8 @@ name: 'Pull Request'
on:
pull_request:
- branches: master
+ branches:
+ - master
concurrency:
group: ci-tests-${{ github.ref }}-1
@@ -13,3 +14,9 @@ jobs:
uses: ./.github/workflows/_golangci-lint.yml
call-test:
uses: ./.github/workflows/_test.yml
+ call-gorelease:
+ needs: [call-lint, call-test]
+ uses: ./.github/workflows/_gorelease.yml
+ call-buildx:
+ needs: call-gorelease
+ uses: ./.github/workflows/_buildx.yml
diff --git a/.github/workflows/pull_request_closed.yml b/.github/workflows/pull_request_closed.yml
new file mode 100644
index 000000000..2cb0f27ee
--- /dev/null
+++ b/.github/workflows/pull_request_closed.yml
@@ -0,0 +1,12 @@
+name: 'Clean up Docker images from PR'
+
+on:
+ pull_request:
+ types:
+ - closed
+
+jobs:
+ delete-tag:
+ uses: ./.github/workflows/_delete-registry-tag.yml
+ with:
+ TAG_NAME: pr-${{ github.event.pull_request.number }}
diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml
index 6f024f03f..ef9967a42 100644
--- a/.github/workflows/push.yml
+++ b/.github/workflows/push.yml
@@ -15,6 +15,7 @@ jobs:
call-test:
uses: ./.github/workflows/_test.yml
call-gorelease:
+ needs: [call-lint, call-test]
uses: ./.github/workflows/_gorelease.yml
call-buildx:
needs: call-gorelease
diff --git a/.github/workflows/version_bump.yml b/.github/workflows/version_bump.yml
index 115f8cab5..725f2156a 100644
--- a/.github/workflows/version_bump.yml
+++ b/.github/workflows/version_bump.yml
@@ -6,6 +6,11 @@ on:
version:
description: "Version to bump to, example: v1.5.2"
required: true
+ ref:
+ description: "Ref to release from"
+ required: true
+ type: string
+ default: master
jobs:
tag-release:
@@ -18,7 +23,7 @@ jobs:
uses: actions/checkout@24cb9080177205b6e8c946b17badbe402adc938f # v3.4.0
with:
fetch-depth: 0
- ref: master
+ ref: ${{ inputs.ref }}
- name: Tag release
run: |
git config user.email "${{github.repository_owner}}@users.noreply.github.com"
diff --git a/Dockerfile b/Dockerfile
index 613dd4efc..09f640d95 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -16,4 +16,4 @@ WORKDIR /shiori
EXPOSE 8080
ENV SHIORI_DIR /shiori/
ENTRYPOINT ["/usr/bin/shiori"]
-CMD ["serve"]
+CMD ["server"]
diff --git a/Dockerfile.compose b/Dockerfile.compose
index 7d7da2ab5..f6d077cc2 100644
--- a/Dockerfile.compose
+++ b/Dockerfile.compose
@@ -3,4 +3,4 @@ FROM docker.io/golang:1.19-alpine3.16
WORKDIR /src/shiori
ENTRYPOINT ["go", "run", "main.go"]
-CMD ["serve"]
+CMD ["server"]
diff --git a/README.md b/README.md
index 02c0bb217..231e6579c 100644
--- a/README.md
+++ b/README.md
@@ -3,7 +3,7 @@
[![IC](https://github.com/go-shiori/shiori/actions/workflows/push.yml/badge.svg?branch=master)](https://github.com/go-shiori/shiori/actions/workflows/push.yml)
[![Go Report Card](https://goreportcard.com/badge/github.com/go-shiori/shiori)](https://goreportcard.com/report/github.com/go-shiori/shiori)
[![#shiori@libera.chat](https://img.shields.io/badge/irc-%23shiori-orange)](https://web.libera.chat/#shiori)
-[![#shiori-general:matrix.org](https://img.shields.io/badge/matrix-%23shiori-orange)](https://matrix.to/#/#shiori-general:matrix.org)
+[![#shiori-general:matrix.org](https://img.shields.io/badge/matrix-%23shiori-orange)](https://matrix.to/#/#shiori:matrix.org)
[![Containers](https://img.shields.io/static/v1?label=Container&message=Images&color=1488C6&logo=docker)](https://github.com/go-shiori/shiori/pkgs/container/shiori)
**Check out our latest [Announcements](https://github.com/go-shiori/shiori/discussions/categories/announcements)**
diff --git a/docs/Storage.md b/docs/Storage.md
new file mode 100644
index 000000000..5b1377daf
--- /dev/null
+++ b/docs/Storage.md
@@ -0,0 +1,14 @@
+# Storage
+
+Shiori requires a folder to store several pieces of data, such as the bookmark archives, thumbnails, ebooks, and others. If the database engine used is sqlite, then the database file will also be stored in this folder.
+
+You can specify the storage folder by using `--storage-dir` or `--portable` flags when running Shiori.
+
+If none specified, Shiori will try to find the correct app folder for your OS.
+
+For example:
+- In Windows, Shiori will use `%APPDATA%`.
+- In Linux, it will use `$XDG_CONFIG_HOME` or `$HOME/.local/share` if `$XDG_CONFIG_HOME` is not set.
+- In macOS, it will use `$HOME/Library/Application Support`.
+
+> For more and up to date information about app folder discovery check [muesli/go-app-paths](https://github.com/muesli/go-app-paths)
diff --git a/docs/index.md b/docs/index.md
index eb6fee64c..295d60024 100644
--- a/docs/index.md
+++ b/docs/index.md
@@ -4,11 +4,12 @@ Shiori is a simple bookmarks manager written in Go language. Intended as a simpl
## Resources
-- [API](./API.md)
+- [API](./API.md) (Deprecated)
- [APIv1](./APIv1.md) ([What is this?](https://github.com/go-shiori/shiori/issues/640))
- [Contributing](./Contribute.md)
- [Configuration](./Configuration.md)
- [FAQ](./Frequently-Asked-Question.md)
- [Installation](./Installation.md)
- [Screenshots](./screenshots/)
+- [Storage](./Storage.md)
- [Usage](./Usage.md)
diff --git a/internal/cmd/add.go b/internal/cmd/add.go
index 80ccab9e1..2c20fe34e 100644
--- a/internal/cmd/add.go
+++ b/internal/cmd/add.go
@@ -29,6 +29,8 @@ func addCmd() *cobra.Command {
}
func addHandler(cmd *cobra.Command, args []string) {
+ cfg, deps := initShiori(cmd.Context(), cmd)
+
// Read flag and arguments
url := args[0]
title, _ := cmd.Flags().GetString("title")
@@ -70,7 +72,7 @@ func addHandler(cmd *cobra.Command, args []string) {
}
// Save bookmark to database
- books, err := db.SaveBookmarks(cmd.Context(), true, book)
+ books, err := deps.Database.SaveBookmarks(cmd.Context(), true, book)
if err != nil {
cError.Printf("Failed to save bookmark: %v\n", err)
os.Exit(1)
@@ -90,7 +92,7 @@ func addHandler(cmd *cobra.Command, args []string) {
if err == nil && content != nil {
request := core.ProcessRequest{
- DataDir: dataDir,
+ DataDir: cfg.Storage.DataDir,
Bookmark: book,
Content: content,
ContentType: contentType,
@@ -112,7 +114,7 @@ func addHandler(cmd *cobra.Command, args []string) {
}
// Save bookmark to database
- _, err = db.SaveBookmarks(cmd.Context(), false, book)
+ _, err = deps.Database.SaveBookmarks(cmd.Context(), false, book)
if err != nil {
cError.Printf("Failed to save bookmark with content: %v\n", err)
os.Exit(1)
diff --git a/internal/cmd/check.go b/internal/cmd/check.go
index 8d0ae4a97..702dc632f 100644
--- a/internal/cmd/check.go
+++ b/internal/cmd/check.go
@@ -29,6 +29,8 @@ func checkCmd() *cobra.Command {
}
func checkHandler(cmd *cobra.Command, args []string) {
+ _, deps := initShiori(cmd.Context(), cmd)
+
// Parse flags
skipConfirm, _ := cmd.Flags().GetBool("yes")
@@ -53,7 +55,7 @@ func checkHandler(cmd *cobra.Command, args []string) {
// Fetch bookmarks from database
filterOptions := database.GetBookmarksOptions{IDs: ids}
- bookmarks, err := db.GetBookmarks(cmd.Context(), filterOptions)
+ bookmarks, err := deps.Database.GetBookmarks(cmd.Context(), filterOptions)
if err != nil {
cError.Printf("Failed to get bookmarks: %v\n", err)
os.Exit(1)
diff --git a/internal/cmd/delete.go b/internal/cmd/delete.go
index fce67a633..2ab032477 100644
--- a/internal/cmd/delete.go
+++ b/internal/cmd/delete.go
@@ -29,6 +29,8 @@ func deleteCmd() *cobra.Command {
}
func deleteHandler(cmd *cobra.Command, args []string) {
+ cfg, deps := initShiori(cmd.Context(), cmd)
+
// Parse flags
skipConfirm, _ := cmd.Flags().GetBool("yes")
@@ -52,7 +54,7 @@ func deleteHandler(cmd *cobra.Command, args []string) {
}
// Delete bookmarks from database
- err = db.DeleteBookmarks(cmd.Context(), ids...)
+ err = deps.Database.DeleteBookmarks(cmd.Context(), ids...)
if err != nil {
cError.Printf("Failed to delete bookmarks: %v\n", err)
os.Exit(1)
@@ -60,15 +62,15 @@ func deleteHandler(cmd *cobra.Command, args []string) {
// Delete thumbnail image and archives from local disk
if len(ids) == 0 {
- thumbDir := fp.Join(dataDir, "thumb")
- archiveDir := fp.Join(dataDir, "archive")
+ thumbDir := fp.Join(cfg.Storage.DataDir, "thumb")
+ archiveDir := fp.Join(cfg.Storage.DataDir, "archive")
os.RemoveAll(thumbDir)
os.RemoveAll(archiveDir)
} else {
for _, id := range ids {
strID := strconv.Itoa(id)
- imgPath := fp.Join(dataDir, "thumb", strID)
- archivePath := fp.Join(dataDir, "archive", strID)
+ imgPath := fp.Join(cfg.Storage.DataDir, "thumb", strID)
+ archivePath := fp.Join(cfg.Storage.DataDir, "archive", strID)
os.Remove(imgPath)
os.Remove(archivePath)
diff --git a/internal/cmd/export.go b/internal/cmd/export.go
index 0aec7be0e..67070a7b5 100644
--- a/internal/cmd/export.go
+++ b/internal/cmd/export.go
@@ -24,8 +24,10 @@ func exportCmd() *cobra.Command {
}
func exportHandler(cmd *cobra.Command, args []string) {
+ _, deps := initShiori(cmd.Context(), cmd)
+
// Fetch bookmarks from database
- bookmarks, err := db.GetBookmarks(cmd.Context(), database.GetBookmarksOptions{})
+ bookmarks, err := deps.Database.GetBookmarks(cmd.Context(), database.GetBookmarksOptions{})
if err != nil {
cError.Printf("Failed to get bookmarks: %v\n", err)
os.Exit(1)
diff --git a/internal/cmd/import.go b/internal/cmd/import.go
index a623b2b6f..48665fc04 100644
--- a/internal/cmd/import.go
+++ b/internal/cmd/import.go
@@ -29,6 +29,8 @@ func importCmd() *cobra.Command {
}
func importHandler(cmd *cobra.Command, args []string) {
+ _, deps := initShiori(cmd.Context(), cmd)
+
// Parse flags
generateTag := cmd.Flags().Changed("generate-tag")
@@ -104,7 +106,7 @@ func importHandler(cmd *cobra.Command, args []string) {
return
}
- _, exist, err := db.GetBookmark(cmd.Context(), 0, url)
+ _, exist, err := deps.Database.GetBookmark(cmd.Context(), 0, url)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
cError.Printf("Skip %s: Get Bookmark fail, %v", url, err)
return
@@ -145,7 +147,7 @@ func importHandler(cmd *cobra.Command, args []string) {
})
// Save bookmark to database
- bookmarks, err = db.SaveBookmarks(cmd.Context(), true, bookmarks...)
+ bookmarks, err = deps.Database.SaveBookmarks(cmd.Context(), true, bookmarks...)
if err != nil {
cError.Printf("Failed to save bookmarks: %v\n", err)
os.Exit(1)
diff --git a/internal/cmd/open.go b/internal/cmd/open.go
index 12ab56763..d7c595da7 100644
--- a/internal/cmd/open.go
+++ b/internal/cmd/open.go
@@ -36,6 +36,8 @@ func openCmd() *cobra.Command {
}
func openHandler(cmd *cobra.Command, args []string) {
+ cfg, deps := initShiori(cmd.Context(), cmd)
+
// Parse flags
skipConfirm, _ := cmd.Flags().GetBool("yes")
archiveMode, _ := cmd.Flags().GetBool("archive")
@@ -73,7 +75,7 @@ func openHandler(cmd *cobra.Command, args []string) {
WithContent: true,
}
- bookmarks, err := db.GetBookmarks(cmd.Context(), getOptions)
+ bookmarks, err := deps.Database.GetBookmarks(cmd.Context(), getOptions)
if err != nil {
cError.Printf("Failed to get bookmarks: %v\n", err)
os.Exit(1)
@@ -130,7 +132,7 @@ func openHandler(cmd *cobra.Command, args []string) {
// Open archive
id := strconv.Itoa(bookmarks[0].ID)
- archivePath := fp.Join(dataDir, "archive", id)
+ archivePath := fp.Join(cfg.Storage.DataDir, "archive", id)
archive, err := warc.Open(archivePath)
if err != nil {
diff --git a/internal/cmd/pocket.go b/internal/cmd/pocket.go
index 4471d2118..a16ad0e9b 100644
--- a/internal/cmd/pocket.go
+++ b/internal/cmd/pocket.go
@@ -25,6 +25,8 @@ func pocketCmd() *cobra.Command {
}
func pocketHandler(cmd *cobra.Command, args []string) {
+ _, deps := initShiori(cmd.Context(), cmd)
+
// Open pocket's file
srcFile, err := os.Open(args[0])
if err != nil {
@@ -70,7 +72,7 @@ func pocketHandler(cmd *cobra.Command, args []string) {
return
}
- _, exist, err := db.GetBookmark(cmd.Context(), 0, url)
+ _, exist, err := deps.Database.GetBookmark(cmd.Context(), 0, url)
if err != nil {
cError.Printf("Skip %s: Get Bookmark fail, %v", url, err)
return
@@ -103,7 +105,7 @@ func pocketHandler(cmd *cobra.Command, args []string) {
})
// Save bookmark to database
- bookmarks, err = db.SaveBookmarks(cmd.Context(), true, bookmarks...)
+ bookmarks, err = deps.Database.SaveBookmarks(cmd.Context(), true, bookmarks...)
if err != nil {
cError.Printf("Failed to save bookmarks: %v\n", err)
os.Exit(1)
diff --git a/internal/cmd/print.go b/internal/cmd/print.go
index bd802d580..c12db8b3e 100644
--- a/internal/cmd/print.go
+++ b/internal/cmd/print.go
@@ -32,6 +32,8 @@ func printCmd() *cobra.Command {
}
func printHandler(cmd *cobra.Command, args []string) {
+ _, deps := initShiori(cmd.Context(), cmd)
+
// Read flags
tags, _ := cmd.Flags().GetStringSlice("tags")
keyword, _ := cmd.Flags().GetString("search")
@@ -61,7 +63,7 @@ func printHandler(cmd *cobra.Command, args []string) {
OrderMethod: orderMethod,
}
- bookmarks, err := db.GetBookmarks(cmd.Context(), searchOptions)
+ bookmarks, err := deps.Database.GetBookmarks(cmd.Context(), searchOptions)
if err != nil {
cError.Printf("Failed to get bookmarks: %v\n", err)
return
diff --git a/internal/cmd/root-dev.go b/internal/cmd/root-dev.go
deleted file mode 100644
index 7d4d626ba..000000000
--- a/internal/cmd/root-dev.go
+++ /dev/null
@@ -1,7 +0,0 @@
-// +build dev
-
-package cmd
-
-func init() {
- developmentMode = true
-}
diff --git a/internal/cmd/root.go b/internal/cmd/root.go
index d59773401..a360450fc 100644
--- a/internal/cmd/root.go
+++ b/internal/cmd/root.go
@@ -6,35 +6,29 @@ import (
fp "path/filepath"
"time"
+ "github.com/go-shiori/shiori/internal/config"
"github.com/go-shiori/shiori/internal/database"
+ "github.com/go-shiori/shiori/internal/domains"
"github.com/go-shiori/shiori/internal/model"
- apppaths "github.com/muesli/go-app-paths"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/net/context"
)
-var (
- db database.DB
- dataDir string
- developmentMode bool
- logLevel string
- logCaller bool
-)
-
// ShioriCmd returns the root command for shiori
func ShioriCmd() *cobra.Command {
- logger := logrus.New()
-
rootCmd := &cobra.Command{
Use: "shiori",
Short: "Simple command-line bookmark manager built with Go",
}
- rootCmd.PersistentPreRun = preRunRootHandler
rootCmd.PersistentFlags().Bool("portable", false, "run shiori in portable mode")
- rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", logrus.InfoLevel.String(), "set logrus loglevel")
- rootCmd.PersistentFlags().BoolVar(&logCaller, "log-caller", false, "logrus report caller or not")
+ rootCmd.PersistentFlags().String("storage-directory", "", "path to store shiori data")
+ rootCmd.MarkFlagsMutuallyExclusive("portable", "storage-directory")
+
+ rootCmd.PersistentFlags().String("log-level", logrus.InfoLevel.String(), "set logrus loglevel")
+ rootCmd.PersistentFlags().Bool("log-caller", false, "logrus report caller or not")
+
rootCmd.AddCommand(
addCmd(),
printCmd(),
@@ -46,104 +40,105 @@ func ShioriCmd() *cobra.Command {
pocketCmd(),
serveCmd(),
checkCmd(),
- newVersionCommand(logger),
- newServerCommand(logger),
+ newVersionCommand(),
+ newServerCommand(),
)
return rootCmd
}
-func preRunRootHandler(cmd *cobra.Command, args []string) {
- // init logrus
- logrus.SetReportCaller(logCaller)
- logrus.SetFormatter(&logrus.TextFormatter{
+func initShiori(ctx context.Context, cmd *cobra.Command) (*config.Config, *config.Dependencies) {
+ logger := logrus.New()
+
+ portableMode, _ := cmd.Flags().GetBool("portable")
+ logLevel, _ := cmd.Flags().GetString("log-level")
+ logCaller, _ := cmd.Flags().GetBool("log-caller")
+ storageDirectory, _ := cmd.Flags().GetString("storage-directory")
+
+ logger.SetReportCaller(logCaller)
+ logger.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: time.RFC3339,
CallerPrettyfier: SFCallerPrettyfier,
})
if lvl, err := logrus.ParseLevel(logLevel); err != nil {
- cError.Printf("Failed to set log level: %v\n", err)
+ logger.WithError(err).Panic("failed to set log level")
} else {
- logrus.SetLevel(lvl)
+ logger.SetLevel(lvl)
}
- // Read flag
- var err error
- portableMode, _ := cmd.Flags().GetBool("portable")
+ cfg := config.ParseServerConfiguration(ctx, logger)
- // Get and create data dir
- dataDir, err = getDataDir(portableMode)
- if err != nil {
- cError.Printf("Failed to get data dir: %v\n", err)
- os.Exit(1)
+ if storageDirectory != "" && cfg.Storage.DataDir != "" {
+ logger.Warn("--storage-directory is set, overriding SHIORI_DIR.")
+ cfg.Storage.DataDir = storageDirectory
}
- err = os.MkdirAll(dataDir, model.DataDirPerm)
+ cfg.SetDefaults(logger, portableMode)
+
+ err := os.MkdirAll(cfg.Storage.DataDir, model.DataDirPerm)
if err != nil {
- cError.Printf("Failed to create data dir: %v\n", err)
- os.Exit(1)
+ logger.WithError(err).Fatal("error creating data directory")
}
- // Open database
- db, err = openDatabase(cmd.Context())
+ db, err := openDatabase(ctx, cfg.Database.DBMS, cfg.Database.URL)
if err != nil {
- cError.Printf("Failed to open database: %v\n", err)
- os.Exit(1)
+ logger.WithError(err).Fatal("error opening database")
}
// Migrate
if err := db.Migrate(); err != nil {
- cError.Printf("Error running migration: %s\n", err)
- os.Exit(1)
+ logger.WithError(err).Fatalf("Error running migration")
}
-}
-func getDataDir(portableMode bool) (string, error) {
- // If in portable mode, uses directory of executable
- if portableMode {
- exePath, err := os.Executable()
- if err != nil {
- return "", err
- }
-
- exeDir := fp.Dir(exePath)
- return fp.Join(exeDir, "shiori-data"), nil
+ if cfg.Development {
+ logger.Warn("Development mode is ENABLED, this will enable some helpers for local development, unsuitable for production environments")
}
- if developmentMode {
- return "dev-data", nil
- }
+ dependencies := config.NewDependencies(logger, db, cfg)
+ dependencies.Domains.Auth = domains.NewAccountsDomain(logger, cfg.Http.SecretKey, db)
+ dependencies.Domains.Archiver = domains.NewArchiverDomain(logger, cfg.Storage.DataDir)
- // Try to look at environment variables
- dataDir, found := os.LookupEnv("SHIORI_DIR")
- if found {
- return dataDir, nil
+ // Workaround: Get accounts to make sure at least one is present in the database.
+ // If there's no accounts in the database, create the shiori/gopher account the legacy api
+ // hardcoded in the login handler.
+ accounts, err := db.GetAccounts(cmd.Context(), database.GetAccountsOptions{})
+ if err != nil {
+ cError.Printf("Failed to get owner account: %v\n", err)
+ os.Exit(1)
}
- // Try to use platform specific app path
- userScope := apppaths.NewScope(apppaths.User, "shiori")
- dataDir, err := userScope.DataPath("")
- if err == nil {
- return dataDir, nil
+ if len(accounts) == 0 {
+ account := model.Account{
+ Username: "shiori",
+ Password: "gopher",
+ Owner: true,
+ }
+
+ if err := db.SaveAccount(cmd.Context(), account); err != nil {
+ logger.WithError(err).Fatal("error ensuring owner account")
+ }
}
- // When all fail, use current working directory
- return ".", nil
+ return cfg, dependencies
}
-func openDatabase(ctx context.Context) (database.DB, error) {
- switch dbms, _ := os.LookupEnv("SHIORI_DBMS"); dbms {
- case "mysql":
+func openDatabase(ctx context.Context, dbms, dbURL string) (database.DB, error) {
+ if dbURL != "" {
+ return database.Connect(ctx, dbURL)
+ }
+ if dbms == "mysql" {
return openMySQLDatabase(ctx)
- case "postgresql":
+ }
+ if dbms == "postgresql" {
return openPostgreSQLDatabase(ctx)
- default:
- return openSQLiteDatabase(ctx)
}
+ return openSQLiteDatabase(ctx)
}
func openSQLiteDatabase(ctx context.Context) (database.DB, error) {
+ dataDir := os.Getenv("SHIORI_DIR")
dbPath := fp.Join(dataDir, "shiori.db")
return database.OpenSQLiteDatabase(ctx, dbPath)
}
diff --git a/internal/cmd/serve.go b/internal/cmd/serve.go
index 347d83556..c711e6679 100644
--- a/internal/cmd/serve.go
+++ b/internal/cmd/serve.go
@@ -1,10 +1,6 @@
package cmd
import (
- "strings"
-
- "github.com/go-shiori/shiori/internal/webserver"
- "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
@@ -15,7 +11,8 @@ func serveCmd() *cobra.Command {
Long: "Run a simple and performant web server which " +
"serves the site for managing bookmarks. If --port " +
"flag is not used, it will use port 8080 by default.",
- Run: serveHandler,
+ Deprecated: "use server instead",
+ Run: newServerCommandHandler(),
}
cmd.Flags().IntP("port", "p", 8080, "Port used by the server")
@@ -25,39 +22,3 @@ func serveCmd() *cobra.Command {
return cmd
}
-
-func serveHandler(cmd *cobra.Command, args []string) {
- // Get flags value
- port, _ := cmd.Flags().GetInt("port")
- address, _ := cmd.Flags().GetString("address")
- rootPath, _ := cmd.Flags().GetString("webroot")
- log, _ := cmd.Flags().GetBool("log")
-
- // Validate root path
- if rootPath == "" {
- rootPath = "/"
- }
-
- if !strings.HasPrefix(rootPath, "/") {
- rootPath = "/" + rootPath
- }
-
- if !strings.HasSuffix(rootPath, "/") {
- rootPath += "/"
- }
-
- // Start server
- serverConfig := webserver.Config{
- DB: db,
- DataDir: dataDir,
- ServerAddress: address,
- ServerPort: port,
- RootPath: rootPath,
- Log: log,
- }
-
- err := webserver.ServeApp(serverConfig)
- if err != nil {
- logrus.Fatalf("Server error: %v\n", err)
- }
-}
diff --git a/internal/cmd/server.go b/internal/cmd/server.go
index 5395a5134..7637ee5ea 100644
--- a/internal/cmd/server.go
+++ b/internal/cmd/server.go
@@ -4,55 +4,51 @@ import (
"context"
"strings"
- "github.com/go-shiori/shiori/internal/config"
- "github.com/go-shiori/shiori/internal/domains"
"github.com/go-shiori/shiori/internal/http"
- "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
-func newServerCommand(logger *logrus.Logger) *cobra.Command {
+func newServerCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "server",
- Short: "Run the Shiori webserver [alpha]",
- Long: "Runs the new Shiori webserver with new API definitions. [alpha]",
- Run: newServerCommandHandler(logger),
+ Short: "Starts the Shiori webserver",
+ Long: "Serves the Shiori web interface and API.",
+ Run: newServerCommandHandler(),
}
cmd.Flags().IntP("port", "p", 8080, "Port used by the server")
cmd.Flags().StringP("address", "a", "", "Address the server listens to")
cmd.Flags().StringP("webroot", "r", "/", "Root path that used by server")
- cmd.Flags().Bool("log", true, "Print out a non-standard access log")
+ cmd.Flags().Bool("access-log", true, "Print out a non-standard access log")
+ cmd.Flags().Bool("serve-web-ui", true, "Serve static files from the webroot path")
+ cmd.Flags().String("secret-key", "", "Secret key used for encrypting session data")
return cmd
}
-func newServerCommandHandler(logger *logrus.Logger) func(cmd *cobra.Command, args []string) {
+func newServerCommandHandler() func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
- logger.Warn("This server is still in alpha, use it at your own risk. For more information check https://github.com/go-shiori/shiori/issues/640")
-
ctx := context.Background()
- database, err := openDatabase(ctx)
- if err != nil {
- logger.WithError(err).Fatal("error opening database")
- }
-
- cfg := config.ParseServerConfiguration(ctx, logger)
-
- if cfg.Development {
- logger.Warn("Development mode is ENABLED, this will enable some helpers for local development, unsuitable for production environments")
- }
-
- dependencies := config.NewDependencies(logger, database, cfg)
- dependencies.Domains.Auth = domains.NewAccountsDomain(logger, cfg.Http.SecretKey, database)
- dependencies.Domains.Archiver = domains.NewArchiverDomain(logger, cfg.Http.Storage.DataDir)
-
- // Get flags value
+ // Get flags values
port, _ := cmd.Flags().GetInt("port")
address, _ := cmd.Flags().GetString("address")
rootPath, _ := cmd.Flags().GetString("webroot")
- accessLog, _ := cmd.Flags().GetBool("log")
+ accessLog, _ := cmd.Flags().GetBool("access-log")
+ serveWebUI, _ := cmd.Flags().GetBool("serve-web-ui")
+ secretKey, _ := cmd.Flags().GetString("secret-key")
+
+ cfg, dependencies := initShiori(ctx, cmd)
+
+ // Check HTTP configuration
+ // For now it will just log to the console, but in the future it will be fatal. The only required
+ // setting for now is the secret key.
+ if errs, isValid := cfg.Http.IsValid(); !isValid {
+ dependencies.Log.Error("Found some errors in configuration.For now server will start but this will be fatal in the future.")
+ for _, err := range errs {
+ dependencies.Log.WithError(err).Error("found invalid configuration")
+ }
+ }
// Validate root path
if rootPath == "" {
@@ -72,13 +68,15 @@ func newServerCommandHandler(logger *logrus.Logger) func(cmd *cobra.Command, arg
cfg.Http.Address = address + ":"
cfg.Http.RootPath = rootPath
cfg.Http.AccessLog = accessLog
+ cfg.Http.ServeWebUI = serveWebUI
+ cfg.Http.SecretKey = secretKey
- server := http.NewHttpServer(logger, cfg.Http, dependencies).Setup(cfg.Http, dependencies)
+ server := http.NewHttpServer(dependencies.Log).Setup(cfg, dependencies)
if err := server.Start(ctx); err != nil {
- logger.WithError(err).Fatal("error starting server")
+ dependencies.Log.WithError(err).Fatal("error starting server")
}
- logger.WithField("addr", address).Debug("started http server")
+ dependencies.Log.WithField("addr", address).Debug("started http server")
server.WaitStop(ctx)
}
diff --git a/internal/cmd/update.go b/internal/cmd/update.go
index 9690bee49..67cf7ab4a 100644
--- a/internal/cmd/update.go
+++ b/internal/cmd/update.go
@@ -41,6 +41,8 @@ func updateCmd() *cobra.Command {
}
func updateHandler(cmd *cobra.Command, args []string) {
+ cfg, deps := initShiori(cmd.Context(), cmd)
+
// Parse flags
url, _ := cmd.Flags().GetString("url")
title, _ := cmd.Flags().GetString("title")
@@ -94,7 +96,7 @@ func updateHandler(cmd *cobra.Command, args []string) {
IDs: ids,
}
- bookmarks, err := db.GetBookmarks(cmd.Context(), filterOptions)
+ bookmarks, err := deps.Database.GetBookmarks(cmd.Context(), filterOptions)
if err != nil {
cError.Printf("Failed to get bookmarks: %v\n", err)
os.Exit(1)
@@ -164,7 +166,7 @@ func updateHandler(cmd *cobra.Command, args []string) {
}
request := core.ProcessRequest{
- DataDir: dataDir,
+ DataDir: cfg.Storage.DataDir,
Bookmark: book,
Content: content,
ContentType: contentType,
@@ -285,7 +287,7 @@ func updateHandler(cmd *cobra.Command, args []string) {
}
// Save bookmarks to database
- bookmarks, err = db.SaveBookmarks(cmd.Context(), false, bookmarks...)
+ bookmarks, err = deps.Database.SaveBookmarks(cmd.Context(), false, bookmarks...)
if err != nil {
cError.Printf("Failed to save bookmark: %v\n", err)
os.Exit(1)
diff --git a/internal/cmd/version.go b/internal/cmd/version.go
index da422bca5..5de356b0f 100644
--- a/internal/cmd/version.go
+++ b/internal/cmd/version.go
@@ -2,21 +2,20 @@ package cmd
import (
"github.com/go-shiori/shiori/internal/model"
- "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
-func newVersionCommand(logger *logrus.Logger) *cobra.Command {
+func newVersionCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Output the shiori version",
- Run: newVersionCommandHandler(logger),
+ Run: newVersionCommandHandler(),
}
return cmd
}
-func newVersionCommandHandler(logger *logrus.Logger) func(cmd *cobra.Command, args []string) {
+func newVersionCommandHandler() func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
cmd.Printf("Shiori version %s (build %s) at %s\n", model.Version, model.Commit, model.Date)
}
diff --git a/internal/config/config.go b/internal/config/config.go
index 984df3f53..336ba5b76 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -3,7 +3,9 @@ package config
import (
"bufio"
"context"
+ "fmt"
"os"
+ "path/filepath"
"strings"
"time"
@@ -41,12 +43,13 @@ func readDotEnv(logger *logrus.Logger) map[string]string {
}
type HttpConfig struct {
- Enabled bool `env:"HTTP_ENABLED,default=True"`
- Port int `env:"HTTP_PORT,default=8080"`
- Address string `env:"HTTP_ADDRESS,default=:"`
- RootPath string `env:"HTTP_ROOT_PATH,default=/"`
- AccessLog bool `env:"HTTP_ACCESS_LOG,default=True"`
- SecretKey string `env:"HTTP_SECRET_KEY"`
+ Enabled bool `env:"HTTP_ENABLED,default=True"`
+ Port int `env:"HTTP_PORT,default=8080"`
+ Address string `env:"HTTP_ADDRESS,default=:"`
+ RootPath string `env:"HTTP_ROOT_PATH,default=/"`
+ AccessLog bool `env:"HTTP_ACCESS_LOG,default=True"`
+ ServeWebUI bool `env:"HTTP_SERVE_WEB_UI,default=True"`
+ SecretKey string `env:"HTTP_SECRET_KEY"`
// Fiber Specific
BodyLimit int `env:"HTTP_BODY_LIMIT,default=1024"`
ReadTimeout time.Duration `env:"HTTP_READ_TIMEOUT,default=10s"`
@@ -54,31 +57,52 @@ type HttpConfig struct {
IDLETimeout time.Duration `env:"HTTP_IDLE_TIMEOUT,default=10s"`
DisableKeepAlive bool `env:"HTTP_DISABLE_KEEP_ALIVE,default=true"`
DisablePreParseMultipartForm bool `env:"HTTP_DISABLE_PARSE_MULTIPART_FORM,default=true"`
- Routes struct {
- Bookmark struct {
- Path string `env:"ROUTES_BOOKMARK_PATH,default=/bookmark"`
- }
- Frontend struct {
- Path string `env:"ROUTES_STATIC_PATH,default=/"`
- MaxAge time.Duration `env:"ROUTES_STATIC_MAX_AGE,default=720h"`
- }
- System struct {
- Path string `env:"ROUTES_SYSTEM_PATH,default=/system"`
- }
- API struct {
- Path string `env:"ROUTE_API_PATH,default=/api/v1"`
- }
- }
- Storage struct {
- DataDir string `env:"DATA_DIR"`
- }
+}
+
+type DatabaseConfig struct {
+ DBMS string `env:"DBMS"` // Deprecated
+ // DBMS requires more environment variables. Check the database package for more information.
+ URL string `env:"DATABASE_URL"`
+}
+
+type StorageConfig struct {
+ DataDir string `env:"DIR"` // Using DIR to be backwards compatible with the old config
}
type Config struct {
Hostname string `env:"HOSTNAME,required"`
- Development bool `env:"DEVELOPMENT,default=false"`
+ Development bool `env:"DEVELOPMENT,default=False"`
+ Database *DatabaseConfig
+ Storage *StorageConfig
// LogLevel string `env:"LOG_LEVEL,default=info"`
- Http HttpConfig
+ Http *HttpConfig
+}
+
+// IsValid checks if the configuration is valid
+func (c HttpConfig) IsValid() (errs []error, isValid bool) {
+ if c.SecretKey == "" {
+ errs = append(errs, fmt.Errorf("SHIORI_HTTP_SECRET_KEY is required"))
+ }
+
+ return errs, len(errs) == 0
+}
+
+// SetDefaults sets the default values for the configuration
+func (c Config) SetDefaults(logger *logrus.Logger, portableMode bool) {
+ // Set the default storage directory if not set, setting also the database url for
+ // sqlite3 if that engine is used
+ if c.Storage.DataDir == "" {
+ var err error
+ c.Storage.DataDir, err = getStorageDirectory(portableMode)
+ if err != nil {
+ logger.WithError(err).Fatal("couldn't determine the data directory")
+ }
+ }
+
+ // Set default database url if not set
+ if c.Database.DBMS == "" && c.Database.URL == "" {
+ c.Database.URL = fmt.Sprintf("sqlite:///%s", filepath.Join(c.Storage.DataDir, "shiori.db"))
+ }
}
func ParseServerConfiguration(ctx context.Context, logger *logrus.Logger) *Config {
diff --git a/internal/config/storage.go b/internal/config/storage.go
new file mode 100644
index 000000000..de2c4dbea
--- /dev/null
+++ b/internal/config/storage.go
@@ -0,0 +1,31 @@
+package config
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+
+ gap "github.com/muesli/go-app-paths"
+)
+
+func getStorageDirectory(portableMode bool) (string, error) {
+ // If in portable mode, uses directory of executable
+ if portableMode {
+ exePath, err := os.Executable()
+ if err != nil {
+ return "", err
+ }
+
+ exeDir := filepath.Dir(exePath)
+ return filepath.Join(exeDir, "shiori-data"), nil
+ }
+
+ // Try to use platform specific app path
+ userScope := gap.NewScope(gap.User, "shiori")
+ dataDir, err := userScope.DataPath("")
+ if err == nil {
+ return dataDir, nil
+ }
+
+ return "", fmt.Errorf("couldn't determine the data directory")
+}
diff --git a/internal/database/database.go b/internal/database/database.go
index 45de936d0..24375946b 100644
--- a/internal/database/database.go
+++ b/internal/database/database.go
@@ -3,7 +3,9 @@ package database
import (
"context"
"embed"
+ "fmt"
"log"
+ "net/url"
"github.com/go-shiori/shiori/internal/model"
"github.com/jmoiron/sqlx"
@@ -43,6 +45,25 @@ type GetAccountsOptions struct {
Owner bool
}
+// Connect connects to database based on submitted database URL.
+func Connect(ctx context.Context, dbURL string) (DB, error) {
+ dbU, err := url.Parse(dbURL)
+ if err != nil {
+ return nil, errors.Wrap(err, "failed to parse database URL")
+ }
+
+ switch dbU.Scheme {
+ case "mysql":
+ return OpenMySQLDatabase(ctx, dbURL)
+ case "postgres":
+ return OpenPGDatabase(ctx, dbURL)
+ case "sqlite":
+ return OpenSQLiteDatabase(ctx, dbU.Path[1:])
+ }
+
+ return nil, fmt.Errorf("unsupported database scheme: %s", dbU.Scheme)
+}
+
// DB is interface for accessing and manipulating data in database.
type DB interface {
// Migrate runs migrations for this database
diff --git a/internal/database/sqlite.go b/internal/database/sqlite.go
index 4605cdbca..2576f2da5 100644
--- a/internal/database/sqlite.go
+++ b/internal/database/sqlite.go
@@ -42,7 +42,7 @@ func OpenSQLiteDatabase(ctx context.Context, databasePath string) (sqliteDB *SQL
}
sqliteDB = &SQLiteDatabase{dbbase: dbbase{*db}}
- return sqliteDB, err
+ return sqliteDB, nil
}
// Migrate runs migrations for this database engine
diff --git a/internal/domains/accounts.go b/internal/domains/accounts.go
index 1a2559ee7..132fa1b27 100644
--- a/internal/domains/accounts.go
+++ b/internal/domains/accounts.go
@@ -66,7 +66,7 @@ func (d *AccountsDomain) GetAccountFromCredentials(ctx context.Context, username
func (d *AccountsDomain) CreateTokenForAccount(account *model.Account, expiration time.Time) (string, error) {
claims := jwt.MapClaims{
- "account": account,
+ "account": account.ToDTO(),
"exp": expiration.UTC().Unix(),
}
diff --git a/internal/http/frontend/content.html b/internal/http/frontend/content.html
deleted file mode 100644
index 4bdee7983..000000000
--- a/internal/http/frontend/content.html
+++ /dev/null
@@ -1,90 +0,0 @@
-
-
-
-
-
- $$.Book.Title$$ - Shiori - Bookmarks Manager
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- $$html .Book.HTML$$
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/internal/http/frontend/css/archive.css b/internal/http/frontend/css/archive.css
deleted file mode 100644
index 3529559fe..000000000
--- a/internal/http/frontend/css/archive.css
+++ /dev/null
@@ -1 +0,0 @@
-:root{--main:#F44336;--border:#E5E5E5;--colorLink:#999;--archiveHeaderBg:rgba(255,255,255,0.95)}@media (prefers-color-scheme:dark){:root{--border:#191919;--archiveHeaderBg:rgba(41,41,41,0.95)}}#shiori-archive-header{top:0;left:0;right:0;height:60px;position:fixed;padding:0 16px;display:-webkit-box;display:flex;-webkit-box-orient:horizontal;-webkit-box-direction:normal;flex-flow:row wrap;-webkit-box-align:center;align-items:center;font-size:16px;border-bottom:1px solid var(--border);background-color:var(--archiveHeaderBg);z-index:9999999999}#shiori-archive-header *{border-width:0;box-sizing:border-box;font-family:"Source Sans Pro",sans-serif;margin:0;padding:0}#shiori-archive-header>*:not(:last-child){margin-right:8px}#shiori-archive-header>.spacer{-webkit-box-flex:1;flex:1}#shiori-archive-header #shiori-logo{font-size:2em;font-weight:100;color:var(--main)}#shiori-archive-header #shiori-logo span{margin-right:8px}#shiori-archive-header a{display:block;color:var(--colorLink);text-decoration:underline}#shiori-archive-header a:hover,#shiori-archive-header a:focus{color:var(--main)}@media (max-width:600px){#shiori-archive-header{font-size:14px;height:50px}#shiori-archive-header #shiori-logo{font-size:1.5em}}
\ No newline at end of file
diff --git a/internal/http/frontend/embed.go b/internal/http/frontend/embed.go
deleted file mode 100644
index 2d127e148..000000000
--- a/internal/http/frontend/embed.go
+++ /dev/null
@@ -1,6 +0,0 @@
-package frontend
-
-import "embed"
-
-//go:embed *
-var Assets embed.FS
diff --git a/internal/http/frontend/index.html b/internal/http/frontend/index.html
deleted file mode 100644
index d3f35d40a..000000000
--- a/internal/http/frontend/index.html
+++ /dev/null
@@ -1,195 +0,0 @@
-
-
-
-
-
- Shiori - Bookmarks Manager
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/internal/http/frontend/js/component/bookmark.js b/internal/http/frontend/js/component/bookmark.js
deleted file mode 100644
index aa016a976..000000000
--- a/internal/http/frontend/js/component/bookmark.js
+++ /dev/null
@@ -1,117 +0,0 @@
-var template = `
-`;
-
-export default {
- template: template,
- props: {
- id: Number,
- url: String,
- title: String,
- excerpt: String,
- public: Number,
- imageURL: String,
- hasContent: Boolean,
- hasArchive: Boolean,
- index: Number,
- showId: Boolean,
- editMode: Boolean,
- listMode: Boolean,
- hideThumbnail: Boolean,
- hideExcerpt: Boolean,
- selected: Boolean,
- menuVisible: Boolean,
- tags: {
- type: Array,
- default() {
- return []
- }
- }
- },
- computed: {
- mainURL() {
- if (this.hasContent) {
- return new URL(`bookmark/${this.id}/content`, document.baseURI);
- } else if (this.hasArchive) {
- return new URL(`bookmark/${this.id}/archive`, document.baseURI);
- } else {
- return this.url;
- }
- },
- hostnameURL() {
- var url = new URL(this.url);
- return url.hostname.replace(/^www\./, "");
- },
- thumbnailVisible() {
- return this.imageURL !== "" &&
- !this.hideThumbnail;
- },
- excerptVisible() {
- return this.excerpt !== "" &&
- !this.thumbnailVisible &&
- !this.hideExcerpt;
- },
- thumbnailStyleURL() {
- return {
- backgroundImage: `url("${this.imageURL}")`
- }
- },
- eventItem() {
- return {
- id: this.id,
- index: this.index,
- }
- }
- },
- methods: {
- tagClicked(name, event) {
- this.$emit("tag-clicked", name, event);
- },
- selectBookmark() {
- this.$emit("select", this.eventItem);
- },
- editBookmark() {
- this.$emit("edit", this.eventItem);
- },
- deleteBookmark() {
- this.$emit("delete", this.eventItem);
- },
- updateBookmark() {
- this.$emit("update", this.eventItem);
- }
- }
-}
diff --git a/internal/http/frontend/js/page/base.js b/internal/http/frontend/js/page/base.js
deleted file mode 100644
index 3d357a784..000000000
--- a/internal/http/frontend/js/page/base.js
+++ /dev/null
@@ -1,114 +0,0 @@
-export default {
- props: {
- activeAccount: {
- type: Object,
- default() {
- return {
- id: 0,
- username: "",
- owner: false,
- }
- }
- },
- appOptions: {
- type: Object,
- default() {
- return {
- showId: false,
- listMode: false,
- nightMode: false,
- hideThumbnail: false,
- hideExcerpt: false,
-
- keepMetadata: false,
- useArchive: false,
- makePublic: false,
- };
- }
- }
- },
- data() {
- return {
- dialog: {}
- }
- },
- methods: {
- defaultDialog() {
- return {
- visible: false,
- loading: false,
- title: '',
- content: '',
- fields: [],
- showLabel: false,
- mainText: 'Yes',
- secondText: '',
- mainClick: () => {
- this.dialog.visible = false;
- },
- secondClick: () => {
- this.dialog.visible = false;
- },
- escPressed: () => {
- if (!this.loading) this.dialog.visible = false;
- }
- }
- },
- showDialog(cfg) {
- var base = this.defaultDialog();
- base.visible = true;
- if (cfg.loading) base.loading = cfg.loading;
- if (cfg.title) base.title = cfg.title;
- if (cfg.content) base.content = cfg.content;
- if (cfg.fields) base.fields = cfg.fields;
- if (cfg.showLabel) base.showLabel = cfg.showLabel;
- if (cfg.mainText) base.mainText = cfg.mainText;
- if (cfg.secondText) base.secondText = cfg.secondText;
- if (cfg.mainClick) base.mainClick = cfg.mainClick;
- if (cfg.secondClick) base.secondClick = cfg.secondClick;
- if (cfg.escPressed) base.escPressed = cfg.escPressed;
- this.dialog = base;
- },
- async getErrorMessage(err) {
- switch (err.constructor) {
- case Error:
- return err.message;
- case Response:
- var text = await err.text();
- return `${text} (${err.status})`;
- default:
- return err;
- }
- },
- isSessionError(err) {
- switch (err.toString().replace(/\(\d+\)/g, "").trim().toLowerCase()) {
- case "session is not exist":
- case "session has been expired":
- return true
- default:
- return false;
- }
- },
- showErrorDialog(msg) {
- var sessionError = this.isSessionError(msg),
- dialogContent = sessionError ? "Session has expired, please login again." : msg;
-
- this.showDialog({
- visible: true,
- title: 'Error',
- content: dialogContent,
- mainText: 'OK',
- mainClick: () => {
- this.dialog.visible = false;
- if (sessionError) {
- var loginUrl = new Url("login", document.baseURI);
- loginUrl.query.dst = window.location.href;
-
- document.cookie = `session-id=; Path=${new URL(document.baseURI).pathname}; Expires=Thu, 01 Jan 1970 00:00:00 GMT;`;
- location.href = loginUrl.toString();
- }
- }
- });
- },
- }
-}
\ No newline at end of file
diff --git a/internal/http/frontend/js/page/home.js b/internal/http/frontend/js/page/home.js
deleted file mode 100644
index 33a0faf22..000000000
--- a/internal/http/frontend/js/page/home.js
+++ /dev/null
@@ -1,836 +0,0 @@
-var template = `
-`
-
-import paginationBox from "../component/pagination.js";
-import bookmarkItem from "../component/bookmark.js";
-import customDialog from "../component/dialog.js";
-import basePage from "./base.js";
-
-export default {
- template: template,
- mixins: [basePage],
- components: {
- bookmarkItem,
- paginationBox,
- customDialog
- },
- data() {
- return {
- loading: false,
- editMode: false,
- selection: [],
-
- search: "",
- page: 0,
- maxPage: 0,
- bookmarks: [],
- tags: [],
-
- dialogTags: {
- visible: false,
- editMode: false,
- title: 'Existing Tags',
- mainText: 'OK',
- secondText: 'Rename Tags',
- mainClick: () => {
- if (this.dialogTags.editMode) {
- this.dialogTags.editMode = false;
- } else {
- this.dialogTags.visible = false;
- }
- },
- secondClick: () => {
- this.dialogTags.editMode = true;
- },
- escPressed: () => {
- this.dialogTags.visible = false;
- this.dialogTags.editMode = false;
- }
- },
- }
- },
- computed: {
- listIsEmpty() {
- return this.bookmarks.length <= 0;
- }
- },
- watch: {
- "dialogTags.editMode"(editMode) {
- if (editMode) {
- this.dialogTags.title = "Rename Tags";
- this.dialogTags.mainText = "Cancel";
- this.dialogTags.secondText = "";
- } else {
- this.dialogTags.title = "Existing Tags";
- this.dialogTags.mainText = "OK";
- this.dialogTags.secondText = "Rename Tags";
- }
- }
- },
- methods: {
- reloadData() {
- if (this.loading) return;
- this.page = 1;
- this.search = "";
- this.loadData(true, true);
- },
- loadData(saveState, fetchTags) {
- if (this.loading) return;
-
- // Set default args
- saveState = (typeof saveState === "boolean") ? saveState : true;
- fetchTags = (typeof fetchTags === "boolean") ? fetchTags : false;
-
- // Parse search query
- var keyword = this.search,
- rxExcludeTagA = /(^|\s)-tag:["']([^"']+)["']/i, // -tag:"with space"
- rxExcludeTagB = /(^|\s)-tag:(\S+)/i, // -tag:without-space
- rxIncludeTagA = /(^|\s)tag:["']([^"']+)["']/i, // tag:"with space"
- rxIncludeTagB = /(^|\s)tag:(\S+)/i, // tag:without-space
- tags = [],
- excludedTags = [],
- rxResult;
-
- // Get excluded tag first, while also removing it from keyword
- while (rxResult = rxExcludeTagA.exec(keyword)) {
- keyword = keyword.replace(rxResult[0], "");
- excludedTags.push(rxResult[2]);
- }
-
- while (rxResult = rxExcludeTagB.exec(keyword)) {
- keyword = keyword.replace(rxResult[0], "");
- excludedTags.push(rxResult[2]);
- }
-
- // Get included tags
- while (rxResult = rxIncludeTagA.exec(keyword)) {
- keyword = keyword.replace(rxResult[0], "");
- tags.push(rxResult[2]);
- }
-
- while (rxResult = rxIncludeTagB.exec(keyword)) {
- keyword = keyword.replace(rxResult[0], "");
- tags.push(rxResult[2]);
- }
-
- // Trim keyword
- keyword = keyword.trim().replace(/\s+/g, " ");
-
- // Prepare URL for API
- var url = new URL("api/v1/bookmarks", document.baseURI);
- url.search = new URLSearchParams({
- keyword: keyword,
- tags: tags.join(","),
- exclude: excludedTags.join(","),
- page: this.page
- });
-
- // Fetch data from API
- var skipFetchTags = Error("skip fetching tags");
-
- this.loading = true;
- fetch(url, {headers: {'Content-Type': 'application/json'}})
- .then(response => {
- if (!response.ok) throw response;
- return response.json();
- })
- .then(json => {
- // Set data
- this.page = json.page;
- this.maxPage = json.maxPage;
- this.bookmarks = json.message;
-
- // Save state and change URL if needed
- if (saveState) {
- var history = {
- activePage: "page-home",
- search: this.search,
- page: this.page
- };
-
- var url = new Url(document.baseURI);
- url.hash = "home";
- url.clearQuery();
- if (this.page > 1) url.query.page = this.page;
- if (this.search !== "") url.query.search = this.search;
-
- window.history.pushState(history, "page-home", url);
- }
-
- // Fetch tags if requested
- if (fetchTags) {
- return fetch(new URL("api/v1/tags", document.baseURI), {headers: {'Content-Type': 'application/json'}});
- } else {
- this.loading = false;
- throw skipFetchTags;
- }
- })
- .then(response => {
- if (!response.ok) throw response;
- return response.json();
- })
- .then(json => {
- this.tags = json.message;
- this.loading = false;
- })
- .catch(err => {
- this.loading = false;
-
- if (err !== skipFetchTags) {
- this.getErrorMessage(err).then(msg => {
- this.showErrorDialog(msg);
- })
- }
- });
- },
- searchBookmarks() {
- this.page = 1;
- this.loadData();
- },
- changePage(page) {
- this.page = page;
- this.$refs.bookmarksGrid.scrollTop = 0;
- this.loadData();
- },
- toggleEditMode() {
- this.selection = [];
- this.editMode = !this.editMode;
- },
- toggleSelection(item) {
- var idx = this.selection.findIndex(el => el.id === item.id);
- if (idx === -1) this.selection.push(item);
- else this.selection.splice(idx, 1);
- },
- isSelected(bookId) {
- return this.selection.findIndex(el => el.id === bookId) > -1;
- },
- dialogTagClicked(event, tag) {
- if (!this.dialogTags.editMode) {
- this.filterTag(tag.name, event.altKey);
- } else {
- this.dialogTags.visible = false;
- this.showDialogRenameTag(tag);
- }
- },
- bookmarkTagClicked(event, tagName) {
- this.filterTag(tagName, event.altKey);
- },
- filterTag(tagName, excludeMode) {
- // Set default parameter
- excludeMode = (typeof excludeMode === "boolean") ? excludeMode : false;
-
- if (this.dialogTags.editMode) {
- return;
- }
-
- if (tagName === "*") {
- this.search = excludeMode ? "-tag:*" : "tag:*";
- this.page = 1;
- this.loadData();
- return;
- }
-
- var rxSpace = /\s+/g,
- includeTag = rxSpace.test(tagName) ? `tag:"${tagName}"` : `tag:${tagName}`,
- excludeTag = "-" + includeTag,
- rxIncludeTag = new RegExp(`(^|\\s)${includeTag}`, "ig"),
- rxExcludeTag = new RegExp(`(^|\\s)${excludeTag}`, "ig"),
- search = this.search;
-
- search = search.replace("-tag:*", "");
- search = search.replace("tag:*", "");
- search = search.trim();
-
- if (excludeMode) {
- if (rxExcludeTag.test(search)) {
- return;
- }
-
- if (rxIncludeTag.test(search)) {
- this.search = search.replace(rxIncludeTag, "$1" + excludeTag);
- } else {
- search += ` ${excludeTag}`;
- this.search = search.trim();
- }
- } else {
- if (rxIncludeTag.test(search)) {
- return;
- }
-
- if (rxExcludeTag.test(search)) {
- this.search = search.replace(rxExcludeTag, "$1" + includeTag);
- } else {
- search += ` ${includeTag}`;
- this.search = search.trim();
- }
- }
-
- this.page = 1;
- this.loadData();
- },
- showDialogAdd() {
- this.showDialog({
- title: "New Bookmark",
- content: "Create a new bookmark",
- fields: [{
- name: "url",
- label: "Url, start with http://...",
- }, {
- name: "title",
- label: "Custom title (optional)"
- }, {
- name: "excerpt",
- label: "Custom excerpt (optional)",
- type: "area"
- }, {
- name: "tags",
- label: "Comma separated tags (optional)",
- separator: ",",
- dictionary: this.tags.map(tag => tag.name)
- }, {
- name: "createArchive",
- label: "Create archive",
- type: "check",
- value: this.appOptions.useArchive,
- }, {
- name: "makePublic",
- label: "Make archive publicly available",
- type: "check",
- value: this.appOptions.makePublic,
- }],
- mainText: "OK",
- secondText: "Cancel",
- mainClick: (data) => {
- // Make sure URL is not empty
- if (data.url.trim() === "") {
- this.showErrorDialog("URL must not empty");
- return;
- }
-
- // Prepare tags
- var tags = data.tags
- .toLowerCase()
- .replace(/\s+/g, " ")
- .split(/\s*,\s*/g)
- .filter(tag => tag.trim() !== "")
- .map(tag => {
- return {
- name: tag.trim()
- };
- });
-
- // Send data
- var data = {
- url: data.url.trim(),
- title: data.title.trim(),
- excerpt: data.excerpt.trim(),
- public: data.makePublic ? 1 : 0,
- tags: tags,
- createArchive: data.createArchive,
- };
-
- this.dialog.loading = true;
- fetch(new URL("api/v1/bookmarks", document.baseURI), {
- method: "post",
- body: JSON.stringify(data),
- headers: { "Content-Type": "application/json" }
- }).then(response => {
- if (!response.ok) throw response;
- return response.json();
- }).then(json => {
- this.dialog.loading = false;
- this.dialog.visible = false;
- this.bookmarks.splice(0, 0, json);
- }).catch(err => {
- this.dialog.loading = false;
- this.getErrorMessage(err).then(msg => {
- this.showErrorDialog(msg);
- })
- });
- }
- });
- },
- showDialogEdit(item) {
- // Check the item
- if (typeof item !== "object") return;
-
- var id = (typeof item.id === "number") ? item.id : 0,
- index = (typeof item.index === "number") ? item.index : -1;
-
- if (id < 1 || index < 0) return;
-
- // Get the existing bookmark value
- var book = JSON.parse(JSON.stringify(this.bookmarks[index])),
- strTags = book.tags.map(tag => tag.name).join(", ");
-
- this.showDialog({
- title: "Edit Bookmark",
- content: "Edit the bookmark's data",
- showLabel: true,
- fields: [{
- name: "url",
- label: "Url",
- value: book.url,
- }, {
- name: "title",
- label: "Title",
- value: book.title,
- }, {
- name: "excerpt",
- label: "Excerpt",
- type: "area",
- value: book.excerpt,
- }, {
- name: "tags",
- label: "Tags",
- value: strTags,
- separator: ",",
- dictionary: this.tags.map(tag => tag.name)
- }, {
- name: "makePublic",
- label: "Make archive publicly available",
- type: "check",
- value: book.public >= 1,
- }],
- mainText: "OK",
- secondText: "Cancel",
- mainClick: (data) => {
- // Validate input
- if (data.title.trim() === "") return;
-
- // Prepare tags
- var tags = data.tags
- .toLowerCase()
- .replace(/\s+/g, " ")
- .split(/\s*,\s*/g)
- .filter(tag => tag.trim() !== "")
- .map(tag => {
- return {
- name: tag.trim()
- };
- });
-
- // Set new data
- book.url = data.url.trim();
- book.title = data.title.trim();
- book.excerpt = data.excerpt.trim();
- book.public = data.makePublic ? 1 : 0;
- book.tags = tags;
-
- // Send data
- this.dialog.loading = true;
- fetch(new URL("api/v1/bookmarks", document.baseURI), {
- method: "put",
- body: JSON.stringify(book),
- headers: { "Content-Type": "application/json" }
- }).then(response => {
- if (!response.ok) throw response;
- return response.json();
- }).then(json => {
- this.dialog.loading = false;
- this.dialog.visible = false;
- this.bookmarks.splice(index, 1, json);
- }).catch(err => {
- this.dialog.loading = false;
- this.getErrorMessage(err).then(msg => {
- this.showErrorDialog(msg);
- })
- });
- }
- });
- },
- showDialogDelete(items) {
- // Check and filter items
- if (typeof items !== "object") return;
- if (!Array.isArray(items)) items = [items];
-
- items = items.filter(item => {
- var id = (typeof item.id === "number") ? item.id : 0,
- index = (typeof item.index === "number") ? item.index : -1;
-
- return id > 0 && index > -1;
- });
-
- if (items.length === 0) return;
-
- // Split ids and indices
- var ids = items.map(item => item.id),
- indices = items.map(item => item.index).sort((a, b) => b - a);
-
- // Create title and content
- var title = "Delete Bookmarks",
- content = "Delete the selected bookmarks ? This action is irreversible.";
-
- if (items.length === 1) {
- title = "Delete Bookmark";
- content = "Are you sure ? This action is irreversible.";
- }
-
- // Show dialog
- this.showDialog({
- title: title,
- content: content,
- mainText: "Yes",
- secondText: "No",
- mainClick: () => {
- this.dialog.loading = true;
- fetch(new URL("api/v1/bookmarks/" + ids, document.baseURI), {
- method: "delete",
- headers: { "Content-Type": "application/json" },
- }).then(response => {
- if (!response.ok) throw response;
- return response;
- }).then(() => {
- this.selection = [];
- this.editMode = false;
- this.dialog.loading = false;
- this.dialog.visible = false;
- indices.forEach(index => this.bookmarks.splice(index, 1))
-
- if (this.bookmarks.length < 20) {
- this.loadData(false);
- }
- }).catch(err => {
- this.selection = [];
- this.editMode = false;
- this.dialog.loading = false;
-
- this.getErrorMessage(err).then(msg => {
- this.showErrorDialog(msg);
- })
- });
- }
- });
- },
- showDialogUpdateCache(items) {
- // Check and filter items
- if (typeof items !== "object") return;
- if (!Array.isArray(items)) items = [items];
-
- items = items.filter(item => {
- var id = (typeof item.id === "number") ? item.id : 0,
- index = (typeof item.index === "number") ? item.index : -1;
-
- return id > 0 && index > -1;
- });
-
- if (items.length === 0) return;
-
- // Show dialog
- var ids = items.map(item => item.id);
-
- this.showDialog({
- title: "Update Cache",
- content: "Update cache for selected bookmarks ? This action is irreversible.",
- fields: [{
- name: "keepMetadata",
- label: "Keep the old title and excerpt",
- type: "check",
- value: this.appOptions.keepMetadata,
- }, {
- name: "createArchive",
- label: "Update archive as well",
- type: "check",
- value: this.appOptions.useArchive,
- }],
- mainText: "Yes",
- secondText: "No",
- mainClick: (data) => {
- var data = {
- ids: ids,
- createArchive: data.createArchive,
- keepMetadata: data.keepMetadata,
- };
-
- this.dialog.loading = true;
- fetch(new URL("api/v1/cache", document.baseURI), {
- method: "put",
- body: JSON.stringify(data),
- headers: { "Content-Type": "application/json" },
- }).then(response => {
- if (!response.ok) throw response;
- return response.json();
- }).then(json => {
- this.selection = [];
- this.editMode = false;
- this.dialog.loading = false;
- this.dialog.visible = false;
-
- json.forEach(book => {
- var item = items.find(el => el.id === book.id);
- this.bookmarks.splice(item.index, 1, book);
- });
- }).catch(err => {
- this.selection = [];
- this.editMode = false;
- this.dialog.loading = false;
-
- this.getErrorMessage(err).then(msg => {
- this.showErrorDialog(msg);
- })
- });
- }
- });
- },
- showDialogAddTags(items) {
- // Check and filter items
- if (typeof items !== "object") return;
- if (!Array.isArray(items)) items = [items];
-
- items = items.filter(item => {
- var id = (typeof item.id === "number") ? item.id : 0,
- index = (typeof item.index === "number") ? item.index : -1;
-
- return id > 0 && index > -1;
- });
-
- if (items.length === 0) return;
-
- // Show dialog
- this.showDialog({
- title: "Add New Tags",
- content: "Add new tags to selected bookmarks",
- fields: [{
- name: "tags",
- label: "Comma separated tags",
- value: "",
- separator: ",",
- dictionary: this.tags.map(tag => tag.name)
- }],
- mainText: 'OK',
- secondText: 'Cancel',
- mainClick: (data) => {
- // Validate input
- var tags = data.tags
- .toLowerCase()
- .replace(/\s+/g, ' ')
- .split(/\s*,\s*/g)
- .filter(tag => tag.trim() !== '')
- .map(tag => {
- return {
- name: tag.trim()
- };
- });
-
- if (tags.length === 0) return;
-
- // Send data
- var request = {
- ids: items.map(item => item.id),
- tags: tags
- }
-
- this.dialog.loading = true;
- fetch(new URL("api/v1/bookmarks/tags", document.baseURI), {
- method: "put",
- body: JSON.stringify(request),
- headers: { "Content-Type": "application/json" },
- }).then(response => {
- if (!response.ok) throw response;
- return response.json();
- }).then(json => {
- this.selection = [];
- this.editMode = false;
- this.dialog.loading = false;
- this.dialog.visible = false;
-
- json.forEach(book => {
- var item = items.find(el => el.id === book.id);
- this.bookmarks.splice(item.index, 1, book);
- });
- }).catch(err => {
- this.selection = [];
- this.editMode = false;
- this.dialog.loading = false;
-
- this.getErrorMessage(err).then(msg => {
- this.showErrorDialog(msg);
- })
- });
- }
- });
- },
- showDialogTags() {
- this.dialogTags.visible = true;
- this.dialogTags.editMode = false;
- this.dialogTags.secondText = this.activeAccount.owner ? "Rename Tags" : "";
- },
- showDialogRenameTag(tag) {
- this.showDialog({
- title: "Rename Tag",
- content: `Change the name for tag "#${tag.name}"`,
- fields: [{
- name: "newName",
- label: "New tag name",
- value: tag.name,
- }],
- mainText: "OK",
- secondText: "Cancel",
- secondClick: () => {
- this.dialog.visible = false;
- this.dialogTags.visible = true;
- },
- escPressed: () => {
- this.dialog.visible = false;
- this.dialogTags.visible = true;
- },
- mainClick: (data) => {
- // Save the old query
- var rxSpace = /\s+/g,
- oldTagQuery = rxSpace.test(tag.name) ? `"#${tag.name}"` : `#${tag.name}`,
- newTagQuery = rxSpace.test(data.newName) ? `"#${data.newName}"` : `#${data.newName}`;
-
- // Send data
- var newData = {
- id: tag.id,
- name: data.newName,
- };
-
- this.dialog.loading = true;
- fetch(new URL("api/tag", document.baseURI), {
- method: "PUT",
- body: JSON.stringify(newData),
- headers: { "Content-Type": "application/json" },
- }).then(response => {
- if (!response.ok) throw response;
- return response.json();
- }).then(() => {
- tag.name = data.newName;
-
- this.dialog.loading = false;
- this.dialog.visible = false;
- this.dialogTags.visible = true;
- this.dialogTags.editMode = false;
- this.tags.sort((a, b) => {
- var aName = a.name.toLowerCase(),
- bName = b.name.toLowerCase();
-
- if (aName < bName) return -1;
- else if (aName > bName) return 1;
- else return 0;
- });
-
- if (this.search.includes(oldTagQuery)) {
- this.search = this.search.replace(oldTagQuery, newTagQuery);
- this.loadData();
- }
- }).catch(err => {
- this.dialog.loading = false;
- this.dialogTags.visible = false;
- this.dialogTags.editMode = false;
- this.getErrorMessage(err).then(msg => {
- this.showErrorDialog(msg);
- })
- });
- },
- });
- },
- },
- mounted() {
- // Prepare history state watcher
- var stateWatcher = (e) => {
- var state = e.state || {},
- activePage = state.activePage || "page-home",
- search = state.search || "",
- page = state.page || 1;
-
- if (activePage !== "page-home") return;
-
- this.page = page;
- this.search = search;
- this.loadData(false);
- }
-
- window.addEventListener('popstate', stateWatcher);
- this.$once('hook:beforeDestroy', () => {
- window.removeEventListener('popstate', stateWatcher);
- })
-
- // Set initial parameter
- var url = new Url;
- this.search = url.query.search || "";
- this.page = url.query.page || 1;
-
- this.loadData(false, true);
- }
-}
diff --git a/internal/http/frontend/js/page/setting.js b/internal/http/frontend/js/page/setting.js
deleted file mode 100644
index 82be3e8aa..000000000
--- a/internal/http/frontend/js/page/setting.js
+++ /dev/null
@@ -1,304 +0,0 @@
-var template = `
-`;
-
-import customDialog from "../component/dialog.js";
-import basePage from "./base.js";
-
-export default {
- template: template,
- mixins: [basePage],
- components: {
- customDialog
- },
- data() {
- return {
- loading: false,
- accounts: []
- }
- },
- methods: {
- saveSetting() {
- this.$emit("setting-changed", {
- showId: this.appOptions.showId,
- listMode: this.appOptions.listMode,
- hideThumbnail: this.appOptions.hideThumbnail,
- hideExcerpt: this.appOptions.hideExcerpt,
- nightMode: this.appOptions.nightMode,
- keepMetadata: this.appOptions.keepMetadata,
- useArchive: this.appOptions.useArchive,
- makePublic: this.appOptions.makePublic,
- });
- },
- loadAccounts() {
- if (this.loading) return;
-
- this.loading = true;
- fetch(new URL("api/v1/accounts", document.baseURI), {headers: {'Content-Type': 'application/json'}})
- .then(response => {
- if (!response.ok) throw response;
- return response.json();
- })
- .then(json => {
- this.loading = false;
- this.accounts = json;
- })
- .catch(err => {
- this.loading = false;
- this.getErrorMessage(err).then(msg => {
- this.showErrorDialog(msg);
- })
- });
- },
- showDialogNewAccount() {
- this.showDialog({
- title: "New Account",
- content: "Input new account's data :",
- fields: [{
- name: "username",
- label: "Username",
- value: "",
- }, {
- name: "password",
- label: "Password",
- type: "password",
- value: "",
- }, {
- name: "repeat",
- label: "Repeat password",
- type: "password",
- value: "",
- }, {
- name: "visitor",
- label: "This account is for visitor",
- type: "check",
- value: false,
- }],
- mainText: "OK",
- secondText: "Cancel",
- mainClick: (data) => {
- if (data.username === "") {
- this.showErrorDialog("Username must not empty");
- return;
- }
-
- if (data.password === "") {
- this.showErrorDialog("Password must not empty");
- return;
- }
-
- if (data.password !== data.repeat) {
- this.showErrorDialog("Password does not match");
- return;
- }
-
- var request = {
- username: data.username,
- password: data.password,
- owner: !data.visitor,
- }
-
- this.dialog.loading = true;
- fetch(new URL("api/v1/accounts", document.baseURI), {
- method: "post",
- body: JSON.stringify(request),
- headers: {
- "Content-Type": "application/json",
- }
- }).then(response => {
- if (!response.ok) throw response;
- return response;
- }).then(() => {
- this.dialog.loading = false;
- this.dialog.visible = false;
-
- this.accounts.push({ username: data.username, owner: !data.visitor });
- this.accounts.sort((a, b) => {
- var nameA = a.username.toLowerCase(),
- nameB = b.username.toLowerCase();
-
- if (nameA < nameB) {
- return -1;
- }
-
- if (nameA > nameB) {
- return 1;
- }
-
- return 0;
- });
- }).catch(err => {
- this.dialog.loading = false;
- this.getErrorMessage(err).then(msg => {
- this.showErrorDialog(msg);
- })
- });
- }
- });
- },
- showDialogChangePassword(account) {
- this.showDialog({
- title: "Change Password",
- content: "Input new password :",
- fields: [{
- name: "oldPassword",
- label: "Old password",
- type: "password",
- value: "",
- }, {
- name: "password",
- label: "New password",
- type: "password",
- value: "",
- }, {
- name: "repeat",
- label: "Repeat password",
- type: "password",
- value: "",
- }],
- mainText: "OK",
- secondText: "Cancel",
- mainClick: (data) => {
- if (data.oldPassword === "") {
- this.showErrorDialog("Old password must not empty");
- return;
- }
-
- if (data.password === "") {
- this.showErrorDialog("New password must not empty");
- return;
- }
-
- if (data.password !== data.repeat) {
- this.showErrorDialog("Password does not match");
- return;
- }
-
- var request = {
- username: account.username,
- oldPassword: data.oldPassword,
- newPassword: data.password,
- owner: account.owner,
- }
-
- this.dialog.loading = true;
- fetch(new URL("api/v1/accounts", document.baseURI), {
- method: "put",
- body: JSON.stringify(request),
- headers: {
- "Content-Type": "application/json",
- },
- }).then(response => {
- if (!response.ok) throw response;
- return response;
- }).then(() => {
- this.dialog.loading = false;
- this.dialog.visible = false;
- }).catch(err => {
- this.dialog.loading = false;
- this.getErrorMessage(err).then(msg => {
- this.showErrorDialog(msg);
- })
- });
- }
- });
- },
- showDialogDeleteAccount(account, idx) {
- this.showDialog({
- title: "Delete Account",
- content: `Delete account "${account.username}" ?`,
- mainText: "Yes",
- secondText: "No",
- mainClick: () => {
- this.dialog.loading = true;
- fetch(`/api/v1/accounts`, {
- method: "delete",
- body: JSON.stringify([account.username]),
- headers: {
- "Content-Type": "application/json",
- },
- }).then(response => {
- if (!response.ok) throw response;
- return response;
- }).then(() => {
- this.dialog.loading = false;
- this.dialog.visible = false;
- this.accounts.splice(idx, 1);
- }).catch(err => {
- this.dialog.loading = false;
- this.getErrorMessage(err).then(msg => {
- this.showErrorDialog(msg);
- })
- });
- }
- });
- },
- },
- mounted() {
- this.loadAccounts();
- }
-}
diff --git a/internal/http/frontend/less/archive.less b/internal/http/frontend/less/archive.less
deleted file mode 100644
index 599a521cb..000000000
--- a/internal/http/frontend/less/archive.less
+++ /dev/null
@@ -1,73 +0,0 @@
-:root {
- --main : #F44336;
- --border : #E5E5E5;
- --colorLink : #999;
- --archiveHeaderBg: rgba(255, 255, 255, 0.95);
-
- @media (prefers-color-scheme: dark) {
- --border : #191919;
- --archiveHeaderBg: rgba(41, 41, 41, 0.95);
- }
-}
-
-#shiori-archive-header {
- top : 0;
- left : 0;
- right : 0;
- height : 60px;
- position : fixed;
- padding : 0 16px;
- display : flex;
- flex-flow : row wrap;
- align-items : center;
- font-size : 16px;
- border-bottom : 1px solid var(--border);
- background-color: var(--archiveHeaderBg);
- z-index : 9999999999;
-
- * {
- border-width: 0;
- box-sizing : border-box;
- font-family : "Source Sans Pro", sans-serif;
- margin : 0;
- padding : 0;
- }
-
- >*:not(:last-child) {
- margin-right: 8px;
- }
-
- >.spacer {
- flex: 1;
- }
-
- #shiori-logo {
- font-size : 2em;
- font-weight: 100;
- color : var(--main);
-
- span {
- margin-right: 8px;
- }
- }
-
- a {
- display : block;
- color : var(--colorLink);
- text-decoration: underline;
-
- &:hover,
- &:focus {
- color: var(--main);
- }
- }
-
- @media (max-width: 600px) {
- font-size: 14px;
- height : 50px;
-
- #shiori-logo {
- font-size: 1.5em;
- }
- }
-}
\ No newline at end of file
diff --git a/internal/http/frontend/login.html b/internal/http/frontend/login.html
deleted file mode 100644
index 3e17b0da3..000000000
--- a/internal/http/frontend/login.html
+++ /dev/null
@@ -1,139 +0,0 @@
-
-
-
-
-
- Login - Shiori
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/internal/http/routes/api/v1/api.go b/internal/http/routes/api/v1/api.go
index e70b39506..65f8bd563 100644
--- a/internal/http/routes/api/v1/api.go
+++ b/internal/http/routes/api/v1/api.go
@@ -9,17 +9,14 @@ import (
)
type APIRoutes struct {
- logger *logrus.Logger
- deps *config.Dependencies
+ logger *logrus.Logger
+ deps *config.Dependencies
+ loginHandler model.LegacyLoginHandler
}
func (r *APIRoutes) Setup(g *gin.RouterGroup) model.Routes {
- if r.deps.Config.Development {
- r.handle(g, "/debug", NewDebugPIRoutes(r.logger, r.deps))
- }
-
// Account API handles authentication in each route
- r.handle(g, "/auth", NewAuthAPIRoutes(r.logger, r.deps))
+ r.handle(g, "/auth", NewAuthAPIRoutes(r.logger, r.deps, r.loginHandler))
// From here on, all routes require authentication
g.Use(middleware.AuthenticationRequired())
@@ -34,9 +31,10 @@ func (s *APIRoutes) handle(g *gin.RouterGroup, path string, routes model.Routes)
routes.Setup(group)
}
-func NewAPIRoutes(logger *logrus.Logger, deps *config.Dependencies) *APIRoutes {
+func NewAPIRoutes(logger *logrus.Logger, deps *config.Dependencies, loginHandler model.LegacyLoginHandler) *APIRoutes {
return &APIRoutes{
- logger: logger,
- deps: deps,
+ logger: logger,
+ deps: deps,
+ loginHandler: loginHandler,
}
}
diff --git a/internal/http/routes/api/v1/auth.go b/internal/http/routes/api/v1/auth.go
index 0a1152e02..a7403fabf 100644
--- a/internal/http/routes/api/v1/auth.go
+++ b/internal/http/routes/api/v1/auth.go
@@ -14,8 +14,9 @@ import (
)
type AuthAPIRoutes struct {
- logger *logrus.Logger
- deps *config.Dependencies
+ logger *logrus.Logger
+ deps *config.Dependencies
+ legacyLoginHandler model.LegacyLoginHandler
}
func (r *AuthAPIRoutes) Setup(group *gin.RouterGroup) model.Routes {
@@ -25,10 +26,6 @@ func (r *AuthAPIRoutes) Setup(group *gin.RouterGroup) model.Routes {
return r
}
-func (r *AuthAPIRoutes) setCookie(c *gin.Context, token string, expiration time.Time) {
- c.SetCookie("auth", token, int(expiration.Unix()), "/", "", !r.deps.Config.Development, false)
-}
-
type loginRequestPayload struct {
Username string `json:"username" validate:"required"`
Password string `json:"password" validate:"required"`
@@ -46,7 +43,9 @@ func (p *loginRequestPayload) IsValid() error {
}
type loginResponseMessage struct {
- Token string `json:"token"`
+ Token string `json:"token"`
+ SessionID string `json:"session"` // Deprecated, used only for legacy APIs
+ Expiration int64 `json:"expires"` // Deprecated, used only for legacy APIs
}
// loginHandler godoc
@@ -87,12 +86,18 @@ func (r *AuthAPIRoutes) loginHandler(c *gin.Context) {
return
}
- responseMessage := loginResponseMessage{
- Token: token,
+ sessionID, err := r.legacyLoginHandler(*account, time.Hour*24*30)
+ if err != nil {
+ r.logger.WithError(err).Error("failed execute legacy login handler")
+ response.SendInternalServerError(c)
+ return
}
- // TODO: move cookie logic to frontend routes
- r.setCookie(c, token, expiration)
+ responseMessage := loginResponseMessage{
+ Token: token,
+ SessionID: sessionID,
+ Expiration: expiration.Unix(),
+ }
response.Send(c, http.StatusOK, responseMessage)
}
@@ -124,8 +129,6 @@ func (r *AuthAPIRoutes) refreshHandler(c *gin.Context) {
Token: token,
}
- r.setCookie(c, token, expiration)
-
response.Send(c, http.StatusAccepted, responseMessage)
}
@@ -147,9 +150,10 @@ func (r *AuthAPIRoutes) meHandler(c *gin.Context) {
response.Send(c, http.StatusOK, ctx.GetAccount())
}
-func NewAuthAPIRoutes(logger *logrus.Logger, deps *config.Dependencies) *AuthAPIRoutes {
+func NewAuthAPIRoutes(logger *logrus.Logger, deps *config.Dependencies, loginHandler model.LegacyLoginHandler) *AuthAPIRoutes {
return &AuthAPIRoutes{
- logger: logger,
- deps: deps,
+ logger: logger,
+ deps: deps,
+ legacyLoginHandler: loginHandler,
}
}
diff --git a/internal/http/routes/api/v1/auth_test.go b/internal/http/routes/api/v1/auth_test.go
index e74c16f6b..3b2ad1c73 100644
--- a/internal/http/routes/api/v1/auth_test.go
+++ b/internal/http/routes/api/v1/auth_test.go
@@ -15,6 +15,10 @@ import (
"github.com/stretchr/testify/require"
)
+func noopLegacyLoginHandler(_ model.Account, _ time.Duration) (string, error) {
+ return "", nil
+}
+
func TestAccountsRoute(t *testing.T) {
logger := logrus.New()
ctx := context.TODO()
@@ -22,7 +26,7 @@ func TestAccountsRoute(t *testing.T) {
t.Run("login invalid", func(t *testing.T) {
g := testutil.NewGin()
_, deps := testutil.GetTestConfigurationAndDependencies(t, ctx, logger)
- router := NewAuthAPIRoutes(logger, deps)
+ router := NewAuthAPIRoutes(logger, deps, noopLegacyLoginHandler)
router.Setup(g.Group("/"))
w := httptest.NewRecorder()
body := []byte(`{"username": "gopher"}`)
@@ -35,7 +39,7 @@ func TestAccountsRoute(t *testing.T) {
t.Run("login incorrect", func(t *testing.T) {
g := testutil.NewGin()
_, deps := testutil.GetTestConfigurationAndDependencies(t, ctx, logger)
- router := NewAuthAPIRoutes(logger, deps)
+ router := NewAuthAPIRoutes(logger, deps, noopLegacyLoginHandler)
router.Setup(g.Group("/"))
w := httptest.NewRecorder()
body := []byte(`{"username": "gopher", "password": "shiori"}`)
@@ -48,7 +52,7 @@ func TestAccountsRoute(t *testing.T) {
t.Run("login correct", func(t *testing.T) {
g := testutil.NewGin()
_, deps := testutil.GetTestConfigurationAndDependencies(t, ctx, logger)
- router := NewAuthAPIRoutes(logger, deps)
+ router := NewAuthAPIRoutes(logger, deps, noopLegacyLoginHandler)
router.Setup(g.Group("/"))
// Create an account manually to test
@@ -73,7 +77,7 @@ func TestAccountsRoute(t *testing.T) {
g := testutil.NewGin()
g.Use(middleware.AuthMiddleware(deps))
- router := NewAuthAPIRoutes(logger, deps)
+ router := NewAuthAPIRoutes(logger, deps, noopLegacyLoginHandler)
router.Setup(g.Group("/"))
// Create an account manually to test
@@ -101,7 +105,7 @@ func TestAccountsRoute(t *testing.T) {
g := testutil.NewGin()
g.Use(middleware.AuthMiddleware(deps))
- router := NewAuthAPIRoutes(logger, deps)
+ router := NewAuthAPIRoutes(logger, deps, noopLegacyLoginHandler)
router.Setup(g.Group("/"))
req := httptest.NewRequest("GET", "/me", nil)
@@ -155,7 +159,7 @@ func TestRefreshHandler(t *testing.T) {
g := testutil.NewGin()
_, deps := testutil.GetTestConfigurationAndDependencies(t, ctx, logger)
- router := NewAuthAPIRoutes(logger, deps)
+ router := NewAuthAPIRoutes(logger, deps, noopLegacyLoginHandler)
g.Use(middleware.AuthMiddleware(deps)) // Requires AuthMiddleware to manipulate context
router.Setup(g.Group("/"))
diff --git a/internal/http/routes/api/v1/debug.go b/internal/http/routes/api/v1/debug.go
deleted file mode 100644
index 4a60a2da7..000000000
--- a/internal/http/routes/api/v1/debug.go
+++ /dev/null
@@ -1,41 +0,0 @@
-package api_v1
-
-import (
- "github.com/gin-gonic/gin"
- "github.com/go-shiori/shiori/internal/config"
- "github.com/go-shiori/shiori/internal/http/response"
- "github.com/go-shiori/shiori/internal/model"
- "github.com/sirupsen/logrus"
-)
-
-type DebugAPIRoutes struct {
- logger *logrus.Logger
- deps *config.Dependencies
-}
-
-func (r *DebugAPIRoutes) Setup(group *gin.RouterGroup) model.Routes {
- group.GET("/create_user", r.createUserHandler)
- return r
-}
-
-func (r *DebugAPIRoutes) createUserHandler(c *gin.Context) {
- account := model.Account{
- Username: "shiori",
- Password: "gopher",
- Owner: true,
- }
-
- if err := r.deps.Database.SaveAccount(c, account); err != nil {
- response.SendError(c, 500, err.Error())
- return
- }
-
- response.Send(c, 201, account)
-}
-
-func NewDebugPIRoutes(logger *logrus.Logger, deps *config.Dependencies) *DebugAPIRoutes {
- return &DebugAPIRoutes{
- logger: logger,
- deps: deps,
- }
-}
diff --git a/internal/http/routes/frontend.go b/internal/http/routes/frontend.go
index 3c234dc7a..c963c6ce9 100644
--- a/internal/http/routes/frontend.go
+++ b/internal/http/routes/frontend.go
@@ -2,45 +2,82 @@ package routes
import (
"embed"
+ "html/template"
"net/http"
- "time"
+ "path/filepath"
"github.com/gin-contrib/gzip"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"github.com/go-shiori/shiori/internal/config"
- "github.com/go-shiori/shiori/internal/http/frontend"
+ views "github.com/go-shiori/shiori/internal/view"
"github.com/sirupsen/logrus"
)
-type frontendFS struct {
+type assetsFS struct {
http.FileSystem
+ logger *logrus.Logger
}
-func (fs frontendFS) Exists(prefix string, path string) bool {
+func (fs assetsFS) Exists(prefix string, path string) bool {
_, err := fs.Open(path)
+ if err != nil {
+ logrus.WithError(err).WithField("path", path).WithField("prefix", prefix).Error("requested frontend file not found")
+ }
return err == nil
}
-func NewFrontendFS(fs embed.FS) static.ServeFileSystem {
- return frontendFS{
+func (fs assetsFS) Open(name string) (http.File, error) {
+ f, err := fs.FileSystem.Open(filepath.Join("assets", name))
+ if err != nil {
+ logrus.WithError(err).WithField("path", name).Error("requested frontend file not found")
+ }
+ return f, err
+}
+
+func newAssetsFS(logger *logrus.Logger, fs embed.FS) static.ServeFileSystem {
+ return assetsFS{
+ logger: logger,
FileSystem: http.FS(fs),
}
}
type FrontendRoutes struct {
logger *logrus.Logger
- maxAge time.Duration
+ cfg *config.Config
+}
+
+func (r *FrontendRoutes) loadTemplates(e *gin.Engine) {
+ tmpl, err := template.New("html").Delims("$$", "$$").ParseFS(views.Templates, "*.html")
+ if err != nil {
+ r.logger.WithError(err).Error("Failed to parse templates")
+ return
+ }
+ e.SetHTMLTemplate(tmpl)
}
func (r *FrontendRoutes) Setup(e *gin.Engine) {
- e.Use(gzip.Gzip(gzip.DefaultCompression))
- e.Use(static.Serve("/", NewFrontendFS(frontend.Assets)))
+ group := e.Group("/")
+ e.Delims("$$", "$$")
+ r.loadTemplates(e)
+ // e.LoadHTMLGlob("internal/view/*.html")
+ group.Use(gzip.Gzip(gzip.DefaultCompression))
+ group.GET("/login", func(ctx *gin.Context) {
+ ctx.HTML(http.StatusOK, "login.html", gin.H{
+ "RootPath": r.cfg.Http.RootPath,
+ })
+ })
+ group.GET("/", func(ctx *gin.Context) {
+ ctx.HTML(http.StatusOK, "index.html", gin.H{
+ "RootPath": r.cfg.Http.RootPath,
+ })
+ })
+ e.StaticFS("/assets", newAssetsFS(r.logger, views.Assets))
}
-func NewFrontendRoutes(logger *logrus.Logger, cfg config.HttpConfig) *FrontendRoutes {
+func NewFrontendRoutes(logger *logrus.Logger, cfg *config.Config) *FrontendRoutes {
return &FrontendRoutes{
logger: logger,
- maxAge: cfg.Routes.Frontend.MaxAge,
+ cfg: cfg,
}
}
diff --git a/internal/http/routes/frontend_test.go b/internal/http/routes/frontend_test.go
index 7e442520c..8db1913bf 100644
--- a/internal/http/routes/frontend_test.go
+++ b/internal/http/routes/frontend_test.go
@@ -1,12 +1,13 @@
package routes
import (
+ "context"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
- "github.com/go-shiori/shiori/internal/config"
+ "github.com/go-shiori/shiori/internal/testutil"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)
@@ -14,8 +15,10 @@ import (
func TestFrontendRoutes(t *testing.T) {
logger := logrus.New()
+ cfg, _ := testutil.GetTestConfigurationAndDependencies(t, context.Background(), logger)
+
g := gin.Default()
- router := NewFrontendRoutes(logger, config.HttpConfig{})
+ router := NewFrontendRoutes(logger, cfg)
router.Setup(g)
t.Run("/", func(t *testing.T) {
@@ -25,16 +28,16 @@ func TestFrontendRoutes(t *testing.T) {
require.Equal(t, 200, w.Code)
})
- t.Run("/login.html", func(t *testing.T) {
+ t.Run("/login", func(t *testing.T) {
w := httptest.NewRecorder()
- req, _ := http.NewRequest("GET", "/login.html", nil)
+ req, _ := http.NewRequest("GET", "/login", nil)
g.ServeHTTP(w, req)
require.Equal(t, 200, w.Code)
})
t.Run("/css/stylesheet.css", func(t *testing.T) {
w := httptest.NewRecorder()
- req, _ := http.NewRequest("GET", "/css/stylesheet.css", nil)
+ req, _ := http.NewRequest("GET", "/assets/css/stylesheet.css", nil)
g.ServeHTTP(w, req)
require.Equal(t, 200, w.Code)
})
diff --git a/internal/http/routes/legacy.go b/internal/http/routes/legacy.go
new file mode 100644
index 000000000..7b7bea271
--- /dev/null
+++ b/internal/http/routes/legacy.go
@@ -0,0 +1,134 @@
+package routes
+
+import (
+ "net/http"
+ "time"
+
+ "github.com/gin-gonic/gin"
+ "github.com/go-shiori/shiori/internal/config"
+ "github.com/go-shiori/shiori/internal/model"
+ "github.com/go-shiori/shiori/internal/webserver"
+ "github.com/gofrs/uuid"
+ "github.com/julienschmidt/httprouter"
+ "github.com/pkg/errors"
+ "github.com/sirupsen/logrus"
+)
+
+type LegacyAPIRoutes struct {
+ logger *logrus.Logger
+ cfg *config.Config
+ deps *config.Dependencies
+ legacyHandler *webserver.Handler
+}
+
+func (r *LegacyAPIRoutes) convertHttprouteParams(params gin.Params) httprouter.Params {
+ routerParams := httprouter.Params{}
+ for _, p := range params {
+ if p.Key == "filepath" {
+ r.logger.WithField("value", p.Value).Error("filepath")
+ }
+ routerParams = append(routerParams, httprouter.Param{
+ Key: p.Key,
+ Value: p.Value,
+ })
+ }
+ return routerParams
+}
+
+func (r *LegacyAPIRoutes) handle(handler func(w http.ResponseWriter, r *http.Request, ps httprouter.Params)) gin.HandlerFunc {
+ return func(ctx *gin.Context) {
+ handler(ctx.Writer, ctx.Request, r.convertHttprouteParams(ctx.Params))
+ }
+}
+
+func (r *LegacyAPIRoutes) HandleLogin(account model.Account, expTime time.Duration) (string, error) {
+ // Create session ID
+ sessionID, err := uuid.NewV4()
+ if err != nil {
+ return "", errors.Wrap(err, "failed to create session ID")
+ }
+
+ // Save session ID to cache
+ strSessionID := sessionID.String()
+ r.legacyHandler.SessionCache.Set(strSessionID, account, expTime)
+
+ return strSessionID, nil
+}
+
+func (r *LegacyAPIRoutes) HandleLogout(c *gin.Context) error {
+ sessionID := r.legacyHandler.GetSessionID(c.Request)
+ r.legacyHandler.SessionCache.Delete(sessionID)
+ return nil
+}
+
+func (r *LegacyAPIRoutes) Setup(g *gin.Engine) {
+ r.legacyHandler = webserver.GetLegacyHandler(webserver.Config{
+ DB: r.deps.Database,
+ DataDir: r.cfg.Storage.DataDir,
+ RootPath: r.cfg.Http.RootPath,
+ Log: false, // Already done by gin
+ })
+ r.legacyHandler.PrepareSessionCache()
+ r.legacyHandler.PrepareTemplates()
+
+ legacyGroup := g.Group("/")
+
+ // Use a custom recovery handler to expose the errors that the frontend catch to redirect to
+ // the login page and display messages.
+ // This will be improved in the new API.
+ legacyGroup.Use(gin.CustomRecovery(func(c *gin.Context, err any) {
+ c.Data(http.StatusInternalServerError, "text/plain", []byte(err.(error).Error()))
+ }))
+
+ legacyGroup.POST("/api/logout", r.handle(r.legacyHandler.ApiLogout))
+
+ // router.GET(jp("/bookmark/:id/thumb"), withLogging(hdl.serveThumbnailImage))
+ legacyGroup.GET("/bookmark/:id/thumb", r.handle(r.legacyHandler.ServeThumbnailImage))
+ // router.GET(jp("/bookmark/:id/content"), withLogging(hdl.serveBookmarkContent))
+ legacyGroup.GET("/bookmark/:id/content", r.handle(r.legacyHandler.ServeBookmarkContent))
+ // router.GET(jp("/bookmark/:id/ebook"), withLogging(hdl.serveBookmarkEbook))
+ legacyGroup.GET("/bookmark/:id/ebook", r.handle(r.legacyHandler.ServeBookmarkEbook))
+ // router.GET(jp("/bookmark/:id/archive/*filepath"), withLogging(hdl.serveBookmarkArchive))
+ legacyGroup.GET("/bookmark/:id/archive/*filepath", r.handle(r.legacyHandler.ServeBookmarkArchive))
+ // legacyGroup.GET("/bookmark/:id/archive/", r.handle(r.legacyHandler.ServeBookmarkArchive))
+
+ // router.GET(jp("/api/tags"), withLogging(hdl.apiGetTags))
+ legacyGroup.GET("/api/tags", r.handle(r.legacyHandler.ApiGetTags))
+ // router.PUT(jp("/api/tag"), withLogging(hdl.apiRenameTag))
+ legacyGroup.PUT("/api/tags", r.handle(r.legacyHandler.ApiRenameTag))
+ // router.GET(jp("/api/bookmarks"), withLogging(hdl.apiGetBookmarks))
+ legacyGroup.GET("/api/bookmarks", r.handle(r.legacyHandler.ApiGetBookmarks))
+ // router.POST(jp("/api/bookmarks"), withLogging(hdl.apiInsertBookmark))
+ legacyGroup.POST("/api/bookmarks", r.handle(r.legacyHandler.ApiInsertBookmark))
+ // router.DELETE(jp("/api/bookmarks"), withLogging(hdl.apiDeleteBookmark))
+ legacyGroup.DELETE("/api/bookmarks", r.handle(r.legacyHandler.ApiDeleteBookmark))
+ // router.PUT(jp("/api/bookmarks"), withLogging(hdl.apiUpdateBookmark))
+ legacyGroup.PUT("/api/bookmarks", r.handle(r.legacyHandler.ApiUpdateBookmark))
+ // router.PUT(jp("/api/cache"), withLogging(hdl.apiUpdateCache))
+ legacyGroup.PUT("/api/cache", r.handle(r.legacyHandler.ApiUpdateCache))
+ // router.PUT(jp("/api/ebook"), withLogging(hdl.apiDownloadEbook))
+ legacyGroup.PUT("/api/ebook", r.handle(r.legacyHandler.ApiDownloadEbook))
+ // router.PUT(jp("/api/bookmarks/tags"), withLogging(hdl.apiUpdateBookmarkTags))
+ legacyGroup.PUT("/api/bookmarks/tags", r.handle(r.legacyHandler.ApiUpdateBookmarkTags))
+ // router.POST(jp("/api/bookmarks/ext"), withLogging(hdl.apiInsertViaExtension))
+ legacyGroup.POST("/api/bookmarks/ext", r.handle(r.legacyHandler.ApiInsertViaExtension))
+ // router.DELETE(jp("/api/bookmarks/ext"), withLogging(hdl.apiDeleteViaExtension))
+ legacyGroup.DELETE("/api/bookmarks/ext", r.handle(r.legacyHandler.ApiDeleteViaExtension))
+
+ // router.GET(jp("/api/accounts"), withLogging(hdl.apiGetAccounts))
+ legacyGroup.GET("/api/accounts", r.handle(r.legacyHandler.ApiGetAccounts))
+ // router.PUT(jp("/api/accounts"), withLogging(hdl.apiUpdateAccount))
+ legacyGroup.PUT("/api/accounts", r.handle(r.legacyHandler.ApiUpdateAccount))
+ // router.POST(jp("/api/accounts"), withLogging(hdl.apiInsertAccount))
+ legacyGroup.POST("/api/accounts", r.handle(r.legacyHandler.ApiInsertAccount))
+ // router.DELETE(jp("/api/accounts"), withLogging(hdl.apiDeleteAccount))
+ legacyGroup.DELETE("/api/accounts", r.handle(r.legacyHandler.ApiDeleteAccount))
+}
+
+func NewLegacyAPIRoutes(logger *logrus.Logger, deps *config.Dependencies, cfg *config.Config) *LegacyAPIRoutes {
+ return &LegacyAPIRoutes{
+ logger: logger,
+ cfg: cfg,
+ deps: deps,
+ }
+}
diff --git a/internal/http/server.go b/internal/http/server.go
index c03563f98..ac7412ab3 100644
--- a/internal/http/server.go
+++ b/internal/http/server.go
@@ -25,26 +25,40 @@ type HttpServer struct {
logger *logrus.Logger
}
-func (s *HttpServer) Setup(cfg config.HttpConfig, deps *config.Dependencies) *HttpServer {
+func (s *HttpServer) Setup(cfg *config.Config, deps *config.Dependencies) *HttpServer {
+ if !cfg.Development {
+ gin.SetMode(gin.ReleaseMode)
+ }
+
+ s.engine = gin.New()
+
+ s.engine.Use(requestid.New())
+
+ if cfg.Http.AccessLog {
+ s.engine.Use(ginlogrus.Logger(deps.Log))
+ }
+
s.engine.Use(
- requestid.New(),
- ginlogrus.Logger(deps.Log),
middleware.AuthMiddleware(deps),
gin.Recovery(),
)
- if !deps.Config.Development {
- gin.SetMode(gin.ReleaseMode)
+ if cfg.Http.ServeWebUI {
+ routes.NewFrontendRoutes(s.logger, cfg).Setup(s.engine)
}
+ // LegacyRoutes will be here until we migrate everything from internal/webserver to this new
+ // package.
+ legacyRoutes := routes.NewLegacyAPIRoutes(s.logger, deps, cfg)
+ legacyRoutes.Setup(s.engine)
+
s.handle("/system", routes.NewSystemRoutes(s.logger))
- s.handle("/bookmark", routes.NewBookmarkRoutes(s.logger, deps))
- s.handle("/api/v1", api_v1.NewAPIRoutes(s.logger, deps))
+ // s.handle("/bookmark", routes.NewBookmarkRoutes(s.logger, deps))
+ s.handle("/api/v1", api_v1.NewAPIRoutes(s.logger, deps, legacyRoutes.HandleLogin))
s.handle("/swagger", routes.NewSwaggerAPIRoutes(s.logger))
- routes.NewFrontendRoutes(s.logger, cfg).Setup(s.engine)
s.http.Handler = s.engine
- s.http.Addr = fmt.Sprintf("%s%d", cfg.Address, cfg.Port)
+ s.http.Addr = fmt.Sprintf("%s%d", cfg.Http.Address, cfg.Http.Port)
return s
}
@@ -81,10 +95,9 @@ func (s *HttpServer) WaitStop(ctx context.Context) {
}
}
-func NewHttpServer(logger *logrus.Logger, cfg config.HttpConfig, dependencies *config.Dependencies) *HttpServer {
+func NewHttpServer(logger *logrus.Logger) *HttpServer {
return &HttpServer{
logger: logger,
http: &http.Server{},
- engine: gin.New(),
}
}
diff --git a/internal/model/account.go b/internal/model/account.go
new file mode 100644
index 000000000..ab5ddee20
--- /dev/null
+++ b/internal/model/account.go
@@ -0,0 +1,25 @@
+package model
+
+// Account is the database model for account.
+type Account struct {
+ ID int `db:"id" json:"id"`
+ Username string `db:"username" json:"username"`
+ Password string `db:"password" json:"password,omitempty"`
+ Owner bool `db:"owner" json:"owner"`
+}
+
+// ToDTO converts Account to AccountDTO.
+func (a Account) ToDTO() AccountDTO {
+ return AccountDTO{
+ ID: a.ID,
+ Username: a.Username,
+ Owner: a.Owner,
+ }
+}
+
+// AccountDTO is data transfer object for Account.
+type AccountDTO struct {
+ ID int `json:"id"`
+ Username string `json:"username"`
+ Owner bool `json:"owner"`
+}
diff --git a/internal/model/legacy.go b/internal/model/legacy.go
new file mode 100644
index 000000000..4042537cd
--- /dev/null
+++ b/internal/model/legacy.go
@@ -0,0 +1,5 @@
+package model
+
+import "time"
+
+type LegacyLoginHandler func(account Account, expTime time.Duration) (string, error)
diff --git a/internal/model/model.go b/internal/model/model.go
index 8b65d5d9a..f3d739e4c 100644
--- a/internal/model/model.go
+++ b/internal/model/model.go
@@ -27,11 +27,3 @@ type Bookmark struct {
CreateArchive bool `json:"createArchive"`
CreateEbook bool `json:"createEbook"`
}
-
-// Account is person that allowed to access web interface.
-type Account struct {
- ID int `db:"id" json:"id"`
- Username string `db:"username" json:"username"`
- Password string `db:"password" json:"password,omitempty"`
- Owner bool `db:"owner" json:"owner"`
-}
diff --git a/internal/testutil/shiori.go b/internal/testutil/shiori.go
index 3285c718e..eb97e2850 100644
--- a/internal/testutil/shiori.go
+++ b/internal/testutil/shiori.go
@@ -26,12 +26,12 @@ func GetTestConfigurationAndDependencies(t *testing.T, ctx context.Context, logg
require.NoError(t, err)
require.NoError(t, db.Migrate())
- cfg.Http.Storage.DataDir = tempDir
+ cfg.Storage.DataDir = tempDir
deps := config.NewDependencies(logger, db, cfg)
deps.Database = db
deps.Domains.Auth = domains.NewAccountsDomain(logger, cfg.Http.SecretKey, db)
- deps.Domains.Archiver = domains.NewArchiverDomain(logger, cfg.Http.Storage.DataDir)
+ deps.Domains.Archiver = domains.NewArchiverDomain(logger, cfg.Storage.DataDir)
return cfg, deps
}
diff --git a/internal/view/css/archive.css b/internal/view/assets/css/archive.css
similarity index 100%
rename from internal/view/css/archive.css
rename to internal/view/assets/css/archive.css
diff --git a/internal/http/frontend/css/bookmark-item.css b/internal/view/assets/css/bookmark-item.css
similarity index 100%
rename from internal/http/frontend/css/bookmark-item.css
rename to internal/view/assets/css/bookmark-item.css
diff --git a/internal/http/frontend/css/custom-dialog.css b/internal/view/assets/css/custom-dialog.css
similarity index 100%
rename from internal/http/frontend/css/custom-dialog.css
rename to internal/view/assets/css/custom-dialog.css
diff --git a/internal/http/frontend/css/fontawesome.min.css b/internal/view/assets/css/fontawesome.min.css
similarity index 100%
rename from internal/http/frontend/css/fontawesome.min.css
rename to internal/view/assets/css/fontawesome.min.css
diff --git a/internal/http/frontend/css/fonts/fa-brands-400.eot b/internal/view/assets/css/fonts/fa-brands-400.eot
similarity index 100%
rename from internal/http/frontend/css/fonts/fa-brands-400.eot
rename to internal/view/assets/css/fonts/fa-brands-400.eot
diff --git a/internal/http/frontend/css/fonts/fa-brands-400.svg b/internal/view/assets/css/fonts/fa-brands-400.svg
similarity index 100%
rename from internal/http/frontend/css/fonts/fa-brands-400.svg
rename to internal/view/assets/css/fonts/fa-brands-400.svg
diff --git a/internal/http/frontend/css/fonts/fa-brands-400.ttf b/internal/view/assets/css/fonts/fa-brands-400.ttf
similarity index 100%
rename from internal/http/frontend/css/fonts/fa-brands-400.ttf
rename to internal/view/assets/css/fonts/fa-brands-400.ttf
diff --git a/internal/http/frontend/css/fonts/fa-brands-400.woff b/internal/view/assets/css/fonts/fa-brands-400.woff
similarity index 100%
rename from internal/http/frontend/css/fonts/fa-brands-400.woff
rename to internal/view/assets/css/fonts/fa-brands-400.woff
diff --git a/internal/http/frontend/css/fonts/fa-brands-400.woff2 b/internal/view/assets/css/fonts/fa-brands-400.woff2
similarity index 100%
rename from internal/http/frontend/css/fonts/fa-brands-400.woff2
rename to internal/view/assets/css/fonts/fa-brands-400.woff2
diff --git a/internal/http/frontend/css/fonts/fa-regular-400.eot b/internal/view/assets/css/fonts/fa-regular-400.eot
similarity index 100%
rename from internal/http/frontend/css/fonts/fa-regular-400.eot
rename to internal/view/assets/css/fonts/fa-regular-400.eot
diff --git a/internal/http/frontend/css/fonts/fa-regular-400.svg b/internal/view/assets/css/fonts/fa-regular-400.svg
similarity index 100%
rename from internal/http/frontend/css/fonts/fa-regular-400.svg
rename to internal/view/assets/css/fonts/fa-regular-400.svg
diff --git a/internal/http/frontend/css/fonts/fa-regular-400.ttf b/internal/view/assets/css/fonts/fa-regular-400.ttf
similarity index 100%
rename from internal/http/frontend/css/fonts/fa-regular-400.ttf
rename to internal/view/assets/css/fonts/fa-regular-400.ttf
diff --git a/internal/http/frontend/css/fonts/fa-regular-400.woff b/internal/view/assets/css/fonts/fa-regular-400.woff
similarity index 100%
rename from internal/http/frontend/css/fonts/fa-regular-400.woff
rename to internal/view/assets/css/fonts/fa-regular-400.woff
diff --git a/internal/http/frontend/css/fonts/fa-regular-400.woff2 b/internal/view/assets/css/fonts/fa-regular-400.woff2
similarity index 100%
rename from internal/http/frontend/css/fonts/fa-regular-400.woff2
rename to internal/view/assets/css/fonts/fa-regular-400.woff2
diff --git a/internal/http/frontend/css/fonts/fa-solid-900.eot b/internal/view/assets/css/fonts/fa-solid-900.eot
similarity index 100%
rename from internal/http/frontend/css/fonts/fa-solid-900.eot
rename to internal/view/assets/css/fonts/fa-solid-900.eot
diff --git a/internal/http/frontend/css/fonts/fa-solid-900.svg b/internal/view/assets/css/fonts/fa-solid-900.svg
similarity index 100%
rename from internal/http/frontend/css/fonts/fa-solid-900.svg
rename to internal/view/assets/css/fonts/fa-solid-900.svg
diff --git a/internal/http/frontend/css/fonts/fa-solid-900.ttf b/internal/view/assets/css/fonts/fa-solid-900.ttf
similarity index 100%
rename from internal/http/frontend/css/fonts/fa-solid-900.ttf
rename to internal/view/assets/css/fonts/fa-solid-900.ttf
diff --git a/internal/http/frontend/css/fonts/fa-solid-900.woff b/internal/view/assets/css/fonts/fa-solid-900.woff
similarity index 100%
rename from internal/http/frontend/css/fonts/fa-solid-900.woff
rename to internal/view/assets/css/fonts/fa-solid-900.woff
diff --git a/internal/http/frontend/css/fonts/fa-solid-900.woff2 b/internal/view/assets/css/fonts/fa-solid-900.woff2
similarity index 100%
rename from internal/http/frontend/css/fonts/fa-solid-900.woff2
rename to internal/view/assets/css/fonts/fa-solid-900.woff2
diff --git a/internal/http/frontend/css/fonts/source-sans-pro-v13-latin-200.woff b/internal/view/assets/css/fonts/source-sans-pro-v13-latin-200.woff
similarity index 100%
rename from internal/http/frontend/css/fonts/source-sans-pro-v13-latin-200.woff
rename to internal/view/assets/css/fonts/source-sans-pro-v13-latin-200.woff
diff --git a/internal/http/frontend/css/fonts/source-sans-pro-v13-latin-200.woff2 b/internal/view/assets/css/fonts/source-sans-pro-v13-latin-200.woff2
similarity index 100%
rename from internal/http/frontend/css/fonts/source-sans-pro-v13-latin-200.woff2
rename to internal/view/assets/css/fonts/source-sans-pro-v13-latin-200.woff2
diff --git a/internal/http/frontend/css/fonts/source-sans-pro-v13-latin-600.woff b/internal/view/assets/css/fonts/source-sans-pro-v13-latin-600.woff
similarity index 100%
rename from internal/http/frontend/css/fonts/source-sans-pro-v13-latin-600.woff
rename to internal/view/assets/css/fonts/source-sans-pro-v13-latin-600.woff
diff --git a/internal/http/frontend/css/fonts/source-sans-pro-v13-latin-600.woff2 b/internal/view/assets/css/fonts/source-sans-pro-v13-latin-600.woff2
similarity index 100%
rename from internal/http/frontend/css/fonts/source-sans-pro-v13-latin-600.woff2
rename to internal/view/assets/css/fonts/source-sans-pro-v13-latin-600.woff2
diff --git a/internal/http/frontend/css/fonts/source-sans-pro-v13-latin-700.woff b/internal/view/assets/css/fonts/source-sans-pro-v13-latin-700.woff
similarity index 100%
rename from internal/http/frontend/css/fonts/source-sans-pro-v13-latin-700.woff
rename to internal/view/assets/css/fonts/source-sans-pro-v13-latin-700.woff
diff --git a/internal/http/frontend/css/fonts/source-sans-pro-v13-latin-700.woff2 b/internal/view/assets/css/fonts/source-sans-pro-v13-latin-700.woff2
similarity index 100%
rename from internal/http/frontend/css/fonts/source-sans-pro-v13-latin-700.woff2
rename to internal/view/assets/css/fonts/source-sans-pro-v13-latin-700.woff2
diff --git a/internal/http/frontend/css/fonts/source-sans-pro-v13-latin-regular.woff b/internal/view/assets/css/fonts/source-sans-pro-v13-latin-regular.woff
similarity index 100%
rename from internal/http/frontend/css/fonts/source-sans-pro-v13-latin-regular.woff
rename to internal/view/assets/css/fonts/source-sans-pro-v13-latin-regular.woff
diff --git a/internal/http/frontend/css/fonts/source-sans-pro-v13-latin-regular.woff2 b/internal/view/assets/css/fonts/source-sans-pro-v13-latin-regular.woff2
similarity index 100%
rename from internal/http/frontend/css/fonts/source-sans-pro-v13-latin-regular.woff2
rename to internal/view/assets/css/fonts/source-sans-pro-v13-latin-regular.woff2
diff --git a/internal/http/frontend/css/source-sans-pro.min.css b/internal/view/assets/css/source-sans-pro.min.css
similarity index 100%
rename from internal/http/frontend/css/source-sans-pro.min.css
rename to internal/view/assets/css/source-sans-pro.min.css
diff --git a/internal/http/frontend/css/stylesheet.css b/internal/view/assets/css/stylesheet.css
similarity index 100%
rename from internal/http/frontend/css/stylesheet.css
rename to internal/view/assets/css/stylesheet.css
diff --git a/internal/view/js/component/bookmark.js b/internal/view/assets/js/component/bookmark.js
similarity index 100%
rename from internal/view/js/component/bookmark.js
rename to internal/view/assets/js/component/bookmark.js
diff --git a/internal/http/frontend/js/component/dialog.js b/internal/view/assets/js/component/dialog.js
similarity index 100%
rename from internal/http/frontend/js/component/dialog.js
rename to internal/view/assets/js/component/dialog.js
diff --git a/internal/http/frontend/js/component/pagination.js b/internal/view/assets/js/component/pagination.js
similarity index 100%
rename from internal/http/frontend/js/component/pagination.js
rename to internal/view/assets/js/component/pagination.js
diff --git a/internal/http/frontend/js/dayjs.min.js b/internal/view/assets/js/dayjs.min.js
similarity index 100%
rename from internal/http/frontend/js/dayjs.min.js
rename to internal/view/assets/js/dayjs.min.js
diff --git a/internal/view/js/page/base.js b/internal/view/assets/js/page/base.js
similarity index 100%
rename from internal/view/js/page/base.js
rename to internal/view/assets/js/page/base.js
diff --git a/internal/view/js/page/home.js b/internal/view/assets/js/page/home.js
similarity index 100%
rename from internal/view/js/page/home.js
rename to internal/view/assets/js/page/home.js
diff --git a/internal/view/js/page/setting.js b/internal/view/assets/js/page/setting.js
similarity index 100%
rename from internal/view/js/page/setting.js
rename to internal/view/assets/js/page/setting.js
diff --git a/internal/http/frontend/js/url.js b/internal/view/assets/js/url.js
similarity index 100%
rename from internal/http/frontend/js/url.js
rename to internal/view/assets/js/url.js
diff --git a/internal/http/frontend/js/url.min.js b/internal/view/assets/js/url.min.js
similarity index 100%
rename from internal/http/frontend/js/url.min.js
rename to internal/view/assets/js/url.min.js
diff --git a/internal/http/frontend/js/vue.js b/internal/view/assets/js/vue.js
similarity index 100%
rename from internal/http/frontend/js/vue.js
rename to internal/view/assets/js/vue.js
diff --git a/internal/http/frontend/js/vue.min.js b/internal/view/assets/js/vue.min.js
similarity index 100%
rename from internal/http/frontend/js/vue.min.js
rename to internal/view/assets/js/vue.min.js
diff --git a/internal/view/less/archive.less b/internal/view/assets/less/archive.less
similarity index 100%
rename from internal/view/less/archive.less
rename to internal/view/assets/less/archive.less
diff --git a/internal/http/frontend/less/bookmark-item.less b/internal/view/assets/less/bookmark-item.less
similarity index 100%
rename from internal/http/frontend/less/bookmark-item.less
rename to internal/view/assets/less/bookmark-item.less
diff --git a/internal/http/frontend/less/custom-dialog.less b/internal/view/assets/less/custom-dialog.less
similarity index 100%
rename from internal/http/frontend/less/custom-dialog.less
rename to internal/view/assets/less/custom-dialog.less
diff --git a/internal/http/frontend/less/stylesheet.less b/internal/view/assets/less/stylesheet.less
similarity index 100%
rename from internal/http/frontend/less/stylesheet.less
rename to internal/view/assets/less/stylesheet.less
diff --git a/internal/http/frontend/res/apple-touch-icon-144x144.png b/internal/view/assets/res/apple-touch-icon-144x144.png
similarity index 100%
rename from internal/http/frontend/res/apple-touch-icon-144x144.png
rename to internal/view/assets/res/apple-touch-icon-144x144.png
diff --git a/internal/http/frontend/res/apple-touch-icon-152x152.png b/internal/view/assets/res/apple-touch-icon-152x152.png
similarity index 100%
rename from internal/http/frontend/res/apple-touch-icon-152x152.png
rename to internal/view/assets/res/apple-touch-icon-152x152.png
diff --git a/internal/http/frontend/res/favicon-16x16.png b/internal/view/assets/res/favicon-16x16.png
similarity index 100%
rename from internal/http/frontend/res/favicon-16x16.png
rename to internal/view/assets/res/favicon-16x16.png
diff --git a/internal/http/frontend/res/favicon-32x32.png b/internal/view/assets/res/favicon-32x32.png
similarity index 100%
rename from internal/http/frontend/res/favicon-32x32.png
rename to internal/view/assets/res/favicon-32x32.png
diff --git a/internal/http/frontend/res/favicon.ico b/internal/view/assets/res/favicon.ico
similarity index 100%
rename from internal/http/frontend/res/favicon.ico
rename to internal/view/assets/res/favicon.ico
diff --git a/internal/view/content.html b/internal/view/content.html
index 9a9b89e65..308e3aeed 100644
--- a/internal/view/content.html
+++ b/internal/view/content.html
@@ -8,19 +8,19 @@
-
-
-
-
-
+
+
+
+
+
-
-
-
-
+
+
+
+
-
-
+
+
@@ -45,7 +45,7 @@
-
+
+
@@ -42,10 +42,10 @@
-