go-cdn/main.go

137 lines
3.4 KiB
Go
Raw Normal View History

2023-06-03 15:55:38 +00:00
package main
import (
2023-06-03 16:53:48 +00:00
"encoding/json"
2023-06-03 15:55:38 +00:00
"fmt"
2023-06-03 16:20:16 +00:00
"io"
2023-06-03 15:55:38 +00:00
"log"
"net/http"
"os"
"path/filepath"
)
2023-06-03 16:53:48 +00:00
type Config struct {
Username string `json:"username"`
Password string `json:"password"`
ServicePort string `json:"service_port"`
UploadsDir string `json:"uploads_dir"`
}
var (
config Config
2023-06-03 15:55:38 +00:00
)
func main() {
2023-06-03 16:53:48 +00:00
loadConfig("config.json")
2023-06-03 15:55:38 +00:00
http.HandleFunc("/", serveCDN)
http.HandleFunc("/upload", handleUpload)
2023-06-03 16:53:48 +00:00
log.Printf("Starting CDN server on port %s...\n", config.ServicePort)
log.Fatal(http.ListenAndServe(":"+config.ServicePort, nil))
}
func loadConfig(filename string) {
file, err := os.Open(filename)
if err != nil {
log.Fatal("Error opening config file:", err)
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(&config)
if err != nil {
log.Fatal("Error decoding config file:", err)
}
2023-06-03 15:55:38 +00:00
}
func serveCDN(w http.ResponseWriter, r *http.Request) {
// Log the request information
log.Printf("Received request: %s %s", r.Method, r.URL.Path)
// Check if the request has valid authentication
if !checkAuthentication(r) {
log.Println("Authentication failed")
w.Header().Set("WWW-Authenticate", `Basic realm="CDN Authentication"`)
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "401 Unauthorized\n")
return
}
// Get the requested file path
2023-06-03 16:53:48 +00:00
filePath := filepath.Join(config.UploadsDir, r.URL.Path)
2023-06-03 15:55:38 +00:00
// Check if the file exists
_, err := os.Stat(filePath)
if err != nil {
if os.IsNotExist(err) {
log.Printf("File not found: %s", r.URL.Path)
http.NotFound(w, r)
} else {
log.Println("Internal Server Error")
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
return
}
// Serve the file
log.Printf("Serving file: %s", r.URL.Path)
http.ServeFile(w, r, filePath)
}
func handleUpload(w http.ResponseWriter, r *http.Request) {
// Log the request information
log.Printf("Received upload request: %s %s", r.Method, r.URL.Path)
// Check if the request has valid authentication
if !checkAuthentication(r) {
log.Println("Authentication failed")
w.Header().Set("WWW-Authenticate", `Basic realm="CDN Authentication"`)
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "401 Unauthorized\n")
return
}
// Parse the uploaded file
file, header, err := r.FormFile("file")
if err != nil {
log.Println("Bad Request")
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
defer file.Close()
// Create the uploads directory if it doesn't exist
2023-06-03 16:53:48 +00:00
err = os.MkdirAll(config.UploadsDir, os.ModePerm)
2023-06-03 15:55:38 +00:00
if err != nil {
log.Println("Internal Server Error")
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
// Create a new file in the uploads directory
2023-06-03 16:53:48 +00:00
dst, err := os.Create(filepath.Join(config.UploadsDir, header.Filename))
2023-06-03 15:55:38 +00:00
if err != nil {
log.Println("Internal Server Error")
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
defer dst.Close()
// Copy the uploaded file to the destination
_, err = io.Copy(dst, file)
if err != nil {
log.Println("Internal Server Error")
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
log.Printf("File uploaded successfully: %s", header.Filename)
fmt.Fprintf(w, "File uploaded successfully!")
}
func checkAuthentication(r *http.Request) bool {
username, password, ok := r.BasicAuth()
2023-06-03 16:53:48 +00:00
return ok && username == config.Username && password == config.Password
2023-06-03 15:55:38 +00:00
}