Improved styling and comments
This commit is contained in:
25
main.go
25
main.go
@@ -1,3 +1,11 @@
|
||||
/*
|
||||
This application ensures the required environment variables are set
|
||||
Then it uses the included countersql package to test a connection to the database
|
||||
It then caches the number of unique visits in memory by querying the database
|
||||
Finally, it sets up a web server with the endpoint http://localhost:8080/
|
||||
For each unique source IP address which makes a GET request to this endpoint,
|
||||
the count of unique visitors is incremented by 1, both in the database and in memory cache.
|
||||
*/
|
||||
package main
|
||||
|
||||
import (
|
||||
@@ -12,8 +20,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
database countersql.Configuration
|
||||
uniqueVisits int
|
||||
database countersql.Configuration // Configuration information required to connect to the database, using the countersql package
|
||||
uniqueVisits int // In memory cache of unique visitors
|
||||
)
|
||||
|
||||
// Application Startup
|
||||
@@ -59,7 +67,6 @@ func init() {
|
||||
|
||||
// HTTP Routing
|
||||
func main() {
|
||||
|
||||
// API Handlers
|
||||
http.HandleFunc("/", countHandler)
|
||||
|
||||
@@ -68,10 +75,14 @@ func main() {
|
||||
|
||||
}
|
||||
|
||||
// HTTP handler function
|
||||
func countHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "GET" {
|
||||
// CORS header change required
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Write([]byte(strconv.Itoa(uniqueVisits)))
|
||||
|
||||
// Connect to database
|
||||
dbConn, err := database.Connect()
|
||||
if err != nil {
|
||||
log.Printf("failed to connect to database: %v", err)
|
||||
@@ -80,16 +91,17 @@ func countHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
defer dbConn.DB.Close()
|
||||
|
||||
// We now get the source IP address of this request
|
||||
var ipAddress string
|
||||
|
||||
// Check if we're behind a reverse proxy / WAF
|
||||
if len(r.Header.Get("X-Forwarded-For")) > 0 {
|
||||
ipAddress = r.Header.Get("X-Forwarded-For")
|
||||
} else {
|
||||
ipAddress = r.RemoteAddr
|
||||
}
|
||||
|
||||
ipAddress = strings.Split(ipAddress, ":")[0]
|
||||
|
||||
// Check if this is the first time this IP address has visited
|
||||
returnVisitor, err := dbConn.HasIPVisited(ipAddress)
|
||||
if err != nil {
|
||||
log.Printf("failed to determine if this is a return visitor, no data is being logged: %v", err)
|
||||
@@ -97,9 +109,11 @@ func countHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if returnVisitor {
|
||||
// Log their visit
|
||||
err = dbConn.IncrementVisitor(ipAddress)
|
||||
log.Printf("return visitor from %s", ipAddress)
|
||||
} else {
|
||||
// Insert a new visitor row in the database
|
||||
err = dbConn.AddVisitor(ipAddress)
|
||||
uniqueVisits++
|
||||
log.Printf("new visitor from %s", ipAddress)
|
||||
@@ -110,6 +124,7 @@ func countHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
} else {
|
||||
// Needs to be GET method
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user