-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
424 lines (364 loc) 路 10.7 KB
/
Copy pathmain.go
File metadata and controls
424 lines (364 loc) 路 10.7 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// filepath: /root/debug_ming/main.go
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/hypnguyen1209/ming/v2"
"github.com/valyala/fasthttp"
)
// User represents a user in our system
type User struct {
ID string `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
Created string `json:"created"`
}
// APIResponse is a standard response format for our API
type APIResponse struct {
Success bool `json:"success"`
Message string `json:"message,omitempty"`
Data interface{} `json:"data,omitempty"`
}
// Global in-memory user database for demonstration
var users = make(map[string]User)
// Middleware to log requests
func loggerMiddleware(next fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
startTime := time.Now()
// Call the next handler
next(ctx)
// Log after the request is processed
fmt.Printf(
"[%s] %s - %s - %s - %d - %v\n",
time.Now().Format("2006/01/02 - 15:04:05"),
ctx.Method(),
ctx.Path(),
ctx.RemoteAddr(),
ctx.Response.StatusCode(),
time.Since(startTime),
)
}
}
// Middleware to check API key
func authMiddleware(next fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
// Skip auth for certain paths
path := string(ctx.Path())
if path == "/" || path == "/ping" {
next(ctx)
return
}
// Check for API key in header
apiKey := string(ctx.Request.Header.Peek("X-API-Key"))
if apiKey != "dev-secret-key" {
ctx.SetStatusCode(fasthttp.StatusUnauthorized)
response := APIResponse{
Success: false,
Message: "Unauthorized - Invalid API Key",
}
json.NewEncoder(ctx).Encode(response)
return
}
next(ctx)
}
}
// Handler for the home page
func homeHandler(ctx *fasthttp.RequestCtx) {
ctx.SetContentType("text/html")
fmt.Fprintf(ctx, "<html><body>")
fmt.Fprintf(ctx, "<h1>Welcome to ming Web Server</h1>")
fmt.Fprintf(ctx, "<p>A high-performance HTTP router for Go</p>")
fmt.Fprintf(ctx, "<h2>Available Routes:</h2>")
fmt.Fprintf(ctx, "<ul>")
fmt.Fprintf(ctx, "<li>GET /ping - Health check</li>")
fmt.Fprintf(ctx, "<li>GET /api/users - List all users</li>")
fmt.Fprintf(ctx, "<li>GET /api/users/{id} - Get user by ID</li>")
fmt.Fprintf(ctx, "<li>POST /api/users - Create a new user</li>")
fmt.Fprintf(ctx, "<li>PUT /api/users/{id} - Update a user</li>")
fmt.Fprintf(ctx, "<li>DELETE /api/users/{id} - Delete a user</li>")
fmt.Fprintf(ctx, "<li>GET /static/{filepath:*} - Serve static files</li>")
fmt.Fprintf(ctx, "</ul>")
fmt.Fprintf(ctx, "<p>Note: All API endpoints require X-API-Key header with value 'dev-secret-key'</p>")
fmt.Fprintf(ctx, "</body></html>")
}
// Handler for health check
func pingHandler(ctx *fasthttp.RequestCtx) {
response := APIResponse{
Success: true,
Message: "pong",
Data: map[string]interface{}{
"timestamp": time.Now().Unix(),
"version": "1.0.0",
},
}
ctx.SetContentType("application/json")
json.NewEncoder(ctx).Encode(response)
}
// Handler to list all users
func listUsersHandler(ctx *fasthttp.RequestCtx) {
userList := make([]User, 0, len(users))
for _, user := range users {
userList = append(userList, user)
}
response := APIResponse{
Success: true,
Data: userList,
}
ctx.SetContentType("application/json")
json.NewEncoder(ctx).Encode(response)
}
// Handler to get a user by ID
func getUserHandler(ctx *fasthttp.RequestCtx) {
id := ming.Param(ctx, "id")
user, exists := users[id]
if !exists {
ctx.SetStatusCode(fasthttp.StatusNotFound)
response := APIResponse{
Success: false,
Message: fmt.Sprintf("User with ID %s not found", id),
}
json.NewEncoder(ctx).Encode(response)
return
}
response := APIResponse{
Success: true,
Data: user,
}
ctx.SetContentType("application/json")
json.NewEncoder(ctx).Encode(response)
}
// Handler to create a new user
func createUserHandler(ctx *fasthttp.RequestCtx) {
var input struct {
Username string `json:"username"`
Email string `json:"email"`
}
if err := json.Unmarshal(ctx.PostBody(), &input); err != nil {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
response := APIResponse{
Success: false,
Message: "Invalid JSON payload",
}
json.NewEncoder(ctx).Encode(response)
return
}
// Validate input
if input.Username == "" || input.Email == "" {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
response := APIResponse{
Success: false,
Message: "Username and email are required",
}
json.NewEncoder(ctx).Encode(response)
return
}
// Create new user
id := fmt.Sprintf("user_%d", len(users)+1)
user := User{
ID: id,
Username: input.Username,
Email: input.Email,
Created: time.Now().Format(time.RFC3339),
}
users[id] = user
ctx.SetStatusCode(fasthttp.StatusCreated)
response := APIResponse{
Success: true,
Message: "User created successfully",
Data: user,
}
ctx.SetContentType("application/json")
json.NewEncoder(ctx).Encode(response)
}
// Handler to update a user
func updateUserHandler(ctx *fasthttp.RequestCtx) {
id := ming.Param(ctx, "id")
// Check if user exists
user, exists := users[id]
if !exists {
ctx.SetStatusCode(fasthttp.StatusNotFound)
response := APIResponse{
Success: false,
Message: fmt.Sprintf("User with ID %s not found", id),
}
json.NewEncoder(ctx).Encode(response)
return
}
// Parse input
var input struct {
Username string `json:"username"`
Email string `json:"email"`
}
if err := json.Unmarshal(ctx.PostBody(), &input); err != nil {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
response := APIResponse{
Success: false,
Message: "Invalid JSON payload",
}
json.NewEncoder(ctx).Encode(response)
return
}
// Update user fields if provided
if input.Username != "" {
user.Username = input.Username
}
if input.Email != "" {
user.Email = input.Email
}
// Save updated user
users[id] = user
response := APIResponse{
Success: true,
Message: "User updated successfully",
Data: user,
}
ctx.SetContentType("application/json")
json.NewEncoder(ctx).Encode(response)
}
// Handler to delete a user
func deleteUserHandler(ctx *fasthttp.RequestCtx) {
id := ming.Param(ctx, "id")
// Check if user exists
_, exists := users[id]
if !exists {
ctx.SetStatusCode(fasthttp.StatusNotFound)
response := APIResponse{
Success: false,
Message: fmt.Sprintf("User with ID %s not found", id),
}
json.NewEncoder(ctx).Encode(response)
return
}
// Delete the user
delete(users, id)
response := APIResponse{
Success: true,
Message: fmt.Sprintf("User with ID %s deleted successfully", id),
}
ctx.SetContentType("application/json")
json.NewEncoder(ctx).Encode(response)
}
// Create a static directory if it doesn't exist
func ensureStaticDir() {
staticDir := filepath.Join(".", "static")
if _, err := os.Stat(staticDir); os.IsNotExist(err) {
os.Mkdir(staticDir, 0755)
// Create a sample index.html file
indexPath := filepath.Join(staticDir, "index.html")
indexContent := `<!DOCTYPE html>
<html>
<head>
<title>Ming Static File Server</title>
<style>
body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
h1 { color: #2c3e50; }
.info { background-color: #f8f9fa; padding: 15px; border-radius: 5px; }
</style>
</head>
<body>
<h1>Static File Server</h1>
<div class="info">
<p>This is a sample static file served by ming web server.</p>
<p>You can place your static files in the <code>static</code> directory.</p>
</div>
</body>
</html>`
os.WriteFile(indexPath, []byte(indexContent), 0644)
}
}
func main() {
// Create static directory with sample file
ensureStaticDir()
// Add some example users
users["user_1"] = User{
ID: "user_1",
Username: "admin",
Email: "admin@example.com",
Created: time.Now().Format(time.RFC3339),
}
users["user_2"] = User{
ID: "user_2",
Username: "test",
Email: "test@example.com",
Created: time.Now().Format(time.RFC3339),
}
// Initialize ming router
r := ming.New()
// Create middleware chain by wrapping handlers
withLogger := func(handler fasthttp.RequestHandler) fasthttp.RequestHandler {
return loggerMiddleware(handler)
}
withAuth := func(handler fasthttp.RequestHandler) fasthttp.RequestHandler {
return authMiddleware(handler)
}
// Define routes
r.Get("/", withLogger(homeHandler))
r.Get("/ping", withLogger(pingHandler))
// API routes
r.Get("/api/users", withLogger(withAuth(listUsersHandler)))
r.Get("/api/users/{id}", withLogger(withAuth(getUserHandler)))
r.Post("/api/users", withLogger(withAuth(createUserHandler)))
r.Put("/api/users/{id}", withLogger(withAuth(updateUserHandler)))
r.Delete("/api/users/{id}", withLogger(withAuth(deleteUserHandler)))
// Example route with regex validation
r.Get("/product/{id:[0-9]+}", withLogger(withAuth(func(ctx *fasthttp.RequestCtx) {
id := ming.Param(ctx, "id")
response := APIResponse{
Success: true,
Message: fmt.Sprintf("Product ID %s is valid", id),
}
ctx.SetContentType("application/json")
json.NewEncoder(ctx).Encode(response)
})))
// Optional parameter example
r.Get("/optional/{param?}", withLogger(func(ctx *fasthttp.RequestCtx) {
param := ming.Param(ctx, "param")
if param == "" {
param = "not provided"
}
response := APIResponse{
Success: true,
Message: fmt.Sprintf("Optional parameter: %s", param),
}
ctx.SetContentType("application/json")
json.NewEncoder(ctx).Encode(response)
}))
// Custom 404 handler for non-static paths
r.NotFound = withLogger(func(ctx *fasthttp.RequestCtx) {
// Handle the static files directory first
path := string(ctx.Path())
if strings.HasPrefix(path, "/static/") {
// Strip the "/static/" prefix
filePath := path[8:]
fullPath := filepath.Join("./static", filePath)
// Check if file exists
if _, err := os.Stat(fullPath); err == nil {
fasthttp.ServeFile(ctx, fullPath)
return
}
}
// Otherwise, return a 404 response
ctx.SetStatusCode(fasthttp.StatusNotFound)
ctx.SetContentType("application/json")
json.NewEncoder(ctx).Encode(APIResponse{
Success: false,
Message: fmt.Sprintf("Path not found: %s", path),
})
})
// Start the server
fmt.Println("Server starting on :8080")
fmt.Println("Home page: http://localhost:8080/")
fmt.Println("Health check: http://localhost:8080/ping")
fmt.Println("API (requires X-API-Key: dev-secret-key header):")
fmt.Println(" GET http://localhost:8080/api/users")
fmt.Println(" GET http://localhost:8080/api/users/user_1")
fmt.Println(" POST http://localhost:8080/api/users")
fmt.Println(" PUT http://localhost:8080/api/users/user_1")
fmt.Println(" DELETE http://localhost:8080/api/users/user_1")
fmt.Println("Static files: http://localhost:8080/static/index.html")
r.Run(":8080")
}