MCPcopy Create free account
hub / github.com/OpenListTeam/OpenList / initIndex

Function initIndex

server/static/static.go:59–107  ·  view source on GitHub ↗
(siteConfig SiteConfig)

Source from the content-addressed store, hash-verified

57}
58
59func initIndex(siteConfig SiteConfig) {
60 utils.Log.Debug("Initializing index.html...")
61 // dist_dir is empty and cdn is not empty, and web_version is empty or beta or dev or rolling
62 if conf.Conf.DistDir == "" && conf.Conf.Cdn != "" && (conf.WebVersion == "" || conf.WebVersion == "beta" || conf.WebVersion == "dev" || conf.WebVersion == "rolling") {
63 utils.Log.Infof("Fetching index.html from CDN: %s/index.html...", siteConfig.Cdn)
64 resp, err := base.RestyClient.R().
65 SetHeader("Accept", "text/html").
66 Get(fmt.Sprintf("%s/index.html", siteConfig.Cdn))
67 if err != nil {
68 utils.Log.Fatalf("failed to fetch index.html from CDN: %v", err)
69 }
70 if resp.StatusCode() != http.StatusOK {
71 utils.Log.Fatalf("failed to fetch index.html from CDN, status code: %d", resp.StatusCode())
72 }
73 conf.RawIndexHtml = string(resp.Body())
74 utils.Log.Info("Successfully fetched index.html from CDN")
75 } else {
76 utils.Log.Debug("Reading index.html from static files system...")
77 indexFile, err := static.Open("index.html")
78 if err != nil {
79 if errors.Is(err, fs.ErrNotExist) {
80 utils.Log.Fatalf("index.html not exist, you may forget to put dist of frontend to public/dist")
81 }
82 utils.Log.Fatalf("failed to read index.html: %v", err)
83 }
84 defer func() {
85 _ = indexFile.Close()
86 }()
87 index, err := io.ReadAll(indexFile)
88 if err != nil {
89 utils.Log.Fatalf("failed to read dist/index.html")
90 }
91 conf.RawIndexHtml = string(index)
92 utils.Log.Debug("Successfully read index.html from static files system")
93 }
94 utils.Log.Debug("Replacing placeholders in index.html...")
95 // Construct the correct manifest path based on basePath
96 manifestPath := "/manifest.json"
97 if siteConfig.BasePath != "/" {
98 manifestPath = siteConfig.BasePath + "/manifest.json"
99 }
100 replaceMap := map[string]string{
101 "cdn: undefined": fmt.Sprintf("cdn: '%s'", siteConfig.Cdn),
102 "base_path: undefined": fmt.Sprintf("base_path: '%s'", siteConfig.BasePath),
103 `href="/manifest.json"`: fmt.Sprintf(`href="%s"`, manifestPath),
104 }
105 conf.RawIndexHtml = replaceStrings(conf.RawIndexHtml, replaceMap)
106 UpdateIndex()
107}
108
109func UpdateIndex() {
110 utils.Log.Debug("Updating index.html with settings...")

Callers 1

StaticFunction · 0.85

Calls 6

replaceStringsFunction · 0.85
SetHeaderMethod · 0.80
UpdateIndexFunction · 0.70
GetMethod · 0.65
OpenMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected