Start starts the esm.sh server
()
| 21 | |
| 22 | // Start starts the esm.sh server |
| 23 | func 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), |
no test coverage detected