-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathwork.go_
More file actions
507 lines (445 loc) · 13.5 KB
/
work.go_
File metadata and controls
507 lines (445 loc) · 13.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
package engine
import (
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"net"
"net/http"
"os"
"path/filepath"
"runtime/debug"
"strconv"
"strings"
"github.com/kitwork/engine/core"
"github.com/kitwork/engine/render"
"github.com/kitwork/engine/value"
"github.com/kitwork/engine/work"
"gopkg.in/yaml.v3"
)
type Config struct {
Port int `yaml:"port"`
Debug bool `yaml:"debug"`
Sources []string `yaml:"source"`
Master []string `yaml:"master"`
Identity string `yaml:"identity"`
Domain string `yaml:"domain"`
}
// Run starts the Kitwork Engine using the provided Config.
// It will automatically discover modular configs and database settings within cfg.Sources.
func Run(cfg *Config) {
if cfg == nil {
cfg = &Config{}
}
e := core.New()
// 1. Map Base Config
e.Config.Port = cfg.Port
if e.Config.Port == 0 {
e.Config.Port = 8080 // Default port
}
e.Config.Debug = cfg.Debug
e.Config.Sources = cfg.Sources
if len(e.Config.Sources) == 0 {
e.Config.Sources = []string{"./"}
}
// 3. Automated Discovery & Initialization
// This will scan each source for work.yaml, logic, and DATABASE configs.
for _, dir := range e.Config.Sources {
if e.Config.Debug {
fmt.Printf("📦 Loading source: %s\n", dir)
}
loadLogic(e, dir)
}
// 3. Finalize & Fire
e.SyncRegistry()
bootServer(e, e.Config.Port)
}
// LoadConfig loads the root configuration file (config.yaml).
func LoadConfig(dir string) (*Config, error) {
cfg := &Config{}
// Root config
path := filepath.Join(dir, "config.yaml")
if data, err := os.ReadFile(path); err == nil {
yaml.Unmarshal(data, cfg)
} else if os.IsNotExist(err) {
fmt.Printf("ℹ️ config.yaml not found at %s. Using default settings.\n", path)
}
// Set defaults
if cfg.Port == 0 {
cfg.Port = 8080
}
if len(cfg.Sources) == 0 {
cfg.Sources = []string{"./"}
}
return cfg, nil
}
func loadLogic(e *core.Engine, dir string) {
// PHA 1: Tìm và nạp file gốc (work.js) để khởi tạo Context / App trước
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if !info.IsDir() && info.Name() == "work.js" {
compileAndRun(e, dir, path)
}
return nil
})
if err != nil && e.Config.Debug {
fmt.Printf("⚠️ Warning: Error walking directory %s (Phase 1): %v\n", dir, err)
}
// PHA 2: Chạy các file JS khác (routes, api, jobs, etc.)
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
// Bỏ qua work.js vì đã nạp ở Pha 1
if !info.IsDir() && strings.HasSuffix(info.Name(), ".js") && info.Name() != "work.js" {
compileAndRun(e, dir, path)
}
return nil
})
if err != nil && e.Config.Debug {
fmt.Printf("⚠️ Warning: Error walking directory %s (Phase 2): %v\n", dir, err)
}
}
// Hàm helper để tránh lặp code khi Build & Trigger
func compileAndRun(e *core.Engine, dir, path string) {
content, _ := os.ReadFile(path)
// Extract tenant and domain from path: public/[tenant]/[domain]/...
tenantID := ""
domain := ""
relPath, _ := filepath.Rel(dir, path)
parts := strings.Split(filepath.ToSlash(relPath), "/")
if len(parts) >= 2 {
if strings.Contains(dir, "public") {
tenantID = parts[0]
if len(parts) >= 2 {
domain = parts[1]
}
}
}
w, err := e.Build(string(content), tenantID, domain, path)
if err == nil {
w.SourcePath, _ = filepath.Abs(path) // Track Source Path
if e.Config.Debug {
fmt.Printf("📜 Logic loaded: %s (Tenant: %s, Domain: %s)\n", path, tenantID, domain)
}
// GLOBAL BYTECODE PROPAGATION
if w.GetBytecode() != nil {
e.RegistryMu.Lock()
for _, rt := range e.Routers {
if rt.Work.Entity == tenantID && rt.Work.Domain == domain && rt.Work.GetBytecode() == nil {
rt.Work.SetBytecode(w.GetBytecode())
}
}
for _, c := range e.Crons {
if c.Work.Entity == tenantID && c.Work.Domain == domain && c.Work.GetBytecode() == nil {
c.Work.SetBytecode(w.GetBytecode())
}
}
e.RegistryMu.Unlock()
}
e.Trigger(context.TODO(), w, nil, nil)
} else {
fmt.Printf("❌ Code Error in %s: %v\n", path, err)
}
}
func bootServer(e *core.Engine, serverPort int) {
port := strconv.Itoa(serverPort)
fmt.Printf("🚀 Kitwork Engine online at http://localhost:%s\n", port)
e.RegistryMu.RLock()
fmt.Printf("🔍 Routes registered: %d\n", len(e.Routers))
e.RegistryMu.RUnlock()
// --- HANDLER CHÍNH (ROUTING + ASSETS) ---
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Recovery from Panics
defer func() {
if rec := recover(); rec != nil {
fmt.Printf("[CRITICAL] Panic Recovered: %v\n", rec)
debug.PrintStack()
http.Error(w, "Internal Server Error (Panic)", http.StatusInternalServerError)
}
}()
path := r.URL.Path
method := r.Method
e.RegistryMu.RLock()
// 1. TÌM ROUTE
var matchedRoute *work.Router
pathParams := make(map[string]value.Value)
host := r.Host
if h, _, err := net.SplitHostPort(host); err == nil {
host = h
}
if e.Config.Debug {
fmt.Printf("[DEBUG] Request Host: %s, Total Routers: %d\n", host, len(e.Routers))
}
for _, rt := range e.Routers {
if e.Config.Debug {
fmt.Printf("[DEBUG] Checking Route: Domain=%s, Method=%s, Path=%s (Request: Host=%s, Path=%s)\n", rt.Work.Domain, rt.Method, rt.Path, host, path)
}
if rt.Work.Domain != "" && rt.Work.Domain != host && rt.Work.Domain != "localhost" && host != "127.0.0.1" {
continue
}
if rt.Method == method || rt.Method == "ANY" {
if params, ok := matchRoute(path, rt.Path); ok {
matchedRoute = rt
for k, v := range params {
pathParams[k] = value.New(v)
}
break
}
}
}
if matchedRoute == nil && e.Config.Debug {
fmt.Printf("[DEBUG] No Route Matched for: %s %s (Host: %s)\n", method, path, host)
}
if matchedRoute != nil && e.Config.Debug {
fmt.Printf("[DEBUG] Matched Route TemplatePath: '%s'\n", matchedRoute.Work.TemplatePath)
}
// 2. TÌM ASSET TĨNH (Nếu không có route)
if matchedRoute == nil && (method == "GET" || method == "HEAD") {
if e.Config.Debug && path == "/favicon.ico" {
fmt.Printf("[DEBUG] Checking assets for %s. Asset count: %d\n", path, len(e.Config.Assets))
}
for _, asset := range e.Config.Assets {
cleanAssetPath := "/" + strings.Trim(asset.Path, "/")
info, err := os.Stat(asset.Dir)
if err != nil {
if e.Config.Debug {
fmt.Printf("[ASSET] Skip %s: %v\n", asset.Dir, err)
}
continue
}
if !info.IsDir() {
// Khớp chính xác file đơn lẻ (Ví dụ: /favicon.ico)
if path == cleanAssetPath {
if e.Config.Debug {
fmt.Printf("[ASSET] Serving File: %s -> %s\n", path, asset.Dir)
}
e.RegistryMu.RUnlock()
w.Header().Set("Cache-Control", "public, max-age=31536000")
http.ServeFile(w, r, asset.Dir)
return
}
} else {
// Khớp thư mục (Ví dụ: /public/*)
prefix := cleanAssetPath
if !strings.HasSuffix(prefix, "/") {
prefix += "/"
}
if path == cleanAssetPath || strings.HasPrefix(path, prefix) {
rel := ""
if path != cleanAssetPath {
rel = strings.TrimPrefix(path, prefix)
}
full := filepath.Join(asset.Dir, rel)
if fi, err := os.Stat(full); err == nil && !fi.IsDir() {
if e.Config.Debug {
fmt.Printf("[ASSET] Serving Prefix Match: %s -> %s\n", path, full)
}
e.RegistryMu.RUnlock()
w.Header().Set("Cache-Control", "public, max-age=31536000")
http.ServeFile(w, r, full)
return
}
}
}
}
}
e.RegistryMu.RUnlock()
if matchedRoute == nil {
fmt.Printf("[HTTP] 404: %s\n", path)
http.NotFound(w, r)
return
}
fmt.Printf("[HTTP] Matched: %s %s\n", method, path)
// 3. THỰC THI LOGIC
queryParams := make(map[string]value.Value)
for k, v := range r.URL.Query() {
if len(v) > 0 {
queryParams[k] = value.New(v[0])
}
}
bodyParams := make(map[string]value.Value)
if method != "GET" && r.Body != nil {
var bodyData map[string]any
if err := json.NewDecoder(r.Body).Decode(&bodyData); err == nil {
for k, v := range bodyData {
bodyParams[k] = value.New(v)
}
}
}
res := e.ExecuteLambda(&matchedRoute.Work, matchedRoute.GetDone(), r, w, queryParams, bodyParams, pathParams)
if res.Error != "" {
http.Error(w, res.Error, 500)
return
}
responseVal := res.Response.Data
if responseVal.K == value.Nil {
responseVal = res.Value
}
// Apply buffered headers
if res.Response.Headers != nil {
for k, v := range res.Response.Headers {
w.Header().Set(k, v)
}
}
// Apply status code
if res.Response.Code != 0 {
w.WriteHeader(res.Response.Code)
}
renderData := responseVal.Interface()
if renderData == nil {
renderData = make(map[string]any)
}
// If script called html(template, data), override TemplatePath
if res.Response.Type == "html" && responseVal.K == value.Map {
tVal := responseVal.Get("template")
if tVal.K == value.String {
tmplPath = tVal.Text()
}
dVal := responseVal.Get("data")
if dVal.K != value.Nil {
renderData = dVal.Interface()
}
}
if tmplPath != "" {
finalPath := tmplPath
if !filepath.IsAbs(finalPath) {
// 1. NGĂN XẾP ƯU TIÊN 1: Tìm ngay bên cạnh file JS (Angular Style / Co-location)
// Sử dụng SourcePath của Work (ví dụ index.js) để làm gốc
if matchedRoute.Work.SourcePath != "" {
dir := filepath.Dir(matchedRoute.Work.SourcePath)
possible := filepath.Join(dir, tmplPath)
if _, err := os.Stat(possible); err == nil {
finalPath = possible
goto found
}
}
// 2. NGĂN XẾP ƯU TIÊN 2: Tìm trong folder 'pages' chuẩn
globalViewDir := ""
if matchedRoute.Work.Entity != "" && matchedRoute.Work.Domain != "" {
globalViewDir = filepath.Join("public", matchedRoute.Work.Entity, matchedRoute.Work.Domain, "pages")
}
if globalViewDir != "" {
p := filepath.Join(globalViewDir, tmplPath)
if _, err := os.Stat(p); err == nil {
finalPath = p
goto found
}
}
}
// Nếu không tìm thấy tệp tin
if _, err := os.Stat(finalPath); err != nil {
fmt.Printf("[HTTP] Template Not Found: %s (Check your path in index.js)\n", tmplPath)
}
found:
content, err := os.ReadFile(finalPath)
if err == nil {
// Re-resolve globalViewDir for shell discovery
globalViewDir := ""
if matchedRoute.Work.Entity != "" && matchedRoute.Work.Domain != "" {
globalViewDir = filepath.Join("public", matchedRoute.Work.Entity, matchedRoute.Work.Domain, "pages")
}
// 1. Render the Page Fragment
rendered := render.RenderWithDir(string(content), renderData, filepath.Dir(finalPath), globalViewDir)
if w.Header().Get("Content-Type") == "" {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
}
w.Write([]byte(rendered))
return
} else {
fmt.Printf("[HTTP] Template Error: %v (Final Path: %s)\n", err, finalPath)
}
}
if res.Response.Type == "html" {
if w.Header().Get("Content-Type") == "" {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
}
w.Write([]byte(responseVal.Text()))
return
}
// AUTO-DETECT: SafeHTML Result -> Render as HTML
if res.Response.Type == "" && responseVal.S == value.SafeHTML {
if w.Header().Get("Content-Type") == "" {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
}
w.Write([]byte(responseVal.Text()))
return
}
// Fallback to JSON if ResType is empty or explicitly "json"
if res.Response.Type == "json" || res.Response.Type == "" {
if w.Header().Get("Content-Type") == "" {
w.Header().Set("Content-Type", "application/json")
}
outputData, _ := json.Marshal(responseVal.Interface())
fmt.Printf("[HTTP] Response: %s\n", string(outputData))
w.Write(outputData)
}
})
p, _ := strconv.Atoi(port)
for {
addr := fmt.Sprintf(":%s", strconv.Itoa(p))
l, err := net.Listen("tcp", addr)
if err == nil {
l.Close()
fmt.Printf("🚀 Kitwork Engine online at http://localhost:%d\n", p)
err = http.ListenAndServe(addr, nil)
if err != nil {
fmt.Printf("❌ Server Failed: %v\n", err)
}
break
}
p++
if p > 9000 {
break
}
}
}
func matchRoute(path, routePath string) (map[string]string, bool) {
if path == routePath {
return nil, true
}
// Handle Wildcard at the end: /assets/*
if strings.HasSuffix(routePath, "*") {
prefix := strings.TrimSuffix(routePath, "*")
if strings.HasPrefix(path, prefix) {
return nil, true
}
}
pSegments := strings.Split(strings.Trim(path, "/"), "/")
rSegments := strings.Split(strings.Trim(routePath, "/"), "/")
if len(pSegments) != len(rSegments) {
return nil, false
}
params := make(map[string]string)
for i := 0; i < len(rSegments); i++ {
if strings.HasPrefix(rSegments[i], ":") {
params[rSegments[i][1:]] = pSegments[i]
} else if rSegments[i] != pSegments[i] {
return nil, false
}
}
return params, true
}
func writeChecksum(path string) {
data, _ := os.ReadFile(path)
hash := sha256.Sum256(data)
os.WriteFile(path+".sha256", []byte(fmt.Sprintf("%x", hash)), 0644)
}
func verifyChecksum(path string) bool {
data, err := os.ReadFile(path)
if err != nil {
return false
}
hash := sha256.Sum256(data)
expected, _ := os.ReadFile(path + ".sha256")
return fmt.Sprintf("%x", hash) == string(expected)
}
// DummyWriter discards output for benchmark mode
type DummyWriter struct {
HeaderMap http.Header
}
func (d *DummyWriter) Header() http.Header { return d.HeaderMap }
func (d *DummyWriter) Write(b []byte) (int, error) { return len(b), nil }
func (d *DummyWriter) WriteHeader(statusCode int) {}