-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
71 lines (58 loc) · 1.49 KB
/
Copy pathmain.go
File metadata and controls
71 lines (58 loc) · 1.49 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
package main
import (
"embed"
"flag"
"io/fs"
"log"
"net/http"
"os"
"path/filepath"
"github.com/jrswab/go-htmx-forge/config"
"github.com/jrswab/go-htmx-forge/internal/httpapp"
)
// To include dot (hidden) files use static/*
//
//go:embed static
var static embed.FS
// @anchor:main:lifecycle flags, config, logging, FS rooting, listener
func main() {
var (
port = ":80"
isLocal = flag.Bool("l", false, "set local development variables")
)
flag.Parse()
if *isLocal {
port = ":3000"
}
file, err := openLogFile("logs/site.log")
if err != nil {
log.Fatalf("Failed to open log file: %v", err)
}
defer file.Close()
// Set the output of the log package to the log file
log.SetOutput(file)
cfg := new(config.Config)
if err := cfg.Load(); err != nil {
log.Fatalf("config: %v", err)
}
// Root filesystems outside request-serving composition.
staticFS, err := fs.Sub(static, "static")
if err != nil {
log.Fatal(err)
}
mediaFS := os.DirFS("media")
handler := httpapp.New(staticFS, mediaFS, cfg.CORSAllowedOrigins)
log.Printf("Starting server on port %s", port)
if err := http.ListenAndServe(port, handler); err != nil {
log.Fatal(err)
}
}
// openLogFile creates the parent directory if needed, then opens/creates the log file.
func openLogFile(path string) (*os.File, error) {
if dir := filepath.Dir(path); dir != "" && dir != "." {
if err := os.MkdirAll(dir, 0o755); err != nil {
return nil, err
}
}
return os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o666)
}