-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
95 lines (83 loc) · 2.17 KB
/
main.go
File metadata and controls
95 lines (83 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
)
type Page struct {
Title string
Body []byte
Files []string
Dirs []string
CurrDir string
}
func serveImage(w http.ResponseWriter, r *http.Request) {
filename := r.URL.Path[len("/image"):]
//body := r.FormValue("fn")
log.Printf("Filename: %s", filename)
file, err := os.Open(filename)
if err != nil {
log.Printf("Error: %s", err)
}
file.Close()
//image, err := ioutil.ReadFile(filename)
//if err != nil {
//fmt.Fprintf(w, "<h1>%s</h1>", err)
//}
http.ServeFile(w, r, filename)
}
func loadPage(title, directory string) (*Page, error) {
searchDir := directory //"/home/syn/Dropbox/Dev/2017/ImageLight"
//searchDir := "/home/syn/images/test11"
fileList := []string{}
dirList := []string{}
dirList = append(dirList, filepath.Dir(directory))
err := filepath.Walk(searchDir, func(path string, f os.FileInfo, err error) error {
ext := filepath.Ext(path)
if f.IsDir() {
dirList = append(dirList, path)
//log.Printf("Directory: %s", path)
//log.Printf("Dirdir: %s", filepath.Dir(path))
}
if ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" || ext == ".mp4" {
fileList = append(fileList, path)
}
return nil
})
if err != nil {
fmt.Println(err)
}
filename := title + ".html"
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return &Page{Title: title, Body: body, Files: fileList, Dirs: dirList}, nil
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
directory := r.URL.Path
log.Printf(directory)
p, err := loadPage("index", directory)
if err != nil {
p = &Page{Title: "index"}
}
t, _ := template.ParseFiles("index.html")
t.Execute(w, p)
}
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello %s!", r.URL.Path[1:])
}
// http://www.alexedwards.net/blog/serving-static-sites-with-go
func main() {
http.HandleFunc("/", indexHandler)
http.HandleFunc("/image/", serveImage)
fs := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
//http.HandleFunc()
log.Println("Listening...")
http.ListenAndServe(":8080", nil)
}