Skip to content

Commit

Permalink
add export_test
Browse files Browse the repository at this point in the history
  • Loading branch information
janiltonmaciel committed Sep 20, 2020
1 parent d4049fe commit 55659a5
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 32 deletions.
6 changes: 6 additions & 0 deletions cmd/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package cmd

var (
NewApp = newApp
VersionPrinter = versionPrinter
)
18 changes: 12 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package cmd

import (
"fmt"
"os"

"github.com/janiltonmaciel/statiks/lib"
"github.com/urfave/cli/v2"
)

// CreateApp.
func CreateApp(version, commit, date string) *cli.App {
// Run cli.
func Run(version, commit, date string) error {
cli.AppHelpTemplate = appHelpTemplate
cli.VersionPrinter = versionPrinter(commit, date)
cli.HelpFlag = &cli.BoolFlag{
Expand All @@ -21,18 +22,23 @@ func CreateApp(version, commit, date string) *cli.App {
Usage: "print the version",
}

app := createCliApp(
app := newApp(
version,
)
return app
return app.Run(os.Args)
}

func createCliApp(version string) *cli.App {
func newApp(version string) *cli.App {
app := cli.NewApp()
app.Name = "statiks"
app.Usage = "fast, zero-configuration, static HTTP filer server."
app.UsageText = "statiks [options] <path>"
app.Authors = []*cli.Author{{Name: "Janilton Maciel", Email: "[email protected]"}}
app.Authors = []*cli.Author{
{
Name: "Janilton Maciel",
Email: "[email protected]",
},
}
app.Version = version
app.Flags = createFlags()
app.Action = func(c *cli.Context) error {
Expand Down
12 changes: 6 additions & 6 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ import (
)

func (s *StatiksSuite) TestApp(c *check.C) {
commit := "da3c509"
date := "2020-09-03T14:45:36Z"
version := "v0.1"
app := cmd.CreateApp(version, commit, date)
app := cmd.NewApp(version)

c.Assert(app.Version, check.Equals, version)
c.Assert(app.Authors[0].Name, check.Equals, "Janilton Maciel")
c.Assert(app.Authors[0].Email, check.Equals, "[email protected]")
}

func (s *StatiksSuite) TestVersionPrinter(c *check.C) {
version := "v0.1"
app := cmd.NewApp(version)

commit := "da3c509"
date := "2020-09-03T14:45:36Z"
version := "v0.1"
app := cmd.CreateApp(version, commit, date)
set := flag.NewFlagSet("test", 0)
ctx := cli.NewContext(app, set, nil)
cli.VersionPrinter(ctx)
vp := cmd.VersionPrinter(commit, date)
vp(ctx)
}
1 change: 1 addition & 0 deletions lib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var hostReplacer = strings.NewReplacer(
"https://", "",
)

// Create New Config.
func NewConfig(c *cli.Context) (config Config) {
config.Host = getHostAddress(c)
config.Path = getPath(c)
Expand Down
7 changes: 7 additions & 0 deletions lib/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package lib

var (
NoCacheHandler = noCacheHandler
CacheHandler = cacheHandler
DelayHandler = delayHandler
)
2 changes: 1 addition & 1 deletion lib/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type neuteredFileSystem struct {
func (nfs neuteredFileSystem) Open(path string) (http.File, error) {
// not allowed hidden file
if !nfs.hidden {
if HideFile(path) {
if hideFile(path) {
return nil, os.ErrNotExist
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/file_hidden.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import (
"strings"
)

func HideFile(path string) bool {
func hideFile(path string) bool {
return strings.HasPrefix(filepath.Base(path), ".")
}
2 changes: 1 addition & 1 deletion lib/file_hidden_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"syscall"
)

func HideFile(path string) bool {
func hideFile(path string) bool {
base := filepath.Base(path)
filenameW, err := syscall.UTF16PtrFromString(base)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion lib/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func dirList(w http.ResponseWriter, r *http.Request, f http.File, config Config)
fmt.Fprintf(w, "<pre>\n")
for _, d := range dirs {
name := d.Name()
isHidden := HideFile(name)
isHidden := hideFile(name)

if !config.IncludeHidden && isHidden {
// not allowed hidden file
Expand Down
6 changes: 3 additions & 3 deletions lib/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var noCacheHeaders = map[string]string{
"Expires": "0",
}

func NoCacheHandler(h http.Handler) http.Handler {
func noCacheHandler(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
for k, v := range noCacheHeaders {
w.Header().Set(k, v)
Expand All @@ -24,7 +24,7 @@ func NoCacheHandler(h http.Handler) http.Handler {
return http.HandlerFunc(fn)
}

func CacheHandler(h http.Handler, cache int) http.Handler {
func cacheHandler(h http.Handler, cache int) http.Handler {
v := fmt.Sprintf("max-age=%d", cache)
fn := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", v)
Expand All @@ -34,7 +34,7 @@ func CacheHandler(h http.Handler, cache int) http.Handler {
return http.HandlerFunc(fn)
}

func DelayHandler(h http.Handler, delay time.Duration) http.Handler {
func delayHandler(h http.Handler, delay time.Duration) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
time.Sleep(delay)
w.Header().Set("X-Delay", delay.String())
Expand Down
12 changes: 6 additions & 6 deletions lib/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ import (

const requestMsg = "completed request"

var logger = golog.New(os.Stdout, "", golog.Ldate|golog.Ltime|golog.Lmicroseconds)
var logg = golog.New(os.Stdout, "", golog.Ldate|golog.Ltime|golog.Lmicroseconds)

type Logger struct {
type logger struct {
AppName string
}

func NewLogger(appName string) *Logger {
return &Logger{AppName: appName}
func newLogger(appName string) *logger {
return &logger{AppName: appName}
}

func (l *Logger) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
func (l *logger) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
start := time.Now()
next(rw, r)
elapsed := time.Since(start)
Expand All @@ -45,5 +45,5 @@ func (l *Logger) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.Ha
buf.WriteString(elapsed.String())
buf.WriteString(" cachecontrol: ")
buf.WriteString(rw.Header().Get("Cache-Control"))
logger.Printf("[%s] %s", l.AppName, buf.String())
logg.Printf("[%s] %s", l.AppName, buf.String())
}
8 changes: 4 additions & 4 deletions lib/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ func NewServer(config Config) *Server {

var handler http.Handler
if config.HasCache {
handler = CacheHandler(fs, config.Cache)
handler = cacheHandler(fs, config.Cache)
} else {
handler = NoCacheHandler(fs)
handler = noCacheHandler(fs)
}

// add delay
if config.Delay > 0 {
handler = DelayHandler(handler, config.Delay)
handler = delayHandler(handler, config.Delay)
}

mux := http.NewServeMux()
Expand All @@ -49,7 +49,7 @@ func NewServer(config Config) *Server {

// add middleware logger
if !config.Quiet {
n.Use(NewLogger(projectName))
n.Use(newLogger(projectName))
}

// add middleware gzip
Expand Down
4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"log"
"os"

"github.com/janiltonmaciel/statiks/cmd"
)
Expand All @@ -14,8 +13,7 @@ var (
)

func main() {
app := cmd.CreateApp(version, commit, date)
if err := app.Run(os.Args); err != nil {
if err := cmd.Run(version, commit, date); err != nil {
log.Fatal(err)
}
}

0 comments on commit 55659a5

Please sign in to comment.