basic configuration options

This commit is contained in:
NotAShelf 2023-06-03 19:53:48 +03:00
parent d57a858096
commit ba9236a500
No known key found for this signature in database
GPG key ID: F0D14CCB5ED5AA22

44
main.go
View file

@ -1,8 +1,7 @@
// main.go
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"io" "io"
"log" "log"
@ -11,18 +10,39 @@ import (
"path/filepath" "path/filepath"
) )
const ( type Config struct {
uploadPath = "uploads" // Directory to store uploaded files Username string `json:"username"`
username = "admin" // Username for authentication Password string `json:"password"`
password = "password" // Password for authentication ServicePort string `json:"service_port"`
UploadsDir string `json:"uploads_dir"`
}
var (
config Config
) )
func main() { func main() {
loadConfig("config.json")
http.HandleFunc("/", serveCDN) http.HandleFunc("/", serveCDN)
http.HandleFunc("/upload", handleUpload) http.HandleFunc("/upload", handleUpload)
log.Println("Starting CDN server on port 8080...") log.Printf("Starting CDN server on port %s...\n", config.ServicePort)
log.Fatal(http.ListenAndServe(":8080", nil)) 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)
}
} }
func serveCDN(w http.ResponseWriter, r *http.Request) { func serveCDN(w http.ResponseWriter, r *http.Request) {
@ -39,7 +59,7 @@ func serveCDN(w http.ResponseWriter, r *http.Request) {
} }
// Get the requested file path // Get the requested file path
filePath := filepath.Join(uploadPath, r.URL.Path) filePath := filepath.Join(config.UploadsDir, r.URL.Path)
// Check if the file exists // Check if the file exists
_, err := os.Stat(filePath) _, err := os.Stat(filePath)
@ -82,7 +102,7 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
defer file.Close() defer file.Close()
// Create the uploads directory if it doesn't exist // Create the uploads directory if it doesn't exist
err = os.MkdirAll(uploadPath, os.ModePerm) err = os.MkdirAll(config.UploadsDir, os.ModePerm)
if err != nil { if err != nil {
log.Println("Internal Server Error") log.Println("Internal Server Error")
http.Error(w, "Internal Server Error", http.StatusInternalServerError) http.Error(w, "Internal Server Error", http.StatusInternalServerError)
@ -90,7 +110,7 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
} }
// Create a new file in the uploads directory // Create a new file in the uploads directory
dst, err := os.Create(filepath.Join(uploadPath, header.Filename)) dst, err := os.Create(filepath.Join(config.UploadsDir, header.Filename))
if err != nil { if err != nil {
log.Println("Internal Server Error") log.Println("Internal Server Error")
http.Error(w, "Internal Server Error", http.StatusInternalServerError) http.Error(w, "Internal Server Error", http.StatusInternalServerError)
@ -112,5 +132,5 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
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 == username && password == password return ok && username == config.Username && password == config.Password
} }