Repository navigation
Expand file tree
/
Copy pathmain_decision.go
More file actions
74 lines (65 loc) · 2.48 KB
/
Copy pathmain_decision.go
File metadata and controls
74 lines (65 loc) · 2.48 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
package server
import (
"os"
"path/filepath"
"webtyp.com/router/routescan"
)
// Startup decision, decided by one file with no configuration:
//
// config/routes.go present -> generate, compile and run the server main
// web/server/main.go present -> compile and run the user's own main (escape hatch if not generated)
// neither -> internal in-memory asset server, nothing compiled
//
// The escape hatch always wins: the tool never overwrites a main the user wrote.
// hasHandWrittenMain reports whether the user supplied their own server entry
// point at <AppRootDir>/<SourceDir>/<MainInputFile> (canonically web/server/main.go)
// that does not contain the generated header.
func (h *ServerHandler) hasHandWrittenMain() bool {
content, err := os.ReadFile(filepath.Join(h.AppRootDir, h.SourceDir, h.mainFileExternalServer))
if err != nil {
return false
}
return !IsGenerated(string(content))
}
// usesGeneratedMain reports whether this project's server process is the
// generated artifact: it declares config/routes.go and has no hand-written main.
func (h *ServerHandler) usesGeneratedMain() bool {
return HasRoutes(h.AppRootDir) && !h.hasHandWrittenMain()
}
// needsExternalProcess reports whether StartServer must compile and run a child
// process (the generated main, or the user's own) rather than serving assets
// from memory.
func (h *ServerHandler) needsExternalProcess() bool {
return h.hasHandWrittenMain() || h.usesGeneratedMain()
}
// serverMainRelPath is the compiler input, relative to AppRootDir: the user's
// own main when present, otherwise the generated artifact.
func (h *ServerHandler) serverMainRelPath() string {
if h.hasHandWrittenMain() {
return filepath.Join(h.SourceDir, h.mainFileExternalServer)
}
return filepath.Join(GeneratedMainDir, GeneratedMainFilename)
}
// routesManifestPath is the absolute path of config/routes.go for this project.
func (h *ServerHandler) routesManifestPath() string {
return filepath.Join(h.AppRootDir, routescan.DefaultFile)
}
// ensureServerMain writes the generated server main when it is needed and not
// already on disk or when it is a generated artifact.
func (h *ServerHandler) ensureServerMain(force bool) error {
if h.hasHandWrittenMain() {
return nil
}
if !force && !HasRoutes(h.AppRootDir) {
return nil
}
cfg := MainConfig{
Port: h.Port(),
PublicDir: h.PublicDir,
PlainHTTP: !h.Https,
}
if h.mainGenerator == nil {
return ErrNoMainGenerator
}
return h.mainGenerator(h.AppRootDir, cfg)
}