-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
41 lines (35 loc) · 890 Bytes
/
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
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
)
func main() {
// Create a log file
logFile, err := os.OpenFile("api.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
defer logFile.Close()
// Create a logger that writes to the log file
logger := log.New(logFile, "", log.LstdFlags)
// Define an HTTP handler
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logger.Printf("Request from %s for %s", r.RemoteAddr, r.URL.Path)
fmt.Fprintln(w, "Hello, World!")
})
// Create an HTTP server with the handler
server := &http.Server{
Addr: ":8080",
Handler: handler,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
// Start the HTTP server
logger.Println("Starting server on port 8080")
if err := server.ListenAndServe(); err != nil {
logger.Fatal(err)
}
}