-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstatic_file_example.go
More file actions
162 lines (138 loc) · 4.62 KB
/
static_file_example.go
File metadata and controls
162 lines (138 loc) · 4.62 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/hypnguyen1209/ming/v2"
"github.com/valyala/fasthttp"
)
// API route handler
func apiHandler(ctx *fasthttp.RequestCtx) {
fmt.Fprintf(ctx, "API response: %s", ctx.Path())
}
// Custom 404 handler
func custom404Handler(ctx *fasthttp.RequestCtx) {
ctx.SetStatusCode(fasthttp.StatusNotFound)
ctx.SetContentType("text/html; charset=utf-8")
fmt.Fprintf(ctx, "<html><body><h1>Custom 404 Page</h1><p>Path not found: %s</p></body></html>", ctx.Path())
}
func main() {
// Create a demo directory structure for static files
setupStaticFiles()
// Create a new router
r := ming.New()
// Define API routes first
r.Get("/api/info", apiHandler)
r.Get("/api/status", apiHandler)
// Set custom 404 handler (optional)
// Note: This won't be used for static file serving if r.Static() is called
r.NotFound = custom404Handler
// Serve static files from the "static_demo" directory (with directory listing enabled)
// Any request not matching a defined route will try to serve a static file
r.Static("./static_demo", true)
fmt.Println("Server is running on http://localhost:8080")
fmt.Println("Try these URLs:")
fmt.Println("- http://localhost:8080/ (Serves index.html)")
fmt.Println("- http://localhost:8080/styles.css (Serves CSS file)")
fmt.Println("- http://localhost:8080/images/ (Shows directory listing)")
fmt.Println("- http://localhost:8080/api/info (API endpoint)")
fmt.Println("- http://localhost:8080/not-found (404 for non-existent files)")
// Start the server
r.Run(":8080")
}
// Helper function to set up demo static files
func setupStaticFiles() {
// Create directories
os.RemoveAll("./static_demo") // Clean up any existing directory
os.MkdirAll("./static_demo/images", os.ModePerm)
os.MkdirAll("./static_demo/js", os.ModePerm)
// Create index.html
indexHTML := `<!DOCTYPE html>
<html>
<head>
<title>Ming Static File Server</title>
<link rel="stylesheet" href="/styles.css">
<script src="/js/app.js"></script>
</head>
<body>
<h1>Welcome to Ming Static File Server</h1>
<p>This is a demonstration of the static file serving capability.</p>
<ul>
<li><a href="/images/">Browse Images Directory</a></li>
<li><a href="/api/info">API Example</a></li>
<li><a href="/not-found">Test 404 Page</a></li>
</ul>
</body>
</html>`
os.WriteFile("./static_demo/index.html", []byte(indexHTML), 0644)
// Create CSS file
cssContent := `body {
font-family: Arial, sans-serif;
line-height: 1.6;
max-width: 800px;
margin: 0 auto;
padding: 20px;
color: #333;
}
h1 {
color: #0066cc;
border-bottom: 1px solid #eee;
padding-bottom: 10px;
}
a {
color: #0066cc;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
ul {
background-color: #f5f5f5;
padding: 15px 15px 15px 30px;
border-radius: 5px;
}`
os.WriteFile("./static_demo/styles.css", []byte(cssContent), 0644)
// Create JavaScript file
jsContent := `document.addEventListener('DOMContentLoaded', function() {
console.log('Static file server example loaded');
// Add a timestamp to the page
const footer = document.createElement('footer');
footer.style.marginTop = '30px';
footer.style.borderTop = '1px solid #eee';
footer.style.paddingTop = '10px';
footer.style.fontSize = '12px';
footer.style.color = '#666';
footer.textContent = 'Page rendered at: ' + new Date().toLocaleString();
document.body.appendChild(footer);
});`
os.WriteFile("./static_demo/js/app.js", []byte(jsContent), 0644)
// Create a sample image (1x1 pixel transparent GIF)
transparentGif := []byte{
0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01, 0x00, 0x80, 0x00,
0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x21, 0xF9, 0x04, 0x01, 0x00,
0x00, 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00,
0x00, 0x02, 0x02, 0x44, 0x01, 0x00, 0x3B,
}
os.WriteFile("./static_demo/images/sample.gif", transparentGif, 0644)
// Create a README file in the images directory
readmeContent := `# Image Directory
This directory contains sample images for the static file server example.
Current files:
- sample.gif - A 1x1 transparent GIF image
`
os.WriteFile("./static_demo/images/README.md", []byte(readmeContent), 0644)
// Print the created directory structure
fmt.Println("Created static file demo directory structure:")
filepath.Walk("./static_demo", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
fmt.Printf("Directory: %s/\n", path)
} else {
fmt.Printf("File: %s (%d bytes)\n", path, info.Size())
}
return nil
})
fmt.Println()
}