-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0fb7f81
Showing
5 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
SHELL = /bin/bash | ||
.SILENT: # comment this one out if you need to debug the Makefile | ||
APP=picsort | ||
|
||
.PHONY: run | ||
run: | ||
go run . ~/Downloads/tmp/pictures/ | ||
|
||
.PHONY: build | ||
build: | ||
go build | ||
# go build -o ${APP} main.go | ||
|
||
.PHONY: test | ||
test: | ||
go test ./... | ||
|
||
.PHONY: watch-test | ||
watch-test: | ||
fswatch \ | ||
--exclude '.git' \ | ||
-o . | \ | ||
xargs -n1 -I{} go test ./... | ||
|
||
.PHONY: clean | ||
clean: | ||
go clean | ||
|
||
|
||
.PHONY: watch-run | ||
watch-run: | ||
fswatch \ | ||
--exclude '.git' \ | ||
-xn . | \ | ||
while read file event; do \ | ||
echo "File $$file has changed, Event: $$event"; \ | ||
$(MAKE) run ; \ | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>PicSort</title> | ||
<link rel="stylesheet" href="/assets/style.css"> | ||
</head> | ||
<body> | ||
<h1>Images</h1> | ||
<ul> | ||
{{range .}} | ||
<li><img src="/static/{{.}}" alt="{{.}}"></li> | ||
{{end}} | ||
</ul> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
body { | ||
font-family: Arial, sans-serif; | ||
margin: 0; | ||
padding: 0; | ||
background-color: #f4f4f4; | ||
} | ||
|
||
h1 { | ||
text-align: center; | ||
margin: 20px; | ||
} | ||
|
||
ul { | ||
list-style-type: none; | ||
display: flex; | ||
flex-wrap: wrap; | ||
justify-content: center; | ||
padding: 0; | ||
} | ||
|
||
li { | ||
margin: 10px; | ||
} | ||
|
||
img { | ||
display: block; | ||
max-width: 200px; | ||
max-height: 200px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/viktomas/picsort | ||
|
||
go 1.20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package main | ||
|
||
import ( | ||
_ "embed" | ||
"fmt" | ||
"html/template" | ||
"io" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
//go:embed assets/index.html | ||
var indexTemplate string | ||
|
||
//go:embed assets/style.css | ||
var css string | ||
|
||
func main() { | ||
if len(os.Args) != 2 { | ||
fmt.Println("Usage: picsort <folder>") | ||
return | ||
} | ||
|
||
rootFolder := os.Args[1] | ||
|
||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
tmpl := template.Must(template.New("index").Parse(indexTemplate)) | ||
imageFiles := getImageFiles(rootFolder) | ||
|
||
err := tmpl.Execute(w, imageFiles) | ||
if err != nil { | ||
http.Error(w, err.Error(), 500) | ||
} | ||
}) | ||
|
||
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(rootFolder)))) | ||
|
||
http.HandleFunc("/assets/style.css", func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Add("Content-Type", "text/css") | ||
io.WriteString(w, css) | ||
}) | ||
|
||
addr := "localhost:8080" | ||
fmt.Printf("Starting server at http://%s\n", addr) | ||
err := http.ListenAndServe(addr, nil) | ||
if err != nil { | ||
fmt.Printf("Error: %s\n", err) | ||
} | ||
} | ||
|
||
func getImageFiles(folderPath string) []string { | ||
var imageFiles []string | ||
_ = filepath.Walk(folderPath, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
if !info.IsDir() && isImageFile(path) { | ||
imageFiles = append(imageFiles, filepath.Base(path)) | ||
} | ||
return nil | ||
}) | ||
return imageFiles | ||
} | ||
|
||
func isImageFile(filename string) bool { | ||
ext := filepath.Ext(filename) | ||
return ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".gif" | ||
} |