Skip to content

Commit

Permalink
Implement Feature Issue #81
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhener committed Nov 21, 2024
1 parent 6a9792d commit 62c67f9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ For a detailed documentation go to [goshs.de](https://goshs.de)
* You can restrict access to specific files completely
* Embed files on compile time
* Self updating binary
* Write output to a log file

# Installation

Expand Down
9 changes: 4 additions & 5 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,13 @@ func NewLogger() *StandardLogger {
// standardLogger.SetReportCaller(true)
}

// We could transform the errors into a JSON format, for external log SaaS tools such as splunk or logstash
// standardLogger.Formatter = &logrus.JSONFormatter{
// PrettyPrint: true,
// }

return standardLogger
}

func LogFile(multiwriter io.Writer) {
logger.SetOutput(multiwriter)
}

// Declare variables to store log messages as new Events
var (
missingEnvMessage = Event{1, "Missing env key: %s"}
Expand Down
23 changes: 23 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"io"
"os"
"os/signal"
"path/filepath"
Expand Down Expand Up @@ -43,6 +44,7 @@ var (
leHTTPPort = "80"
leTLSPort = "443"
embedded = false
output = ""
)

// Man page
Expand All @@ -63,6 +65,7 @@ Web server options:
-si, --silent Running without dir listing (default: false)
-c, --cli Enable cli (only with auth and tls) (default: false)
-e, --embedded Show embedded files in UI (default: false)
-o, --output Write output to logfile (default: false)
TLS options:
-s, --ssl Use TLS
Expand Down Expand Up @@ -153,6 +156,8 @@ func flags() (*bool, *bool, *bool, *bool) {
flag.StringVar(&leTLSPort, "le-tls", leTLSPort, "")
flag.BoolVar(&embedded, "e", embedded, "")
flag.BoolVar(&embedded, "embedded", embedded, "")
flag.StringVar(&output, "o", output, "")
flag.StringVar(&output, "output", output, "")
updateGoshs := flag.Bool("update", false, "update")
hash := flag.Bool("H", false, "hash")
hashLong := flag.Bool("hash", false, "hash")
Expand Down Expand Up @@ -250,6 +255,7 @@ func init() {
logger.Fatalf("Webroot cannot be constructed: %+v", err)
}
}

}

// Sanity checks if basic auth has the right format
Expand Down Expand Up @@ -293,6 +299,23 @@ func main() {
myKey = "key"
}

// If a logpath/-file is provided via -o/--output set the multiwriter to output both
if output != "" {
if !filepath.IsAbs(output) {
// If the provided path is not an abspath then merge with CWD
wd, _ := os.Getwd()
output = filepath.Join(wd, output)
}

logFile, err := os.OpenFile(output, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
if err != nil {
logger.Panicf("Cannot open file to write output logfile: %s - %+v", output, err)
}

multiWriter := io.MultiWriter(os.Stdout, logFile)
logger.LogFile(multiWriter)
}

// Setup the custom file server
server := &httpserver.FileServer{
IP: ip,
Expand Down

0 comments on commit 62c67f9

Please sign in to comment.