-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
155 lines (138 loc) · 3.94 KB
/
main.go
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
package main
import (
"context"
"embed"
"flag"
"fmt"
"log"
"mime"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/options"
"github.com/wailsapp/wails/v2/pkg/options/assetserver"
"github.com/wailsapp/wails/v2/pkg/options/linux"
"github.com/wailsapp/wails/v2/pkg/options/mac"
"github.com/wailsapp/wails/v2/pkg/options/windows"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
//go:embed all:frontend/dist
var assets embed.FS
//go:embed build/appicon.png
var icon []byte
func myFileHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
var err error
app := req.Context().Value(AppKey).(*App)
requestedFilename := strings.TrimPrefix(req.URL.Path, "/")
if strings.HasPrefix(requestedFilename, "@term2") {
requestedFilename = strings.TrimPrefix(requestedFilename, "@term2")
} else {
next.ServeHTTP(res, req)
return
}
if strings.HasPrefix(requestedFilename, ".") {
if app.dev {
cwd, err := os.Getwd()
if err != nil {
runtime.MessageDialog(app.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Error",
Message: "Couldn't find CWD",
})
runtime.Quit(app.ctx)
}
requestedFilename = filepath.Join(cwd, "assets", requestedFilename)
} else {
basePath, err := os.UserHomeDir()
if err != nil {
runtime.MessageDialog(app.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Error",
Message: "Couldn't find HOMEDIR",
})
runtime.Quit(app.ctx)
}
requestedFilename = filepath.Join(basePath, "/.term2/assets", requestedFilename)
}
}
logger.Printf("Requested file: %s\n", requestedFilename)
if stats, err := os.Stat(requestedFilename); err != nil || stats.IsDir() {
res.WriteHeader(http.StatusNotFound)
res.Write([]byte(fmt.Sprintf("File %s not found", requestedFilename)))
return
}
fileData, err := os.ReadFile(requestedFilename)
if err != nil {
res.WriteHeader(http.StatusBadRequest)
res.Write([]byte(fmt.Sprintf("Could not load file %s", requestedFilename)))
}
res.Write(fileData)
res.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(requestedFilename)))
res.Header().Set("Content-Length", fmt.Sprintf("%d", len(fileData)))
})
}
type mainKey int
const (
AppKey mainKey = iota
)
func main() {
var err error
tempDir := os.TempDir()
logFile, err = os.OpenFile(filepath.Join(tempDir, "term2.txt"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
logger = log.New(logFile, "App ", log.Lshortfile|log.Lmsgprefix|log.Ltime)
flag.Parse()
dev := flag.Arg(0) == "dev"
// Create an instance of the app structure
app := NewApp(dev)
appName := "term2"
// Create application with options
err = wails.Run(&options.App{
Title: appName,
Width: 1024,
Height: 768,
Frameless: true,
AssetServer: &assetserver.Options{
Assets: assets,
Middleware: assetserver.ChainMiddleware(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), AppKey, app)))
})
}, myFileHandler),
},
Windows: &windows.Options{
Theme: windows.Dark,
},
Linux: &linux.Options{
Icon: icon,
ProgramName: appName,
},
Mac: &mac.Options{
About: &mac.AboutInfo{
Title: appName,
Icon: icon,
},
},
SingleInstanceLock: &options.SingleInstanceLock{
UniqueId: "fToKHxo3iuaCiSNhBB8EELCsvy03EvV5npJur7neMFc",
OnSecondInstanceLaunch: func(secondInstanceData options.SecondInstanceData) {
runtime.WindowUnminimise(app.ctx)
runtime.Show(app.ctx)
},
},
BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 1},
OnStartup: app.startup,
OnShutdown: app.shutdown,
Bind: []interface{}{
app,
},
})
if err != nil {
println("Error:", err.Error())
}
}