MCPcopy
hub / github.com/esm-dev/esm.sh / Start

Function Start

server/server.go:23–114  ·  view source on GitHub ↗

Start starts the esm.sh server

()

Source from the content-addressed store, hash-verified

21
22// Start starts the esm.sh server
23func Start() {
24 var cfile string
25 var err error
26
27 flag.StringVar(&cfile, "config", "config.json", "the config file path")
28 flag.Parse()
29
30 if existsFile(cfile) {
31 config, err = LoadConfig(cfile)
32 if err != nil {
33 fmt.Println(err.Error())
34 os.Exit(1)
35 }
36 if DEBUG {
37 fmt.Printf("%s [info] Config loaded from %s\n", time.Now().Format("2006-01-02 15:04:05"), cfile)
38 }
39 }
40
41 if DEBUG {
42 config.LogLevel = "debug"
43 } else {
44 // disable log color in release build
45 os.Setenv("NO_COLOR", "1")
46 }
47
48 logger, err := log.New(fmt.Sprintf("file:%s?buffer=64k&fileDateFormat=20060102&term", path.Join(config.LogDir, "server.log")))
49 if err != nil {
50 fmt.Println("failed to initialize logger:", err)
51 os.Exit(1)
52 }
53 if os.Getenv("ESMDIR") != "" {
54 logger.Term(false)
55 }
56 logger.SetLevelByName(config.LogLevel)
57
58 accessLogger, err := log.New(fmt.Sprintf("file:%s?buffer=1m&fileDateFormat=20060102", path.Join(config.LogDir, "access.log")))
59 if err != nil {
60 logger.Fatalf("failed to initialize access logger: %v", err)
61 }
62
63 // initialize storage
64 esmStorage, err := storage.New(&config.Storage)
65 if err != nil {
66 logger.Fatalf("failed to initialize storage(%s): %v", config.Storage.Type, err)
67 }
68 logger.Debugf("storage initialized, type: %s, endpoint: %s", config.Storage.Type, config.Storage.Endpoint)
69
70 // load node runtime in background
71 go getNodeRuntimeJS("fs")
72
73 // add middlewares
74 rex.Use(
75 pprofRouter(),
76 cors(config.CorsAllowOrigins),
77 rex.Header("Server", "esm.sh"),
78 rex.Logger(logger),
79 rex.Optional(rex.AccessLogger(accessLogger), config.AccessLog),
80 rex.Optional(rex.Compress(), config.Compress),

Callers 1

mainFunction · 0.92

Calls 11

NewFunction · 0.92
existsFileFunction · 0.85
LoadConfigFunction · 0.85
getNodeRuntimeJSFunction · 0.85
corsFunction · 0.85
customLandingPageFunction · 0.85
esmLegacyRouterFunction · 0.85
esmRouterFunction · 0.85
ErrorMethod · 0.80
HeaderMethod · 0.80
pprofRouterFunction · 0.70

Tested by

no test coverage detected