Skip to content

Commit

Permalink
Merge pull request #2 from ashleyprimo/v1.2.0-dev
Browse files Browse the repository at this point in the history
Mini Refactor:
* Allows metrics to be served on a separate socket/webserver endpoint
* `address` changes to `host`
* Code Tidy
  • Loading branch information
ashleyprimo authored Jan 17, 2022
2 parents f811ff0 + 63681e1 commit 026f5a4
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 116 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,24 @@

```
Usage of ./go-qr-generator:
-address string
The address to listen on for HTTP requests. (default "0.0.0.0")
-enable.docs
Enable documentation (/docs) endpoint. (default true)
-enable.metrics
Enable metrics (/metrics) endpoint. (default true)
-enable.metrics.server
Enable seperate metrics server
-host string
The host/address to listen on for HTTP requests. (default "0.0.0.0")
-https
Enable, or Disable HTTPS
-log.conn
Log connections to API (default true)
-log.level string
The level of logs to log (default "info")
-metrics.server.host string
The host/address to listen on for metrics HTTP requests. (default "0.0.0.0")
-metrics.server.port string
The port number to listen on for metrics HTTP requests. (default "9100")
-port string
The port number to listen on for HTTP requests. (default "8080")
-qr.default.size int
Expand Down
5 changes: 2 additions & 3 deletions documentation/documentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package docs

import (
"fmt"
"net/http"
"strconv"
"net/http"

"github.com/ashleyprimo/go-qr-generator/initialize"
"github.com/ashleyprimo/go-qr-generator/initialize"
)

func Landing(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -54,4 +54,3 @@ func Landing(w http.ResponseWriter, r *http.Request) {
)
w.Write([]byte(fmt.Sprintf(documentationPage, initialize.ApplicationName, r.Host, *initialize.QREndpoint, strconv.Itoa(*initialize.MaxSize), strconv.Itoa(*initialize.DefaultSize))))
}

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.17

require (
github.com/boombuler/barcode v1.0.1
github.com/prometheus/client_golang v1.11.0
github.com/sirupsen/logrus v1.8.1
)

Expand All @@ -12,7 +13,6 @@ require (
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/prometheus/client_golang v1.11.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
Expand Down Expand Up @@ -90,9 +91,9 @@ github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand All @@ -113,7 +114,6 @@ golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -125,6 +125,7 @@ golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
Expand All @@ -142,4 +143,5 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
44 changes: 24 additions & 20 deletions initialize/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,40 @@ package initialize
import (
"flag"

log "github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
)

var (
// Webserver Flags
PortNumber = flag.String("port", "8080", "The port number to listen on for HTTP requests.")
Address = flag.String("address", "0.0.0.0", "The address to listen on for HTTP requests.")
// Webserver Flags
PortNumber = flag.String("port", "8080", "The port number to listen on for HTTP requests.")
Host = flag.String("host", "0.0.0.0", "The host/address to listen on for HTTP requests.")

Https = flag.Bool("https", false, "Enable, or Disable HTTPS")
Server_crt = flag.String("server_crt", "server.crt", "Certificate file")
Server_key = flag.String("server_key", "server.key", "Certificate key file.")
Https = flag.Bool("https", false, "Enable, or Disable HTTPS")
Server_crt = flag.String("server_crt", "server.crt", "Certificate file")
Server_key = flag.String("server_key", "server.key", "Certificate key file.")

// QR Setup Flags
QREndpoint = flag.String("qr.endpoint", "/", "QR API endpoint location")
// QR Setup Flags
QREndpoint = flag.String("qr.endpoint", "/", "QR API endpoint location")

DefaultSize = flag.Int("qr.default.size", 250, "Default QR Image Size, if unspecified by end user.")
MaxSize = flag.Int("qr.max.size", 1000, "Maximum QR Image Size")
DefaultSize = flag.Int("qr.default.size", 250, "Default QR Image Size, if unspecified by end user.")
MaxSize = flag.Int("qr.max.size", 1000, "Maximum QR Image Size")

// Logging Flags
LogLevel = flag.String("log.level", "info", "The level of logs to log")
LogConn = flag.Bool("log.conn", true, "Log connections to API")
// Logging Flags
LogLevel = flag.String("log.level", "info", "The level of logs to log")
LogConn = flag.Bool("log.conn", true, "Log connections to API")

// Documentation Flags
EnableDocs = flag.Bool("enable.docs", true, "Enable documentation (/docs) endpoint.")
// Documentation Flags
EnableDocs = flag.Bool("enable.docs", true, "Enable documentation (/docs) endpoint.")

// Metrics Flagd
EnableMetrics = flag.Bool("enable.metrics", true, "Enable metrics (/metrics) endpoint.")
// Metrics Flags
EnableMetrics = flag.Bool("enable.metrics", true, "Enable metrics (/metrics) endpoint.")

// Misc Flags
VersionFlag = flag.Bool("v", false, "Outputs package version")
MetricServer = flag.Bool("enable.metrics.server", false, "Enable seperate metrics server")
MetricServerPort = flag.String("metrics.server.port", "9100", "The port number to listen on for metrics HTTP requests.")
MetricServerHost = flag.String("metrics.server.host", "0.0.0.0", "The host/address to listen on for metrics HTTP requests.")

// Misc Flags
VersionFlag = flag.Bool("v", false, "Outputs package version")
)

func Flags() {
Expand Down
8 changes: 3 additions & 5 deletions initialize/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import (
)

const (
Version string = "1.1.0"
MetricNamespace string = "qr_generator"
Version string = "1.2.0"
MetricNamespace string = "qr_generator"
)

var (
ApplicationName string = os.Args[0]

ApplicationName string = os.Args[0]
)

37 changes: 15 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import (

log "github.com/sirupsen/logrus"

"github.com/ashleyprimo/go-qr-generator/initialize"
"github.com/ashleyprimo/go-qr-generator/documentation"
"github.com/ashleyprimo/go-qr-generator/initialize"
"github.com/ashleyprimo/go-qr-generator/qr"
"github.com/ashleyprimo/go-qr-generator/documentation"

"github.com/prometheus/client_golang/prometheus/promhttp"
)

func loglevel(opt string) {
Expand All @@ -30,43 +28,38 @@ func loglevel(opt string) {
}

func health(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Ping."))
w.WriteHeader(http.StatusOK)
w.Write([]byte("Ping."))
}

func main() {
initialize.Flags()
initialize.Flags()

if *initialize.VersionFlag {
fmt.Printf("%s v%s", initialize.ApplicationName, initialize.Version)
os.Exit(0)
}
fmt.Printf("%s v%s", initialize.ApplicationName, initialize.Version)
os.Exit(0)
}

loglevel(*initialize.LogLevel)
metrics()

// QR Engine API Endpoint
http.HandleFunc(*initialize.QREndpoint, qr.Engine)

// Documentation Endpoint
if *initialize.EnableDocs {
log.Debugf("Documentation Endpoint Enabled")
http.HandleFunc("/docs", docs.Landing)
log.Debugf("Documentation Endpoint Enabled")
http.HandleFunc("/docs", docs.Landing)
}

// Health Check Endpoint
http.HandleFunc("/health", health)

// Metrics Endpoint
if *initialize.EnableMetrics {
log.Debugf("Metrics Endpoint Enabled")
http.Handle("/metrics", promhttp.Handler())
}
http.HandleFunc("/health", health)

log.Infof("Listening for requests on %s:%s", *initialize.Address, *initialize.PortNumber)
log.Infof("Listening for requests on %s:%s", *initialize.Host, *initialize.PortNumber)

if *initialize.Https {
log.Fatalf("Failed to start web server with TLS: %s", http.ListenAndServeTLS(fmt.Sprintf("%s:%s", *initialize.Address, *initialize.PortNumber), *initialize.Server_crt, *initialize.Server_key, nil))
log.Fatalf("Failed to start web server with TLS: %s", http.ListenAndServeTLS(fmt.Sprintf("%s:%s", *initialize.Host, *initialize.PortNumber), *initialize.Server_crt, *initialize.Server_key, nil))
} else {
log.Fatalf("Failed to start web server: %s", http.ListenAndServe(fmt.Sprintf("%s:%s", *initialize.Address, *initialize.PortNumber), nil))
log.Fatalf("Failed to start web server: %s", http.ListenAndServe(fmt.Sprintf("%s:%s", *initialize.Host, *initialize.PortNumber), nil))
}
}
48 changes: 40 additions & 8 deletions metrics.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,54 @@
package main

import (
"fmt"
"net/http"
"time"

log "github.com/sirupsen/logrus"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"

"github.com/ashleyprimo/go-qr-generator/initialize"
"github.com/ashleyprimo/go-qr-generator/initialize"
)

var (
versionExport = promauto.NewGauge(
prometheus.GaugeOpts{
Namespace: initialize.MetricNamespace,
Name: "version",
Help: "current running version",
versionExport = promauto.NewGauge(
prometheus.GaugeOpts{
Namespace: initialize.MetricNamespace,
Name: "version",
Help: "current running version",
ConstLabels: map[string]string{
"version": initialize.Version,
},
},
)
},
)
)

func metrics() {
// Metrics Endpoint
if *initialize.EnableMetrics {
log.Debugf("Metrics Endpoint Enabled")
if *initialize.MetricServer {
log.Debugf("Metrics Server Enabled")
log.Infof("Listening for metrics requests on %s:%s", *initialize.MetricServerHost, *initialize.MetricServerPort)
go func() {
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
srv := &http.Server{
Addr: fmt.Sprintf("%s:%s", *initialize.MetricServerHost, *initialize.MetricServerPort),
Handler: mux,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 3 * time.Minute,
}
log.Fatalf("Failed to start web server: %s", srv.ListenAndServe())
}()

} else {
http.Handle("/metrics", promhttp.Handler())
}
}
}
Loading

0 comments on commit 026f5a4

Please sign in to comment.