-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
150 lines (128 loc) · 4.17 KB
/
main.go
File metadata and controls
150 lines (128 loc) · 4.17 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
package main
import (
"bytes"
"embed"
_ "embed"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/pkg/events"
)
//go:embed frontend/dist
var assets embed.FS
// localDir 设置为包级别变量
var localDir string = "./"
// gFileName 当前打开的文件名
var gFileName string = ""
// bChanged 当前文件已修改
var bChanged bool = false
func getMimeType(filename string) string {
var mimeTypesByExt = map[string]string{
".avif": "image/avif",
".css": "text/css; charset=utf-8",
".gif": "image/gif",
".htm": "text/html; charset=utf-8",
".html": "text/html; charset=utf-8",
".jpeg": "image/jpeg",
".jpg": "image/jpeg",
".js": "text/javascript; charset=utf-8",
".json": "application/json",
".mjs": "text/javascript; charset=utf-8",
".pdf": "application/pdf",
".png": "image/png",
".svg": "image/svg+xml",
".wasm": "application/wasm",
".webp": "image/webp",
".xml": "text/xml; charset=utf-8",
}
return mimeTypesByExt[filepath.Ext(filename)]
}
func main() {
app := application.New(application.Options{
Name: "mdPad",
Description: "Markdown Editor Pad",
Services: []application.Service{
application.NewService(&App{}),
},
Assets: application.AssetOptions{
Handler: application.AssetFileServerFS(assets),
Middleware: func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
path := req.URL.Path
// log.Println(" --> Middleware path: ", path)
// 如果路径以 /dist 或 /wails 开头,或者以 http(s):// 开头,则直接跳过
if strings.HasPrefix(path, "/dist") || strings.HasPrefix(path, "/wails") || strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") {
next.ServeHTTP(rw, req)
return
}
// 检查本地文件是否存在
localFilePath := filepath.Join(localDir, path)
// log.Println(" --> Middleware real path: ", localFilePath)
if fileInfo, err := os.Stat(localFilePath); err == nil && !fileInfo.IsDir() {
// 如果本地文件存在,则使用 assetserver.ServeFile 处理
// log.Println(" --> Middleware find in local path: ", localFilePath)
fileData, _ := os.ReadFile(localFilePath)
header := rw.Header()
header.Set("Content-Length", fmt.Sprintf("%d", len(fileData)))
if mimeType := header.Get("Content-Type"); mimeType == "" {
mimeType = getMimeType(localFilePath)
header.Set("Content-Type", mimeType)
}
rw.WriteHeader(http.StatusOK)
io.Copy(rw, bytes.NewReader(fileData))
return
}
// 如果本地文件不存在,则调用 next.ServeHTTP
next.ServeHTTP(rw, req)
})
},
},
Mac: application.MacOptions{
ApplicationShouldTerminateAfterLastWindowClosed: true,
},
})
// Listen for files being used to open the application
app.OnApplicationEvent(events.Common.ApplicationStarted, func(event *application.ApplicationEvent) {
if len(os.Args) >= 2 {
if fileInfo, err := os.Stat(os.Args[1]); err == nil && !fileInfo.IsDir() {
gFileName = os.Args[1]
}
}
})
window := app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
Title: "mdPad",
Mac: application.MacWindow{
InvisibleTitleBarHeight: 50,
Backdrop: application.MacBackdropTranslucent,
TitleBar: application.MacTitleBarHiddenInset,
},
BackgroundColour: application.NewRGB(27, 38, 54),
URL: "/",
DefaultContextMenuDisabled: true,
EnableDragAndDrop: true,
ShouldClose: func(window *application.WebviewWindow) bool {
ok := true
if bChanged {
ok = false
app.EmitEvent("qsave")
}
return ok
},
})
window.OnWindowEvent(events.Common.WindowFilesDropped, func(event *application.WindowEvent) {
files := event.Context().DroppedFiles()
app.EmitEvent("files", files)
app.Logger.Info("Files Dropped!", "files", files)
})
// Run the application. This blocks until the application has been exited.
err := app.Run()
// If an error occurred while running the application, log it and exit.
if err != nil {
log.Fatal(err)
}
}