-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
114 lines (91 loc) · 2.61 KB
/
Copy pathmain.go
File metadata and controls
114 lines (91 loc) · 2.61 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"flag"
"fmt"
"log/slog"
"net/http"
"os"
"os/user"
"path/filepath"
"strings"
"github.com/NYTimes/gziphandler"
"github.com/banansys/httpserver"
"github.com/rs/cors"
)
const (
HealthCheckPath = "/_health"
IndexFile = "index.html"
)
func main() {
defer handlePanic()
slog.SetDefault(httpserver.DefaultLoggerProduction)
var (
mode = flag.String("mode", "production", "Server mode (default: development) [development|production]")
port = flag.Int("port", 3000, "HTTP server port")
)
flag.Parse()
addr := fmt.Sprintf(":%d", *port)
serverMode := setLoggerAndServerMode(*mode)
serveDir := ""
if len(flag.Args()) == 1 {
serveDir = flag.Args()[0]
} else {
serveDir = Must(os.Getwd())
}
serveDir = ResolvePath(serveDir)
server := setupServer(serverMode, serveDir, addr)
slog.Info("", "www-root", serveDir)
if err := server.Run(); err != nil {
slog.Error("server error", "err", err)
}
}
func setLoggerAndServerMode(modeFlag string) httpserver.Mode {
serverMode := httpserver.ModeDevelopment
if modeFlag == "production" {
slog.SetDefault(httpserver.DefaultLoggerProduction)
serverMode = httpserver.ModeProduction
} else {
slog.SetDefault(httpserver.DefaultLoggerDevelopment)
}
return serverMode
}
func setupServer(serverMode httpserver.Mode, rootDir, listenAddr string) *httpserver.Server {
mux := http.NewServeMux()
mux.Handle(HealthCheckPath, healthcheckHandler(rootDir))
mux.Handle("/", spaHandler(rootDir))
// middlewares ..
muxWithMiddlewares := requestLogging(slog.Default())(mux)
muxWithMiddlewares = cors.AllowAll().Handler(muxWithMiddlewares)
muxWithMiddlewares = gziphandler.GzipHandler(muxWithMiddlewares)
return httpserver.New(muxWithMiddlewares,
httpserver.WithMode(serverMode),
httpserver.WithLogger(slog.Default()),
httpserver.ListenOn(listenAddr),
)
}
func handlePanic() {
if err := recover(); err != nil {
slog.Error("panic", "err", err)
os.Exit(1)
}
}
func Must[T any](res T, err error) T {
if err != nil {
panic(err)
}
return res
}
// ResolvePath resolves a path (absolute, relative, or home path with ~) to an absolute path,
// and also resolves any symbolic links along the way.
func ResolvePath(inputPath string) string {
// Handle home directory path starting with `~`
if strings.HasPrefix(inputPath, "~") {
// Replace ~ with the user's home directory
inputPath = filepath.Join(Must(user.Current()).HomeDir, inputPath[1:])
}
// Convert relative path to absolute path
absPath := Must(filepath.Abs(inputPath))
// Resolve any symbolic links in the path
resolvedPath := Must(filepath.EvalSymlinks(absPath))
return resolvedPath
}