Skip to content

Commit a51cf5b

Browse files
committed
parsing HTML directly from string instead of index.html,
resizing QRCode, added new sample gifs, created github release for distributing the binary
1 parent 7adb73c commit a51cf5b

File tree

12 files changed

+181
-399
lines changed

12 files changed

+181
-399
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,8 @@ _testmain.go
2929

3030
# external packages folder
3131
vendor/
32+
33+
MD5SUMS
34+
SHA512SUMS
35+
*.tar.gz
36+
fs-server/fs-server

README.md

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
# go-fileserver
2-
> A simple HTTP server to share files over same wifi via QRCode
2+
> A simple HTTP file server to share files over WiFi via QRCode
33
44
# Installation
5-
```
6-
git clone https://github.com/prdpx7/go-fileserver
7-
cd go-fileserver/fs-server
8-
go build
9-
# now just run the binary
10-
./fs-server
11-
```
5+
* You can download compressed version from [releases](https://github.com/prdpx7/go-fileserver/releases)
6+
```
7+
wget https://github.com/prdpx7/go-fileserver/releases/download/v0.1/fs-server-2020.07.25.tar.gz
8+
tar -xzf fs-server-2020.07.25.tar.gz
9+
chmod +x fs-server && sudo cp fs-server /usr/local/bin/fs-server
10+
```
11+
* Or download the binary directly
12+
```
13+
wget https://github.com/prdpx7/go-fileserver/releases/download/v0.1/fs-server
14+
chmod +x fs-server && sudo cp fs-server /usr/local/bin/fs-server
15+
```
16+
17+
* Or you can clone from GitHub and build the binary yourself
18+
```
19+
git clone https://github.com/prdpx7/go-fileserver --depth=1
20+
cd go-fileserver/fs-server
21+
# requires go 1.14
22+
go build
23+
# make binary executable
24+
chmod +x ./fs-server
25+
# may require root permission
26+
cp fs-server /usr/local/bin/fs-server
27+
```
1228
# Usage
1329
```
1430
fs-server - A simple HTTP Server to share files on a network.
@@ -20,4 +36,15 @@ fs-server - serve files from current directory
2036
fs-server /home/user/documents/ - serve files from given directory
2137
```
2238
# Demo
23-
<img src ="./fs-server.gif">
39+
40+
### Step 1 - Run in terminal
41+
<img src ="./fs-server_cli.gif" width=800 height=500>
42+
43+
### Step 2 - Scan QRCode on Phone
44+
<img src="./fs-server_mobile.gif" width=350 height=700>
45+
46+
# Inspiration
47+
* Inspired from [http-server](https://github.com/http-party/http-server) project
48+
49+
# Licence
50+
* MIT

fileserver.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type customFileHandler struct {
2020
root http.FileSystem
2121
}
2222

23+
//CustomFileServer ...
2324
func CustomFileServer(root http.FileSystem) http.Handler {
2425
return &customFileHandler{root}
2526
}
@@ -31,11 +32,11 @@ func (cf *customFileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
3132
r.URL.Path = upath
3233
}
3334

34-
ServeFile(w, r, cf.root, path.Clean(upath), true)
35+
ServeFile(w, r, cf.root, path.Clean(upath), true, "")
3536
}
3637

3738
// ServeFile ...
38-
func ServeFile(w http.ResponseWriter, r *http.Request, fs http.FileSystem, name string, redirect bool) {
39+
func ServeFile(w http.ResponseWriter, r *http.Request, fs http.FileSystem, name string, redirect bool, templateName string) {
3940
f, err := fs.Open(name)
4041
if err != nil {
4142
msg, code := toHTTPError(err)
@@ -50,8 +51,9 @@ func ServeFile(w http.ResponseWriter, r *http.Request, fs http.FileSystem, name
5051
http.Error(w, msg, code)
5152
return
5253
}
54+
5355
if d.IsDir() {
54-
ListDirectory(w, r, f, "index")
56+
ListDirectory(w, r, f, templateName)
5557
return
5658
}
5759
http.ServeContent(w, r, d.Name(), d.ModTime(), f)
@@ -68,6 +70,7 @@ func toHTTPError(err error) (msg string, httpStatus int) {
6870
return "500 Internal Server Error", http.StatusInternalServerError
6971
}
7072

73+
7174
//ListDirectory render directory content in templateName.html
7275
func ListDirectory(w http.ResponseWriter, r *http.Request, f http.File, templateName string) {
7376
RootDir, err := f.Stat()
@@ -90,7 +93,7 @@ func ListDirectory(w http.ResponseWriter, r *http.Request, f http.File, template
9093
fileExtension := "page"
9194
if d.IsDir() {
9295
name += "/"
93-
fileExtension = "directory"
96+
fileExtension = "folder"
9497
} else if len(filepath.Ext(name)) > 1 {
9598
fileExtension = filepath.Ext(name)[1:]
9699
}
@@ -119,15 +122,26 @@ type FileContent struct {
119122
}
120123

121124
func renderTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
122-
t, err := template.ParseFiles(tmpl + ".html")
125+
var t *template.Template
126+
var err error
127+
// use default rendering html
128+
if len(tmpl) == 0 {
129+
t = template.New("index")
130+
t, err = t.Parse(utils.DirListTemplateHTML)
131+
} else {
132+
templatePath, _ := filepath.Abs(tmpl + ".html")
133+
fmt.Println("template path", templatePath)
134+
t, err = template.ParseFiles(templatePath)
135+
}
136+
123137
if err != nil {
124138
fmt.Println("Error in parsing template ",err)
125139
panic(err)
126140
}
127141
t.Execute(w, data)
128142
}
129143

130-
//RequestLogger
144+
//RequestLogger ...
131145
func RequestLogger(handler http.Handler) http.Handler {
132146
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
133147
log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)

fs-server.gif

-6.91 MB
Binary file not shown.

fs-server/fs-server

223 Bytes
Binary file not shown.

fs-server/index.html

Lines changed: 0 additions & 379 deletions
This file was deleted.

fs-server/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ func main() {
2525
fmt.Printf(url)
2626
qr, _ := qrcode.New(url, qrcode.High)
2727
// qr.DisableBorder = true
28-
art := qr.ToString(false)
29-
fmt.Println(art)
28+
fmt.Println(qr.ToSmallString(false))
3029
}
3130

3231
fs := fileserver.CustomFileServer(http.Dir(dirpath))

fs-server_cli.gif

431 KB
Loading

fs-server_mobile.gif

2.73 MB
Loading

go.mod

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@ module github.com/prdpx7/go-fileserver
22

33
go 1.14
44

5-
require github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
5+
require (
6+
github.com/gobuffalo/envy v1.9.0 // indirect
7+
github.com/gobuffalo/packd v1.0.0 // indirect
8+
github.com/gobuffalo/packr v1.30.1 // indirect
9+
github.com/rogpeppe/go-internal v1.6.0 // indirect
10+
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
11+
)

0 commit comments

Comments
 (0)