~hokiegeek/bildsortierer

80b79543d4fc03578b5aefcae35ba2c3887d0afd — HokieGeek 8 months ago c7a30a2 master
Taking a stab at this
1 files changed, 108 insertions(+), 0 deletions(-)

A main.go
A main.go => main.go +108 -0
@@ 0,0 1,108 @@
package main

import (
	// "bytes"
	"flag"
	"fmt"
	"html/template"
	"io/ioutil"
	"log"
	"net/http"

	"github.com/gorilla/mux"
)

const dirListing = `{{range .}}{{if .IsDir}}D {{else}}F {{end}}{{.Name}}<br />{{end}}`

const tpl = `
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>TODO</title>
	</head>
	<body>
		{{range .}}{{if .IsDir}}D {{else}}F {{end}}{{.Name}}<br />{{end}}
	</body>
</html>`

func dirList(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	dir := vars["dir"]

	files, err := ioutil.ReadDir("/" + dir)
	if err != nil {
		log.Println(err)
	}

	tmpl, err := template.New("dls").Parse(tpl)
	if err != nil {
		log.Println(err)
	}

	//buf := new(bytes.Buffer)
	err = tmpl.Execute(w, files)
	if err != nil {
		log.Println(err)
	}

	/*
		for _, f := range files {
			fmt.Println(f.Name())
		}

		fmt.Fprintf(w, buf.String())
	*/
}

func galleryView(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	dir := vars["dir"]

	files, err := ioutil.ReadDir("/" + dir)
	if err != nil {
		log.Println(err)
	}

	tmpl, err := template.New("dls").Parse(tpl)
	if err != nil {
		log.Println(err)
	}

	//buf := new(bytes.Buffer)
	err = tmpl.Execute(w, files)
	if err != nil {
		log.Println(err)
	}

	/*
		for _, f := range files {
			fmt.Println(f.Name())
		}

		fmt.Fprintf(w, buf.String())
	*/
}

func home(w http.ResponseWriter, r *http.Request) {
	// http.Redirect(w, r, "list", 301)
	http.Redirect(w, r, "gallery", 301)
}

func main() {
	portPtr := flag.Int("port", 8080, "Specify the port to use")
	// defaultDirPtr := flag.String("dir", "/", "Specify the directory to start with")
	flag.Parse()

	log.Printf("Port: %d\n", *portPtr)

	r := mux.NewRouter()

	r.HandleFunc("/gallery", galleryView).Methods("GET")
	r.HandleFunc("/gallery/{dir}", galleryView).Methods("GET")
	r.HandleFunc("/list", dirList).Methods("GET")
	r.HandleFunc("/list/{dir}", dirList).Methods("GET")
	r.HandleFunc("/", home).Methods("GET")

	http.ListenAndServe(fmt.Sprintf(":%d", *portPtr), r)
}