-
Notifications
You must be signed in to change notification settings - Fork 0
/
servedir.go
48 lines (41 loc) · 1010 Bytes
/
servedir.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"context"
"flag"
"fmt"
"net/http"
"github.com/mikolajb/servedir/internal/handlers/files"
"github.com/mikolajb/servedir/internal/handlers/landing"
"github.com/mikolajb/servedir/internal/handlers/uploads"
"github.com/mikolajb/servedir/internal/logger"
)
var flagPath string
var flagPort uint
func main() {
flag.StringVar(&flagPath, "path", ".", "Path")
flag.UintVar(&flagPort, "port", 8080, "Port")
flag.Parse()
log, ctx := logger.NewLogger(context.Background())
log.Info().Uint("port", flagPort).Str("os root path", flagPath).Msgf("serving directory %s on port %d", flagPath, flagPort)
http.Handle(files.RootURLPath,
logger.NewLoggerForHTTPHandler(
ctx,
files.New(flagPath),
),
)
http.Handle("/upload/",
logger.NewLoggerForHTTPHandler(
ctx,
uploads.New(),
),
)
http.Handle("/",
logger.NewLoggerForHTTPHandler(
ctx,
landing.New(),
),
)
if err := http.ListenAndServe(fmt.Sprintf(":%d", flagPort), nil); err != nil {
panic(err)
}
}