feat: vendor, makefiles, build settings
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"database/sql"
|
||||
"expvar"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
@@ -23,6 +24,10 @@ import (
|
||||
// number as a hard-coded global constant.
|
||||
const version = "1.0.0"
|
||||
|
||||
// Create a buildTime variable to hold the executable binary build time. Note that this
|
||||
// must be a string type, as the -X linker flag will only work with string variables.
|
||||
var buildTime string
|
||||
|
||||
// Define a config struct to hold all the configuration settings for our application.
|
||||
// For now, the only configuration settings will be the network port that we want the
|
||||
// server to listen on, and the name of the current operating environment for the
|
||||
@@ -120,8 +125,20 @@ func main() {
|
||||
return nil
|
||||
})
|
||||
|
||||
// Create a new version boolean flag with the default value of false.
|
||||
displayVersion := flag.Bool("version", false, "Display version and exit")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
// If the version flag value is true, then print out the version number and
|
||||
// immediately exit.
|
||||
if *displayVersion {
|
||||
fmt.Printf("Version:\t%s\n", version)
|
||||
// Print out the contents of the buildTime variable.
|
||||
fmt.Printf("Build time:\t%s\n", buildTime)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// Call the openDB() helper function (see below) to create the connection pool,
|
||||
// passing in the config struct. If this returns an error, we log it and exit the
|
||||
// application immediately.
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"errors"
|
||||
"expvar"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -12,6 +11,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/felixge/httpsnoop"
|
||||
"github.com/tomasen/realip"
|
||||
"golang.org/x/time/rate"
|
||||
"greenlight.debuggingjon.dev/internal/data"
|
||||
"greenlight.debuggingjon.dev/internal/validator"
|
||||
@@ -82,20 +82,14 @@ func (app *application) rateLimit(next http.Handler) http.Handler {
|
||||
}()
|
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Only carry out the check if rate limiting is enabled.
|
||||
if app.config.limiter.enabled {
|
||||
ip, _, err := net.SplitHostPort(r.RemoteAddr)
|
||||
if err != nil {
|
||||
app.serverErrorResponse(w, r, err)
|
||||
return
|
||||
}
|
||||
// Use the realip.FromRequest() function to get the client's real IP address.
|
||||
ip := realip.FromRequest(r)
|
||||
|
||||
mu.Lock()
|
||||
|
||||
if _, found := clients[ip]; !found {
|
||||
clients[ip] = &client{
|
||||
// Use the requests-per-second and burst values from the config
|
||||
// struct.
|
||||
limiter: rate.NewLimiter(rate.Limit(app.config.limiter.rps), app.config.limiter.burst),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user