Skip to content

Commit

Permalink
web-ui: foundation, auth, search, download (closes #69)
Browse files Browse the repository at this point in the history
  • Loading branch information
maizy committed Sep 18, 2021
2 parents 6c6df7a + 77fcaa6 commit 25350b3
Show file tree
Hide file tree
Showing 37 changed files with 2,441 additions and 48 deletions.
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ cd "${PROJECT_DIR}"

VERSION=`git describe --tags --always`

go build -o bin -ldflags "-X main.Version=${VERSION}" -x ./...
go build -o bin -ldflags "-X dev.maizy.ru/ponylib/ponylib_app.version=${VERSION}" -x ./...
4 changes: 2 additions & 2 deletions cmd/ponylib/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ var rootCmd = &cobra.Command{
Use: "ponylib",
}

func Execute(appVersion string) error {
func Execute() error {
rootCmd.SetVersionTemplate(`{{printf "%s version: %s\n" .Name .Version}}`)
rootCmd.Version = ponylib_app.NormalizeVersion(appVersion)
rootCmd.Version = ponylib_app.GetVersion()
return rootCmd.Execute()
}

Expand Down
29 changes: 23 additions & 6 deletions cmd/ponylib/cmd/web_ui.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
package cmd

import (
"fmt"
"os"
"time"

"github.com/gin-gonic/gin"
"github.com/spf13/cobra"

"dev.maizy.ru/ponylib/ponylib_app/db"
"dev.maizy.ru/ponylib/ponylib_app/web"
)

// flags
var (
bindPort int
bindHost string
bindPort int
bindHost string
debugMode bool
devMode bool
)

func init() {
webUiCmd.PersistentFlags().IntVar(&bindPort, "port", 55387, "bind port")
webUiCmd.PersistentFlags().StringVar(&bindHost, "host", "127.0.0.1", "bind host")
webUiCmd.PersistentFlags().BoolVar(&debugMode, "debug", false, "enable debug mode")
webUiCmd.PersistentFlags().BoolVar(&devMode, "dev-mode", false, "enable dev mode (templates hot reload)")

rootCmd.AddCommand(webUiCmd)
}
Expand All @@ -32,10 +38,21 @@ var webUiCmd = &cobra.Command{
if os.Getenv("DB_SKIP_MIGRATIONS") != "1" {
migrateOrExit(conn)
}
if !debugMode {
gin.SetMode(gin.ReleaseMode)
}

printErrF("TODO: launch web app at: http://%s:%d", bindHost, bindPort)
for {
time.Sleep(1 * time.Minute)
engine := gin.Default()
web.SetupMiddlewares(engine)
web.SetupTemplates(engine, devMode)
web.AppendRouters(engine, conn, devMode)

addr := fmt.Sprintf("%s:%d", bindHost, bindPort)
fmt.Printf("launching web app at: http://%s\n", addr)
err := engine.Run(addr)
if err != nil {
printErrF("Unable to start WebUi: %s", err)
os.Exit(1)
}
},
}
4 changes: 1 addition & 3 deletions cmd/ponylib/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"dev.maizy.ru/ponylib/cmd/ponylib/cmd"
)

var Version string = "unknown"

func main() {
_ = cmd.Execute(Version)
_ = cmd.Execute()
}
33 changes: 28 additions & 5 deletions fb2_parser/fb2_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ func (b *Book) String() string {
return b.Title
}

func (b *Book) WrittenAt() string {
if b.Date != nil {
if b.Date.Month() == time.January && b.Date.Day() == 1 {
return b.Date.Format("2006")
}
return b.Date.Format("2006-01-02")
} else if b.FormattedDate != nil {
return *b.FormattedDate
}
return ""
}

func (b *Book) AdditionalInfoString() *string {
if b.Date != nil || b.FormattedDate != nil || b.Lang != nil {
var sb strings.Builder
Expand All @@ -34,11 +46,7 @@ func (b *Book) AdditionalInfoString() *string {
sb.WriteString(", ")
}
sb.WriteString("Written at: ")
if b.Date != nil {
sb.WriteString(b.Date.Format("2006-01-02"))
} else {
sb.WriteString(*b.FormattedDate)
}
sb.WriteString(b.WrittenAt())
}
return u.StrPtr(sb.String())
}
Expand Down Expand Up @@ -208,3 +216,18 @@ func (f *Fb2Metadata) String() string {
}
return sb.String()
}

func (f *Fb2Metadata) AuthorsString() string {
if f.Authors != nil {
var sb strings.Builder
for index, author := range *f.Authors {
if index > 0 {
sb.WriteString(", ")
}
sb.WriteString(author.String())
}
return sb.String()
} else {
return ""
}
}
16 changes: 14 additions & 2 deletions fb2_parser/genres.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,26 @@ func (g *GenreIndexEntity) String() string {
sb.WriteString("[")
sb.WriteString(g.Genre.Code)
sb.WriteString("] ")
sb.WriteString(g.En())
sb.WriteString(" (")
sb.WriteString(g.Ru())
sb.WriteString(")")
return sb.String()
}

