-
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.
Merge pull request #7 from plan97/dev
Refactored API's and added new tests
- Loading branch information
Showing
16 changed files
with
636 additions
and
543 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
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 |
---|---|---|
@@ -1,4 +1,17 @@ | ||
# gofileserver | ||
# Go File Server | ||
[![Go Reference](https://pkg.go.dev/badge/github.com/plan97/gofileserver.svg)](https://pkg.go.dev/github.com/plan97/gofileserver) | ||
|
||
A simple server with web UI, which serves files under a directory and is written in Go. | ||
|
||
## Install | ||
|
||
``` | ||
go get github.com/plan97/gofileserver/cmd/runfs | ||
``` | ||
|
||
## Usage | ||
|
||
After installing, run the following to check on usage. | ||
``` | ||
runfs --help | ||
``` |
This file was deleted.
Oops, something went wrong.
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
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,25 @@ | ||
package main | ||
|
||
import ( | ||
server "github.com/plan97/gofileserver" | ||
"github.com/plan97/gofileserver/config" | ||
) | ||
|
||
func runfs() error { | ||
conf := config.New() | ||
if err := conf.Fetch(); err != nil { | ||
return err | ||
} | ||
|
||
router, err := server.Setup(conf) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if conf.HTTPS { | ||
err = router.RunTLS(conf.Addr, conf.SSLCertFile, conf.SSLKeyFile) | ||
} else { | ||
err = router.Run(conf.Addr) | ||
} | ||
return err | ||
} |
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
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,24 @@ | ||
package config | ||
|
||
import "testing" | ||
|
||
func TestConfig_Fetch(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
conf *Config | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "test 1", | ||
conf: New(), | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := tt.conf.Fetch(); (err != nil) != tt.wantErr { | ||
t.Errorf("Config.Fetch() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,12 +1,11 @@ | ||
module github.com/plan97/gofileserver | ||
|
||
go 1.15 | ||
go 1.16 | ||
|
||
require ( | ||
github.com/gin-contrib/cors v1.3.1 | ||
github.com/gin-gonic/gin v1.6.3 | ||
github.com/jteeuwen/go-bindata v3.0.7+incompatible | ||
github.com/olebedev/staticbin v0.0.0-20150208021645-588762e15a7f | ||
github.com/go-playground/assert/v2 v2.0.1 | ||
github.com/spf13/pflag v1.0.5 | ||
github.com/spf13/viper v1.7.1 | ||
) |
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
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 |
---|---|---|
|
@@ -43,6 +43,5 @@ func ListDirFiles(baseDir string) gin.HandlerFunc { | |
} | ||
} | ||
c.JSON(http.StatusOK, req) | ||
return | ||
} | ||
} |
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,152 @@ | ||
package handlers | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"net/http/httptest" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/go-playground/assert/v2" | ||
) | ||
|
||
var errAssertionFailed = errors.New("assertion failed") | ||
|
||
func TestListDirFiles(t *testing.T) { | ||
type args struct { | ||
baseDir string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
testFunc func(gin.HandlerFunc) error | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "test 1", | ||
args: args{baseDir: func() string { | ||
dir, err := filepath.Abs("") | ||
if err != nil { | ||
return "" | ||
} | ||
return dir | ||
}()}, | ||
testFunc: func(fn gin.HandlerFunc) error { | ||
gin.SetMode(gin.ReleaseMode) | ||
router := gin.New() | ||
router.Use(gin.Recovery()) | ||
api := router.Group("/api") | ||
api.POST("list_dir_files", fn) | ||
|
||
w := httptest.NewRecorder() | ||
body := strings.NewReader(`{"dir":[]}`) | ||
req, _ := http.NewRequest(http.MethodPost, "/api/list_dir_files", body) | ||
req.Header.Set("Content-Type", "application/json") | ||
router.ServeHTTP(w, req) | ||
|
||
if !assert.IsEqual(http.StatusOK, w.Code) { | ||
return errAssertionFailed | ||
} | ||
return nil | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "test 2", | ||
args: args{baseDir: func() string { | ||
dir, err := filepath.Abs("") | ||
if err != nil { | ||
return "" | ||
} | ||
return dir | ||
}()}, | ||
testFunc: func(fn gin.HandlerFunc) error { | ||
gin.SetMode(gin.ReleaseMode) | ||
router := gin.New() | ||
router.Use(gin.Recovery()) | ||
api := router.Group("/api") | ||
api.POST("list_dir_files", fn) | ||
|
||
w := httptest.NewRecorder() | ||
body := strings.NewReader(`{"dir":[".."]}`) | ||
req, _ := http.NewRequest(http.MethodPost, "/api/list_dir_files", body) | ||
req.Header.Set("Content-Type", "application/json") | ||
router.ServeHTTP(w, req) | ||
|
||
if !assert.IsEqual(http.StatusBadRequest, w.Code) { | ||
return errAssertionFailed | ||
} | ||
return nil | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "test 3", | ||
args: args{baseDir: func() string { | ||
dir, err := filepath.Abs("") | ||
if err != nil { | ||
return "" | ||
} | ||
return dir | ||
}()}, | ||
testFunc: func(fn gin.HandlerFunc) error { | ||
gin.SetMode(gin.ReleaseMode) | ||
router := gin.New() | ||
router.Use(gin.Recovery()) | ||
api := router.Group("/api") | ||
api.POST("list_dir_files", fn) | ||
|
||
w := httptest.NewRecorder() | ||
body := strings.NewReader(`{"dir":["non_existing_dir"]}`) | ||
req, _ := http.NewRequest(http.MethodPost, "/api/list_dir_files", body) | ||
req.Header.Set("Content-Type", "application/json") | ||
router.ServeHTTP(w, req) | ||
|
||
if !assert.IsEqual(http.StatusInternalServerError, w.Code) { | ||
return errAssertionFailed | ||
} | ||
return nil | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "test 4", | ||
args: args{baseDir: func() string { | ||
dir, err := filepath.Abs("") | ||
if err != nil { | ||
return "" | ||
} | ||
return dir | ||
}()}, | ||
testFunc: func(fn gin.HandlerFunc) error { | ||
gin.SetMode(gin.ReleaseMode) | ||
router := gin.New() | ||
router.Use(gin.Recovery()) | ||
api := router.Group("/api") | ||
api.POST("list_dir_files", fn) | ||
|
||
w := httptest.NewRecorder() | ||
body := strings.NewReader(`[]`) | ||
req, _ := http.NewRequest(http.MethodPost, "/api/list_dir_files", body) | ||
req.Header.Set("Content-Type", "application/json") | ||
router.ServeHTTP(w, req) | ||
|
||
if !assert.IsEqual(http.StatusBadRequest, w.Code) { | ||
return errAssertionFailed | ||
} | ||
return nil | ||
}, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := ListDirFiles(tt.args.baseDir) | ||
if err := tt.testFunc(got); (err != nil) != tt.wantErr { | ||
t.Errorf("testFunc(HandlerFunc) error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.