-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
32 lines (27 loc) · 937 Bytes
/
server.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
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
)
func main() {
// create a log file to store requests
f, err := os.Create("request.log")
if err != nil {
log.Fatal(err)
}
defer f.Close()
logger := log.New(f, "", log.LstdFlags)
// Create a handler function to handle incoming HTTP requests
// Didn't really end up using the log feature. While it works, and logs information about the request, I decided to put loggin into cloudwatch instead.
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Log the request details to the file
logger.Printf("%s - [%s] \"%s %s %s\" \"%s\"\n", r.RemoteAddr, time.Now().Format("02/Jan/2006:15:04:05 -0700"), r.Method, r.URL.Path, r.Proto, r.UserAgent())
// Return a response to the client
fmt.Fprintf(w, "Successful Query to port 80")
})
log.Println("Server listening on port 80...")
log.Fatal(http.ListenAndServe(":80", handler))
}