diff main.go @ 1:3e7247db5c6e

show index.html
author Atarwn Gard <a@qwa.su>
date Mon, 09 Mar 2026 01:04:16 +0500
parents 48bdab3eec8a
children d19133be91ba
line wrap: on
line diff
--- a/main.go	Mon Mar 09 00:37:49 2026 +0500
+++ b/main.go	Mon Mar 09 01:04:16 2026 +0500
@@ -36,7 +36,6 @@
 		log.Fatalf("d2o: config error: %v", err)
 	}
 
-	// Apply @d2o global settings
 	for _, d := range cfg.Abstract("d2o") {
 		switch d.Key {
 		case "threads":
@@ -94,7 +93,6 @@
 	return http.Serve(ln, h)
 }
 
-// collectPorts scans all blocks for port / port+tls directives.
 func collectPorts(cfg *icf.Config) []portConfig {
 	seen := make(map[string]bool)
 	var out []portConfig
@@ -102,7 +100,6 @@
 	for _, b := range cfg.Blocks {
 		dirs := cfg.ResolveBlock(b, nil)
 
-		// Collect tls paths defined in this block
 		var cert, key string
 		for _, d := range dirs {
 			if d.Key == "tls" {
@@ -149,7 +146,6 @@
 	host := stripPort(r.Host)
 	reqPath := path.Clean(r.URL.Path)
 
-	// Try host+path first, then host alone
 	dirs, caps := h.cfg.Match(host + reqPath)
 	if dirs == nil {
 		dirs, caps = h.cfg.Match(host)
@@ -164,11 +160,11 @@
 
 func (h *handler) serve(w http.ResponseWriter, r *http.Request, dirs []icf.Directive, _ map[string]string) {
 	var (
-		rootDir  string
-		rootShow bool
-		fcgiAddr string
-		fcgiPat  string
-		rprxAddr string
+		rootDir   string
+		rootIndex []string
+		fcgiAddr  string
+		fcgiPat   string
+		rprxAddr  string
 	)
 
 	for _, d := range dirs {
@@ -177,9 +173,13 @@
 			rootDir = safeArg(d.Args, 0)
 			switch safeArg(d.Args, 1) {
 			case "show":
-				rootShow = true
+				if len(d.Args) >= 3 {
+					rootIndex = d.Args[2:]
+				} else {
+					rootIndex = []string{"index.html"}
+				}
 			case "hide", "":
-				rootShow = false
+				rootIndex = nil
 			default:
 				log.Printf("d2o: root: unknown mode %q (want show|hide)", safeArg(d.Args, 1))
 			}
@@ -194,7 +194,6 @@
 		}
 	}
 
-	// Priority: rprx > fcgi > static root
 	if rprxAddr != "" {
 		serveReverseProxy(w, r, rprxAddr)
 		return
@@ -207,7 +206,7 @@
 		return
 	}
 	if rootDir != "" {
-		serveStatic(w, r, rootDir, rootShow)
+		serveStatic(w, r, rootDir, rootIndex)
 		return
 	}
 
@@ -216,7 +215,7 @@
 
 // --- Static -----------------------------------------------------------------
 
-func serveStatic(w http.ResponseWriter, r *http.Request, rootDir string, showDir bool) {
+func serveStatic(w http.ResponseWriter, r *http.Request, rootDir string, rootIndex []string) {
 	fpath := filepath.Join(rootDir, filepath.FromSlash(path.Clean(r.URL.Path)))
 
 	info, err := os.Stat(fpath)
@@ -228,14 +227,23 @@
 		http.Error(w, "internal error", http.StatusInternalServerError)
 		return
 	}
+
 	if info.IsDir() {
-		if !showDir {
+		if rootIndex == nil {
 			http.Error(w, "forbidden", http.StatusForbidden)
 			return
 		}
+		for _, idx := range rootIndex {
+			idxPath := filepath.Join(fpath, idx)
+			if _, err := os.Stat(idxPath); err == nil {
+				http.ServeFile(w, r, idxPath)
+				return
+			}
+		}
 		listDir(w, r, fpath, r.URL.Path)
 		return
 	}
+
 	http.ServeFile(w, r, fpath)
 }