Skip to content

Commit

Permalink
Polish, e2e tests, better first user experience (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
SmilyOrg authored Jan 6, 2024
2 parents 7f16523 + 0a588d2 commit 9f4be1e
Show file tree
Hide file tree
Showing 99 changed files with 7,802 additions and 2,297 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ tmp
tools/jupyter/data
.env
dist/
node_modules/
/blob-report/
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ before:
hooks:
- go mod tidy
- go generate -x
- sh -c "cd ui && npm install && npm run build && npm run docs:build"
- sh -c "cd docs && npm install && npm run docs:build && cd ../ui && npm install && npm run build"
builds:
- env:
- CGO_ENABLED=0
Expand Down
12 changes: 11 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@
"mode": "auto",
"program": "${workspaceFolder}",
"buildFlags": "-tags embedui"
}
},
{
"name": "Debug bddgen",
"request": "launch",
"program": "${workspaceFolder}/e2e/node_modules/playwright-bdd/dist/cli/index.js",
"autoAttachChildProcesses": true,
"skipFiles": [
"<node_internals>/**"
],
"type": "node"
},
]
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020-2023 Miha Lunar
Copyright (c) 2020-2024 Miha Lunar

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
156 changes: 156 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package main

import (
"fmt"
"log"
"os"
"path/filepath"
"photofield/internal/clip"
"photofield/internal/collection"
"photofield/internal/fs"
"photofield/internal/geo"
"photofield/internal/image"
"photofield/internal/layout"
"photofield/internal/render"
"photofield/tag"
"strings"

"github.com/goccy/go-yaml"
"github.com/imdario/mergo"
)

type AppConfig struct {
Collections []collection.Collection `json:"collections"`
ExpandedPaths []string `json:"-"`
Layout layout.Layout `json:"layout"`
Render render.Render `json:"render"`
Media image.Config `json:"media"`
AI clip.AI `json:"ai"`
Geo geo.Config `json:"geo"`
Tags tag.Config `json:"tags"`
TileRequests TileRequestConfig `json:"tile_requests"`
}

var CONFIG_FILENAME = "configuration.yaml"

func watchConfig(dataDir string, callback func(appConfig *AppConfig)) {
w, err := fs.NewFileWatcher(filepath.Join(dataDir, CONFIG_FILENAME))
if err != nil {
log.Fatalln("Unable to watch config", err)
}

var expandWatcher *fs.Watcher
var collectionsChanged chan fs.Event
var appConfig *AppConfig
reloadConfig := func() {
appConfig, err = loadConfig(dataDir)
if err != nil {
log.Fatalln("Unable to load config", err)
}
expandWatcher.Close()
if len(appConfig.ExpandedPaths) > 0 {
expandWatcher, err = fs.NewPathsWatcher(appConfig.ExpandedPaths)
if err != nil {
log.Fatalln("Unable to watch expanded paths", err)
}
collectionsChanged = expandWatcher.Events
}
callback(appConfig)
}

reloadConfig()
go func() {
defer w.Close()
for {
select {
case <-w.Events:
log.Println("config changed, reloading")
case e := <-collectionsChanged:
switch e.Op {
case fs.Update, fs.Rename:
if info, err := os.Stat(e.Path); err != nil || !info.IsDir() {
// Updated or renamed item was not a dir
continue
}
case fs.Remove:
removed := false
for _, collection := range appConfig.Collections {
for _, dir := range collection.Dirs {
if dir == e.Path {
removed = true
break
}
}
if removed {
break
}
}
if !removed {
// Removed item was not a collection dir
continue
}
}
log.Println("collection changed, reloading")
}
reloadConfig()
}
}()
}

func initDefaults() {
if err := yaml.Unmarshal(defaultsYaml, &defaults); err != nil {
panic(err)
}
}

func loadConfig(dataDir string) (*AppConfig, error) {
path := filepath.Join(dataDir, CONFIG_FILENAME)

var appConfig AppConfig

log.Printf("config path %v", path)
bytes, err := os.ReadFile(path)
if err == nil {
if err := yaml.Unmarshal(bytes, &appConfig); err != nil {
return nil, fmt.Errorf("unable to parse %s: %w", path, err)
} else if err := mergo.Merge(&appConfig, defaults); err != nil {
return nil, fmt.Errorf("unable to merge defaults: %w", err)
}
} else {
log.Printf("config read failed (using defaults) for %s: %v", path, err)
appConfig = defaults
}

// Expand collections
expanded := make([]collection.Collection, 0, len(appConfig.Collections))
expandedDirs := make(map[string]bool) // Track deduplicated dirs
for _, collection := range appConfig.Collections {
if collection.ExpandSubdirs {
for _, dir := range collection.Dirs {
expandedDirs[dir] = true
}
expanded = append(expanded, collection.Expand()...)
} else {
expanded = append(expanded, collection)
}
}
appConfig.Collections = expanded
appConfig.ExpandedPaths = make([]string, 0, len(expandedDirs))
for dir := range expandedDirs {
appConfig.ExpandedPaths = append(appConfig.ExpandedPaths, dir)
}

for i := range appConfig.Collections {
collection := &appConfig.Collections[i]
collection.GenerateId()
collection.Layout = strings.ToUpper(collection.Layout)
if collection.Limit > 0 && collection.IndexLimit == 0 {
collection.IndexLimit = collection.Limit
}
}

appConfig.Media.AI = appConfig.AI
appConfig.Media.DataDir = dataDir
appConfig.Tags.Enable = appConfig.Tags.Enable || appConfig.Tags.Enabled
return &appConfig, nil
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
2 changes: 1 addition & 1 deletion ui/docs/configuration.md → docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ collections:
## Defaults
<<< @/../../defaults.yaml
<<< @/../defaults.yaml
File renamed without changes.
8 changes: 4 additions & 4 deletions ui/docs/credits.md → docs/credits.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
* [BalmUI] - Material UI components
* [OpenLayers] - in-browser tiled image rendering
* [geoBoundaries](https://www.geoboundaries.org/) for geographic boundary data used for reverse geolocation
* [+ more Go libraries](go.mod)
* [+ more npm libraries](ui/package.json)
* + more Go libraries
* + more npm libraries

## Special Thanks

Expand All @@ -26,11 +26,11 @@
Thanks to all the authors of the third party libraries used in this project.

### API
<<< @/../../go.mod
<<< @/../go.mod

### UI

<<< @/../package.json
<<< @/../ui/package.json


[open an issue]: https://github.com/SmilyOrg/photofield/issues
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion ui/docs/license.md → docs/license.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

This project is licensed under the MIT License.

<<< @/../../LICENSE
<<< @/../LICENSE
File renamed without changes.
Loading

0 comments on commit 9f4be1e

Please sign in to comment.