-
Notifications
You must be signed in to change notification settings - Fork 0
/
devServer.go
90 lines (80 loc) · 1.97 KB
/
devServer.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
package devServer
import (
"github.com/evanw/esbuild/pkg/api"
"github.com/seasonjs/espack/internal/devServer/pkg/liteS"
"github.com/seasonjs/espack/internal/utils"
"mime"
"net"
"net/http"
"path/filepath"
"strings"
)
//TODO: Server-sent events 解决HMR 问题
//不能使用esbuild的serve,因为它的serve不会提供额外的插件输出能力
type INMemory uint8
const (
IsINMemoryTrue INMemory = iota
IsINMemoryFalse
)
type ctx struct {
iNMemory INMemory
r *liteS.Engine
res *map[string][]byte
}
func NewDevServer() *ctx {
//if !utils.Env.Dev() {
// liteS.SetMode(liteS.ReleaseMode)
//}
// 默认内存读取 TODO:支持从文件夹读取
return &ctx{IsINMemoryTrue, liteS.Default(), nil}
}
// Add 将build好的资源转换为Map格式
func (c *ctx) Add(outputFiles *[]api.OutputFile) *ctx {
res := make(map[string][]byte)
if len(*outputFiles) <= 0 {
c.res = nil
}
for _, file := range *outputFiles {
path, _ := utils.FS.ConvertPath(file.Path)
res[path] = file.Contents
}
c.res = &res
return c
}
// Run liteS服务器启动
func (c *ctx) Run() {
r := c.r
res := make(map[string][]byte)
go func() {
//TODO:proxy,websocket,使用内置特性
r.GET("/*action", func(g *liteS.Context) {
p := strings.ToLower(g.Request.URL.Path)
//转换为映射的路径key TODO: 替换成从配置中读取
p, _ = utils.FS.ConvertPath("./dist/" + p)
// 是否是文件夹或者根路径
if p == "" || utils.FS.IsDir(p) {
p = filepath.Join(p, "/index.html")
}
ct := g.ContentType()
if len(ct) <= 0 {
ct = mime.TypeByExtension(filepath.Ext(p))
g.Header("Content-Type", ct)
}
if c.res != nil {
res = *c.res
}
if len(res) <= 0 {
g.String(http.StatusOK, "构建进行中...")
return
}
body, ok := res[p]
if ok {
g.String(http.StatusOK, string(body))
return
}
g.String(http.StatusNotFound, "资源未找到...")
})
ln, _ := net.Listen("tcp", "127.0.0.1:8080")
r.RunListener(ln)
}()
}