-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
198 lines (157 loc) · 5.5 KB
/
app.go
File metadata and controls
198 lines (157 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"context"
"database/sql"
"embed"
"log"
"net/http"
"path/filepath"
db "gvnotes/db/generated"
"gvnotes/internal/config"
"gvnotes/internal/controllers"
"gvnotes/internal/dto"
"gvnotes/internal/services"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// appMigrations is set from main.go before NewApp() is called.
var appMigrations embed.FS
// App is the Wails application struct. It acts as a facade over internal
// controllers, exposing methods to the frontend via Wails bindings.
// imageServerPort is the fixed port used for the local image HTTP server.
// Must match the proxy target in vite.config.ts for dev mode.
const imageServerPort = "34201"
type App struct {
ctx context.Context
sqlDB *sql.DB
auth *controllers.AuthController
notebook *controllers.NotebookController
note *controllers.NoteController
image *controllers.ImageController
}
// NewApp creates a new App application struct.
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. Dependencies are composed here.
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
sqlDB, err := config.OpenDB(appMigrations)
if err != nil {
log.Fatalf("failed to open database: %v", err)
}
a.sqlDB = sqlDB
imagesDir, err := config.ImagesDir()
if err != nil {
log.Fatalf("failed to resolve images directory: %v", err)
}
querier := db.New(sqlDB)
a.auth = controllers.NewAuthController(services.NewAuthService(querier))
a.notebook = controllers.NewNotebookController(services.NewNotebookService(querier))
a.note = controllers.NewNoteController(services.NewNoteService(querier))
a.image = controllers.NewImageController(services.NewImageService(querier, imagesDir))
// Start a local HTTP server on a fixed port to serve image files.
// In dev mode (Vite), this port is proxied via vite.config.ts server.proxy.
// In production, the Wails AssetServer Handler serves /images/* directly.
go func() {
if err := http.ListenAndServe("127.0.0.1:"+imageServerPort, &imageFileHandler{imagesDir: imagesDir}); err != nil {
log.Printf("image server stopped: %v", err)
}
}()
}
// shutdown is called when the app closes.
func (a *App) shutdown(_ context.Context) {
if a.sqlDB != nil {
a.sqlDB.Close()
}
}
// --- Auth ---
func (a *App) GetAuthStatus() interface{} {
return a.auth.GetAuthStatus()
}
func (a *App) SetPassword(password string) interface{} {
return a.auth.SetPassword(password)
}
func (a *App) VerifyPassword(password string) interface{} {
return a.auth.VerifyPassword(password)
}
// --- Notebooks ---
func (a *App) ListNotebooks(parentID string) ([]dto.NotebookListItem, error) {
return a.notebook.ListNotebooks(parentID)
}
func (a *App) GetNotebook(id string) (dto.NotebookDetail, error) {
return a.notebook.GetNotebook(id)
}
func (a *App) CreateNotebook(req dto.CreateNotebookRequest) (dto.NotebookDetail, error) {
return a.notebook.CreateNotebook(req)
}
func (a *App) UpdateNotebookTitle(id string, req dto.UpdateNotebookTitleRequest) (dto.NotebookDetail, error) {
return a.notebook.UpdateNotebookTitle(id, req)
}
func (a *App) UpdateNotebookPosition(id string, req dto.UpdatePositionRequest) error {
return a.notebook.UpdateNotebookPosition(id, req)
}
func (a *App) DeleteNotebook(id string) error {
return a.notebook.DeleteNotebook(id)
}
func (a *App) MoveNotebook(id string, req dto.MoveNotebookRequest) (dto.NotebookDetail, error) {
return a.notebook.MoveNotebook(id, req)
}
// --- Notes ---
func (a *App) ListNotes(notebookID string) ([]dto.NoteListItem, error) {
return a.note.ListNotes(notebookID)
}
func (a *App) GetNote(id string) (dto.NoteDetail, error) {
return a.note.GetNote(id)
}
func (a *App) CreateNote(req dto.CreateNoteRequest) (dto.NoteDetail, error) {
return a.note.CreateNote(req)
}
func (a *App) UpdateNoteTitle(id string, req dto.UpdateNoteTitleRequest) (dto.NoteDetail, error) {
return a.note.UpdateNoteTitle(id, req)
}
func (a *App) UpdateNoteContent(id string, req dto.UpdateNoteContentRequest) (dto.NoteDetail, error) {
return a.note.UpdateNoteContent(id, req)
}
func (a *App) UpdateNotePosition(id string, req dto.UpdatePositionRequest) error {
return a.note.UpdateNotePosition(id, req)
}
func (a *App) DeleteNote(id string) error {
return a.note.DeleteNote(id)
}
func (a *App) MoveNote(id string, req dto.MoveNoteRequest) (dto.NoteDetail, error) {
return a.note.MoveNote(id, req)
}
// --- Images ---
func (a *App) ListImagesByNote(noteID string) ([]dto.ImageItem, error) {
return a.image.ListImagesByNote(noteID)
}
func (a *App) GetImage(id string) (dto.ImageItem, error) {
return a.image.GetImage(id)
}
func (a *App) SaveImage(noteID string, mimeType string, data []byte) (dto.ImageItem, error) {
return a.image.SaveImage(noteID, mimeType, data)
}
func (a *App) DeleteImage(id string) error {
return a.image.DeleteImage(id)
}
func (a *App) GetImagePath(filename string) (string, error) {
return a.image.GetImagePath(filename)
}
// DownloadImage opens a native Save dialog and copies the image to the chosen path.
func (a *App) DownloadImage(filename string) error {
destPath, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
DefaultFilename: filename,
Title: "Guardar imagen",
Filters: []runtime.FileFilter{
{DisplayName: "Imágenes", Pattern: "*.png;*.jpg;*.jpeg;*.gif;*.webp"},
},
})
if err != nil || destPath == "" {
return err
}
// Preserve the original extension if the user didn't type one.
if filepath.Ext(destPath) == "" {
destPath += filepath.Ext(filename)
}
return a.image.DownloadImage(filename, destPath)
}