-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (70 loc) · 1.96 KB
/
main.go
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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/gorilla/mux"
)
// Middleware for logging requests
func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s\n", r.Method, r.RequestURI, r.RemoteAddr)
next.ServeHTTP(w, r)
})
}
// Handler for the root path
func helloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World!")
}
// Handler for a greeting endpoint with query parameters
func greetHandler(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
if name == "" {
name = "Guest"
}
fmt.Fprintf(w, "Hello, %s!", name)
}
// JSON response struct
type jsonResponse struct {
Message string `json:"message"`
}
// Handler for a JSON API endpoint
func jsonHandler(w http.ResponseWriter, r *http.Request) {
response := jsonResponse{Message: "Hello, JSON World!"}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
// Serve static files
func staticFileHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./static"+r.URL.Path)
}
func main() {
// Create a new router
r := mux.NewRouter()
// Add routes and their handlers
r.HandleFunc("/", helloHandler).Methods("GET")
r.HandleFunc("/greet", greetHandler).Methods("GET")
r.HandleFunc("/api/json", jsonHandler).Methods("GET")
// Serve static files from the "static" directory
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
// Apply logging middleware
r.Use(loggingMiddleware)
// Start the server
addr := ":8080"
if port := os.Getenv("PORT"); port != "" {
addr = ":" + port
}
srv := &http.Server{
Handler: r,
Addr: addr,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Printf("Starting server at %s\n", addr)
if err := srv.ListenAndServe(); err != nil {
log.Fatal(err)
}
}