Skip to content

Commit

Permalink
-n command line argument and n query string param
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebd committed Oct 6, 2023
1 parent 8818188 commit 60b94f9
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 11 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ are naturally in `/var/log`.

## Arguments

| Argument | Description |
|------------------|---------------------------------------|
| `-h` \| `--help` | Print the help message |
| `-lt` | Log with timestamps (UTC) |
| `-p` | HTTP Port to listen on (default 8080) |
| Argument | Description |
|------------------|----------------------------------------|
| `-h` \| `--help` | Print the help message |
| `-lt` | Log with timestamps (UTC) |
| `-n` | Number of lines to return (default 25) |
| `-p` | HTTP Port to listen on (default 8080) |
7 changes: 5 additions & 2 deletions config/arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import (
)

type Arguments struct {
HttpPort int
LogTimestamps bool
HttpPort int
LogTimestamps bool
NumberOfLogLines int
}

func ParseArguments() Arguments {
args := Arguments{}
configure(&args)

flag.IntVar(&args.HttpPort, "p", HttpPort, "HTTP port")
flag.BoolVar(&args.LogTimestamps, "lt", false, "Log timestamps (UTC)")
flag.IntVar(&args.NumberOfLogLines, "n", NumberOfLogLines, "Number of log lines to return")
flag.Parse()

return args
Expand Down
1 change: 1 addition & 0 deletions config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package config
const (
HttpPort = 8080
LogDirectory = "/var/log"
NumberOfLogLines = 25
UnsupportedFileTypes = "gz|zip|bz2|tar|tgz|7z|rar"
)
13 changes: 13 additions & 0 deletions config/inject.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package config

// Framework-less "dependency injection" for code paths that are difficult to pass these Arguments to

var arguments *Arguments

func configure(args *Arguments) {
arguments = args
}

func GetArguments() *Arguments {
return arguments
}
18 changes: 17 additions & 1 deletion controller/rest/v1/logs.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package v1

import (
"fmt"
"github.com/julienschmidt/httprouter"
"go-read-var-log/config"
"go-read-var-log/controller/rest/util"
"go-read-var-log/service"
"log"
"net/http"
"slices"
"strconv"
)

// GetLogs handles GET /api/v1/logs and returns a list of log files in the log directory
Expand All @@ -21,7 +23,21 @@ func GetLogs(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
func GetLog(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
log.Println("handling", r.URL.RequestURI())
logFilename := p.ByName("log")
logEvents, err := service.GetLog(config.LogDirectory, logFilename)

maxLines := config.GetArguments().NumberOfLogLines
maxLinesParam := r.URL.Query().Get("n")
if maxLinesParam != "" {
intParam, err := strconv.Atoi(maxLinesParam)
if err == nil && intParam > 0 {
maxLines = intParam
} else {
util.RenderTextPlain(w, nil, fmt.Errorf("invalid value for parameter 'n': '%s'", maxLinesParam))
return
}
}

logEvents, err := service.GetLog(config.LogDirectory, logFilename, maxLines)

slices.Reverse(logEvents)
util.RenderTextPlain(w, logEvents, err)
}
11 changes: 9 additions & 2 deletions service/getLog.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// GetLog returns the contents of a log file
func GetLog(directoryPath string, filename string) ([]string, error) {
func GetLog(directoryPath string, filename string, maxLines int) ([]string, error) {
filepath := strings.Join([]string{directoryPath, filename}, "/")

if !validLogFromName(directoryPath, filename) {
Expand All @@ -19,5 +19,12 @@ func GetLog(directoryPath string, filename string) ([]string, error) {
if err != nil {
return nil, err
}
return strings.Split(string(byteSlice), "\n"), nil
result := strings.Split(string(byteSlice), "\n")

// Restrict output to at most maxLines
endIndex := len(result) - 1
startIndex := max(0, endIndex-maxLines)
result = result[startIndex:endIndex]

return result, nil
}
25 changes: 25 additions & 0 deletions service/getLog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package service

import (
"fmt"
"strings"
)

// Log output is in natural order at the service layer.
// Controllers may reverse the order as required by their clients.

func ExampleGetLog_logDir1_10KiB_log() {
lines, _ := GetLog("testdata/logDir1", "10KiB.log", 2)
fmt.Println(strings.Join(lines, "\n"))
// Output:
// 2023-10-06T15:18:24.408740Z|info |olaret esanus ivo hey enug tewos ebad it u tuge po elora e iwemat o
// 2023-10-06T15:18:24.408762Z|debug|tucev uho e u ela opif ce igodeto hudegor ivosu ehab eaunopi balohan tagused gicefas
}

func ExampleGetLog_logDir1_1line_log() {
// Requesting more lines than available returns all available lines.
lines, _ := GetLog("testdata/logDir1", "1line.log", 10)
fmt.Println(strings.Join(lines, "\n"))
// Output:
// 2023-10-06T15:18:24.406350Z|debug|toyeni vate riwehu ato ped afe ral bo h redi esohet sir moyireh nema lidef
}
2 changes: 1 addition & 1 deletion service/listLogs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import (
func ExampleListLogs_logDir1() {
fmt.Println(ListLogs("testdata/logDir1"))
// Output:
// [10KiB.log] <nil>
// [10KiB.log 1line.log] <nil>
}
1 change: 1 addition & 0 deletions service/testdata/logDir1/1line.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2023-10-06T15:18:24.406350Z|debug|toyeni vate riwehu ato ped afe ral bo h redi esohet sir moyireh nema lidef

0 comments on commit 60b94f9

Please sign in to comment.