Sfoglia il codice sorgente

add new file go/l16.go for serve html file from env path

hhu67 4 settimane fa
parent
commit
f902723e2c
2 ha cambiato i file con 30 aggiunte e 0 eliminazioni
  1. 30 0
      go/l16.go
  2. BIN
      go/output/l16

+ 30 - 0
go/l16.go

@@ -0,0 +1,30 @@
+package main
+
+import (
+	"fmt"
+	"os"
+	"net/http"
+	"log"
+	"github.com/joho/godotenv"
+)
+
+func handler(w http.ResponseWriter, r *http.Request) {
+	err := godotenv.Load()
+	if err != nil {
+		log.Fatalf("%v\n", err)
+	}
+	PathFile := os.Getenv("html_file_for_l16.go") // Enter the file path for the html_file_for_l16.go variable into the env file
+	html, err := os.ReadFile(PathFile)
+	if err != nil {
+		log.Fatalf("%v\n", err)
+	}
+	fmt.Fprintf(w, string(html))
+}
+
+func main() {
+	http.HandleFunc("/", handler)
+	err := http.ListenAndServe(":2345", nil)
+	if err != nil {
+		log.Fatalf("%v\n", err)
+	}
+}

BIN
go/output/l16