oktopus/backend/services/utils/file-server/main.go

51 lines
1.0 KiB
Go

package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/joho/godotenv"
)
func main() {
err := godotenv.Load()
localEnv := ".env.local"
if _, err := os.Stat(localEnv); err == nil {
_ = godotenv.Overload(localEnv)
log.Println("Loaded variables from '.env.local'")
} else {
log.Println("Loaded variables from '.env'")
}
if err != nil {
log.Println("Error to load environment variables:", err)
}
directoryPath := os.Getenv("DIRECTORY_PATH")
// Check if the directory exists
_, err = os.Stat(directoryPath)
if os.IsNotExist(err) {
fmt.Printf("Directory '%s' not found.\n", directoryPath)
return
}
// Create a file server handler to serve the directory's contents
fileServer := http.FileServer(http.Dir(directoryPath))
// Create a new HTTP server and handle requests
http.Handle("/", fileServer)
port := os.Getenv("SERVER_PORT")
fmt.Printf("Server started at http://localhost%s\n", port)
err = http.ListenAndServe(port, nil)
if err != nil {
fmt.Printf("Error starting server: %s\n", err)
}
}