Loakel Config Datei (config.json) eingebaut und die Parameter der Datei genutzt
This commit is contained in:
		
							
								
								
									
										5
									
								
								config.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								config.json
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,5 @@
 | 
				
			|||||||
 | 
					{
 | 
				
			||||||
 | 
					    "Host":"http://127.0.0.1",
 | 
				
			||||||
 | 
					    "Port":"8000",
 | 
				
			||||||
 | 
					    "IMGPath":"C:\\Fotos"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										44
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										44
									
								
								main.go
									
									
									
									
									
								
							@@ -17,19 +17,29 @@ import (
 | 
				
			|||||||
	"github.com/gorilla/mux"
 | 
						"github.com/gorilla/mux"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var imgPath = "C:/Fotos"
 | 
					var config = readConfig("")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Configuration struct {
 | 
				
			||||||
 | 
						Host    string
 | 
				
			||||||
 | 
						Port    string
 | 
				
			||||||
 | 
						IMGPath string
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// our main function
 | 
					// our main function
 | 
				
			||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						fmt.Println("Host: " + config.Host)
 | 
				
			||||||
 | 
						fmt.Println("Post: " + config.Port)
 | 
				
			||||||
 | 
						fmt.Println("ImgPath: " + config.IMGPath)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	router := mux.NewRouter()
 | 
						router := mux.NewRouter()
 | 
				
			||||||
	router.HandleFunc("/folder/{foldername:.*}", getFolderContent).Methods("GET")
 | 
						router.HandleFunc("/folder/{foldername:.*}", getFolderContent).Methods("GET")
 | 
				
			||||||
	router.HandleFunc("/qr/{qrlink:.*}", getQRCode).Methods("GET")
 | 
						router.HandleFunc("/qr/{qrlink:.*}", getQRCode).Methods("GET")
 | 
				
			||||||
	router.HandleFunc("/thumb/{imgname:.*}", getThumbNail).Methods("GET")
 | 
						router.HandleFunc("/thumb/{imgname:.*}", getThumbNail).Methods("GET")
 | 
				
			||||||
	router.HandleFunc("/imgdl/{imgname:.*}", getImageDownload).Methods("GET")
 | 
						router.HandleFunc("/imgdl/{imgname:.*}", getImageDownload).Methods("GET")
 | 
				
			||||||
	router.PathPrefix("/img/").Handler(http.StripPrefix("/img/", http.FileServer(http.Dir(imgPath)))).Methods("GET")
 | 
						router.PathPrefix("/img/").Handler(http.StripPrefix("/img/", http.FileServer(http.Dir(config.IMGPath)))).Methods("GET")
 | 
				
			||||||
	router.PathPrefix("/").Handler(http.FileServer(http.Dir("web"))).Methods("GET")
 | 
						router.PathPrefix("/").Handler(http.FileServer(http.Dir("web"))).Methods("GET")
 | 
				
			||||||
	log.Fatal(http.ListenAndServe(":8000", router))
 | 
						log.Fatal(http.ListenAndServe(":"+config.Port, router))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -37,8 +47,8 @@ func getFolderContent(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	params := mux.Vars(r)
 | 
						params := mux.Vars(r)
 | 
				
			||||||
	pFolder := params["foldername"]
 | 
						pFolder := params["foldername"]
 | 
				
			||||||
	fmt.Println(path.Join(imgPath, pFolder))
 | 
						fmt.Println(path.Join(config.IMGPath, pFolder))
 | 
				
			||||||
	files, err := ioutil.ReadDir(path.Join(imgPath, pFolder))
 | 
						files, err := ioutil.ReadDir(path.Join(config.IMGPath, pFolder))
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Fatal(err)
 | 
							log.Fatal(err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -80,9 +90,9 @@ func getThumbNail(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
	pIMGName := params["imgname"]
 | 
						pIMGName := params["imgname"]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Das Thumbnail soll nur generiert werden wenn es noch nicht existiert
 | 
						// Das Thumbnail soll nur generiert werden wenn es noch nicht existiert
 | 
				
			||||||
	if !fileExists(imgPath + "/" + pIMGName + ".thumb.jpg") {
 | 
						if !fileExists(config.IMGPath + "/" + pIMGName + ".thumb.jpg") {
 | 
				
			||||||
		//  Originaldatei öffnen, welche angefragt wird
 | 
							//  Originaldatei öffnen, welche angefragt wird
 | 
				
			||||||
		src, err := imaging.Open(imgPath + "/" + pIMGName)
 | 
							src, err := imaging.Open(config.IMGPath + "/" + pIMGName)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			log.Fatalf("failed to open image: %v", err)
 | 
								log.Fatalf("failed to open image: %v", err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
@@ -90,7 +100,7 @@ func getThumbNail(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
		// Foto auf 200px skalieren
 | 
							// Foto auf 200px skalieren
 | 
				
			||||||
		src = imaging.Resize(src, 200, 0, imaging.NearestNeighbor)
 | 
							src = imaging.Resize(src, 200, 0, imaging.NearestNeighbor)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		err = imaging.Save(src, imgPath+"/"+pIMGName+".thumb.jpg")
 | 
							err = imaging.Save(src, config.IMGPath+"/"+pIMGName+".thumb.jpg")
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			log.Fatalf("failed to save image: %v", err)
 | 
								log.Fatalf("failed to save image: %v", err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
@@ -99,7 +109,7 @@ func getThumbNail(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
		png.Encode(w, src)
 | 
							png.Encode(w, src)
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		src, err := imaging.Open(imgPath + "/" + pIMGName + ".thumb.jpg")
 | 
							src, err := imaging.Open(config.IMGPath + "/" + pIMGName + ".thumb.jpg")
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			log.Fatalf("failed to open image: %v", err)
 | 
								log.Fatalf("failed to open image: %v", err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
@@ -116,7 +126,7 @@ func getImageDownload(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
	pIMGName := params["imgname"]
 | 
						pIMGName := params["imgname"]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	//  Originaldatei öffnen, welche angefragt wird
 | 
						//  Originaldatei öffnen, welche angefragt wird
 | 
				
			||||||
	src, err := imaging.Open(imgPath + "/" + pIMGName)
 | 
						src, err := imaging.Open(config.IMGPath + "/" + pIMGName)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Fatalf("failed to open image: %v", err)
 | 
							log.Fatalf("failed to open image: %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -138,3 +148,17 @@ func fileExists(filename string) bool {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	return !info.IsDir()
 | 
						return !info.IsDir()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func readConfig(filename string) *Configuration {
 | 
				
			||||||
 | 
						// initialize conf with default values.
 | 
				
			||||||
 | 
						conf := &Configuration{Host: "http://127.0.0.1", Port: "8000", IMGPath: "C:\\Fotos"}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						b, err := ioutil.ReadFile("./config.json")
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return conf
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if err = json.Unmarshal(b, conf); err != nil {
 | 
				
			||||||
 | 
							return conf
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return conf
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user