func (g *GenreIndexEntity) En() string {
var sb strings.Builder
sb.WriteString(g.Category.En)
sb.WriteString(" :: ")
sb.WriteString(g.Genre.En)
sb.WriteString(" (")
return sb.String()
}

func (g *GenreIndexEntity) Ru() string {
var sb strings.Builder
sb.WriteString(g.Category.Ru)
sb.WriteString(" :: ")
sb.WriteString(g.Genre.Ru)
sb.WriteString(")")
return sb.String()
}

Expand Down
4 changes: 2 additions & 2 deletions fb2_scanner/archives.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "github.com/gabriel-vasile/mimetype"
type SupportedArchive string

const (
Zip SupportedArchive = "zip"
ZipArchive SupportedArchive = "zip"
)

func archivePtr(archive SupportedArchive) *SupportedArchive {
Expand All @@ -16,7 +16,7 @@ func DetectSupportedArchive(path string) *SupportedArchive {
if mime, err := mimetype.DetectFile(path); err == nil {
switch mime.String() {
case "application/zip":
return archivePtr(Zip)
return archivePtr(ZipArchive)
default:
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion fb2_scanner/fs_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (d *DirectoryTarget) RId() resource.RId {
}

func (d *DirectoryTarget) Type() TargetType {
return FsDir
return FsDirTargetType
}

func (d *DirectoryTarget) GetUUID() *string {
Expand Down
2 changes: 1 addition & 1 deletion fb2_scanner/fs_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (f *FileTarget) RId() resource.RId {
}

func (f *FileTarget) Type() TargetType {
return FsFile
return FsFileTargetType
}

func (f *FileTarget) GetUUID() *string {
Expand Down
25 changes: 25 additions & 0 deletions fb2_scanner/resource/rid.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package resource
import (
"errors"
"net/url"
"path"
)

type Q struct {
Expand All @@ -18,10 +19,34 @@ type RId struct {
Query []Q
}

const SubPathKey = "p"

func (u *RId) String() string {
return EncodeRId(u.Scheme, u.Path, u.Query)
}

func (u *RId) SubPath() string {
if u.Query != nil {
for _, pair := range u.Query {
if pair.Key == SubPathKey {
return pair.Value
}
}
}
return ""
}

func (u *RId) ResourceBaseName() string {
objectPath := u.Path
if subPath := u.SubPath(); subPath != "" {
objectPath = subPath
}
if objectPath != "" {
return path.Base(objectPath)
}
return ""
}

func EncodeRId(scheme, path string, query []Q) string {
internalUrl := url.URL{Scheme: scheme, Path: path}
urlQ := url.Values{}
Expand Down
66 changes: 55 additions & 11 deletions fb2_scanner/resource/rid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,53 @@ type args struct {
}

var tests = []struct {
name string
args args
full string
name string
args args
full string
baseName string
}{
{"full", args{"file", "/path/to/book.fb2", []Q{{"reload", "true"}}}, "file:///path/to/book.fb2?reload=true"},
{"only scheme", args{scheme: "special"}, "special:"},
{"scheme and path", args{scheme: "dir", path: "/path/to/dir"}, "dir:///path/to/dir"},
{"with space in path", args{scheme: "dir", path: "/path/to/dir with spaces"},
"dir:///path/to/dir%20with%20spaces"},
{"with unicode in path", args{scheme: "dir", path: "/unicode/path/привет"},
"dir:///unicode/path/%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82"},
{"repeated args", args{scheme: "special", query: []Q{{"k", "z"}, {"k", "a"}}}, "special:?k=z&k=a"},
{
"full",
args{"file", "/path/to/book.fb2", []Q{{"reload", "true"}}},
"file:///path/to/book.fb2?reload=true",
"book.fb2",
},
{
"only scheme",
args{scheme: "special"},
"special:",
"",
},
{
"scheme and path",
args{scheme: "dir", path: "/path/to/dir"},
"dir:///path/to/dir",
"dir",
},
{
"with space in path",
args{scheme: "dir", path: "/path/to/dir with spaces"},
"dir:///path/to/dir%20with%20spaces",
"dir with spaces",
},
{
"with unicode in path",
args{scheme: "dir", path: "/unicode/path/привет"},
"dir:///unicode/path/%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82",
"привет",
},
{
"repeated args",
args{scheme: "special", query: []Q{{"k", "z"}, {"k", "a"}}},
"special:?k=z&k=a",
"",
},
{
"with subpath",
args{scheme: "zip", path: "/path/to/archive.zip", query: []Q{{SubPathKey, "path/to/file.fb2"}}},
"zip:///path/to/archive.zip?p=path%2Fto%2Ffile.fb2",
"file.fb2",
},
}

func TestEncodeRId(t *testing.T) {
Expand All @@ -46,6 +81,15 @@ func TestRId_String(t *testing.T) {
}
}

func TestRId_BaseName(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := (&RId{tt.args.scheme, tt.args.path, tt.args.query}).ResourceBaseName()
assert.Equal(t, tt.baseName, got)
})
}
}

func TestDecodeRId(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Loading

0 comments on commit 25350b3

Please sign in to comment.