forked from jrswab/go-pwa-forge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
113 lines (92 loc) · 2.46 KB
/
Copy pathmain.go
File metadata and controls
113 lines (92 loc) · 2.46 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
package main
import (
"embed"
"flag"
"io/fs"
"log"
"net/http"
"os"
"strings"
"github.com/jrswab/go-htmx-forge/config"
"github.com/jrswab/go-htmx-forge/handlers"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
)
// To include dot (hidden) files use static/*
//
//go:embed static
var static embed.FS
func main() {
var (
port = ":80"
isLocal = flag.Bool("l", false, "set local development variables")
)
flag.Parse()
if *isLocal {
port = ":3000"
}
// Open or create a log file
file, err := os.OpenFile("logs/site.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
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)
cfg.Load()
// Initialize Chi router
r := chi.NewRouter()
// Brotli Compression:
c := middleware.NewCompressor(brotli.DefaultCompression, // default level = 6
"text/html",
"application/json",
)
c.SetEncoder("br", func(w io.Writer, level int) io.Writer {
// NewWriterV2 currently supports levels 0-7 only.
if level > 7 {
level = brotli.DefaultCompression
}
return brotli.NewWriterV2(w, level) // v2 encoder
})
// Enable brotli compression for use
r.Use(c.Handler)
// CORS middleware
r.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"https://hda.site.com", "https://app.site.com"},
AllowedMethods: []string{"GET", "OPTIONS"},
AllowedHeaders: []string{
"Accept",
"Content-Type",
"Origin",
"hx-request",
},
}))
// Additional middleware
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
// Get paths for all files in the embeded static directory
staticFS, err := fs.Sub(static, "static")
if err != nil {
log.Fatal(err)
}
r.Handle("/static/*", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Make sure .js files have a content type of application/javascript
if strings.HasSuffix(r.URL.Path, ".js") {
w.Header().Set("Content-Type", "application/javascript")
}
http.StripPrefix("/static/", http.FileServer(http.FS(staticFS))).ServeHTTP(w, r)
}))
// Serve media files
mediaServer := http.FileServer(http.Dir("media"))
r.Handle("/media/*", http.StripPrefix("/media/", mediaServer))
// Routes
h := handlers.HomeHandler{}
r.Get("/", h.Load)
// Start server
log.Printf("Starting server on port %s", port)
if err := http.ListenAndServe(port, r); err != nil {
log.Fatal(err)
}
}