mirror of
https://github.com/NotAShelf/go-cdn.git
synced 2024-11-26 09:26:46 +00:00
try to get path relative to main executable
This commit is contained in:
parent
0031af0057
commit
9a5d191c46
1 changed files with 50 additions and 5 deletions
55
main.go
55
main.go
|
@ -8,6 +8,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/go-chi/chi"
|
"github.com/go-chi/chi"
|
||||||
|
@ -56,6 +57,12 @@ func main() {
|
||||||
address := fmt.Sprintf(":%s", config.ServicePort)
|
address := fmt.Sprintf(":%s", config.ServicePort)
|
||||||
logger.Infof("Starting CDN server on port %s...", config.ServicePort)
|
logger.Infof("Starting CDN server on port %s...", config.ServicePort)
|
||||||
logger.Infof("Serving files from %s", config.UploadsDir)
|
logger.Infof("Serving files from %s", config.UploadsDir)
|
||||||
|
|
||||||
|
// Print upload path and list files
|
||||||
|
uploadPath := filepath.Join(".", config.UploadsDir)
|
||||||
|
logger.Infof("Upload path: %s", uploadPath)
|
||||||
|
listFiles(uploadPath)
|
||||||
|
|
||||||
err = http.ListenAndServe(address, router)
|
err = http.ListenAndServe(address, router)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatalf("Server error: %s", err)
|
logger.Fatalf("Server error: %s", err)
|
||||||
|
@ -63,18 +70,38 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadConfig(filename string) error {
|
func loadConfig(filename string) error {
|
||||||
file, err := os.Open(filename)
|
// Get the absolute path of the main.go file
|
||||||
|
_, currentFile, _, _ := runtime.Caller(1)
|
||||||
|
currentDir := filepath.Dir(currentFile)
|
||||||
|
|
||||||
|
// Construct the absolute path for the config file
|
||||||
|
configPath := filepath.Join(currentDir, filename)
|
||||||
|
|
||||||
|
// Open the config file
|
||||||
|
file, err := os.Open(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error opening config file: %s", err)
|
return fmt.Errorf("error opening config file: %s", err)
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
|
// Decode the config file into the config variable
|
||||||
decoder := json.NewDecoder(file)
|
decoder := json.NewDecoder(file)
|
||||||
err = decoder.Decode(&config)
|
err = decoder.Decode(&config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error decoding config file: %s", err)
|
return fmt.Errorf("error decoding config file: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set the relative path for the uploads directory
|
||||||
|
config.UploadsDir = filepath.Join(currentDir, filepath.FromSlash(config.UploadsDir))
|
||||||
|
|
||||||
|
// Create the uploads directory if it doesn't exist
|
||||||
|
if _, err := os.Stat(config.UploadsDir); os.IsNotExist(err) {
|
||||||
|
err := os.MkdirAll(config.UploadsDir, os.ModePerm)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error creating uploads directory: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,7 +119,7 @@ func serveCDN(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the requested file path
|
// Get the requested file path
|
||||||
filePath := filepath.Join(config.UploadsDir, r.URL.Path)
|
filePath := filepath.Join(config.UploadsDir, filepath.Clean(r.URL.Path))
|
||||||
|
|
||||||
// Check if the file exists
|
// Check if the file exists
|
||||||
_, err := os.Stat(filePath)
|
_, err := os.Stat(filePath)
|
||||||
|
@ -152,8 +179,8 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
|
||||||
// Create the uploads directory if it doesn't exist
|
// Create the uploads directory if it doesn't exist
|
||||||
err = os.MkdirAll(config.UploadsDir, os.ModePerm)
|
err = os.MkdirAll(config.UploadsDir, os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Internal Server Error:", err)
|
logger.Error("Error creating uploads directory:", err)
|
||||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
http.Error(w, "Error creating uploads directory", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -177,7 +204,6 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
|
||||||
logger.Infof("File uploaded successfully: %s", filename)
|
logger.Infof("File uploaded successfully: %s", filename)
|
||||||
fmt.Fprintf(w, "File uploaded successfully!")
|
fmt.Fprintf(w, "File uploaded successfully!")
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkAuthentication(r *http.Request) bool {
|
func checkAuthentication(r *http.Request) bool {
|
||||||
username, password, ok := r.BasicAuth()
|
username, password, ok := r.BasicAuth()
|
||||||
return ok && username == config.Username && password == config.Password
|
return ok && username == config.Username && password == config.Password
|
||||||
|
@ -190,3 +216,22 @@ func sanitizeFilename(filename string) string {
|
||||||
func isValidFilename(filename string) bool {
|
func isValidFilename(filename string) bool {
|
||||||
return filenameRegex.MatchString(filename)
|
return filenameRegex.MatchString(filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func listFiles(dirPath string) {
|
||||||
|
logger.Infof("Files in %s:", dirPath)
|
||||||
|
|
||||||
|
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("Error accessing file: %s", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if !info.IsDir() {
|
||||||
|
logger.Infof("- %s", path)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("Error listing files: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue