Echo/main.go
2023-12-01 20:43:45 +03:00

44 lines
826 B
Go

package main
import (
"fmt"
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"github.com/joho/godotenv"
)
func main() {
err := godotenv.Load()
if err != nil {
fmt.Println("Error loading .env file")
}
serverPort := os.Getenv("SERVER_PORT")
if serverPort == "" {
serverPort = "8080"
}
basePath := os.Getenv("BASE_PATH")
if basePath == "" {
basePath = "."
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if regexp.MustCompile(`\.\.\/?`).MatchString(r.URL.Path) {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
filePath := filepath.Join(basePath, r.URL.Path)
http.ServeFile(w, r, filePath)
})
port, _ := strconv.Atoi(serverPort)
fmt.Printf("Echo started with base path %s on port %d\n", basePath, port)
http.ListenAndServe(":"+serverPort, nil)
}