Skip to content

Commit

Permalink
Merge branch 'add-go-qol-lint' into 'main'
Browse files Browse the repository at this point in the history
feat: add Go quality of life lint jobs

See merge request BobyMCbobs/go-http-server!23
  • Loading branch information
BobyMCbobs committed Jul 25, 2023
2 parents 00ce055 + dce2fe4 commit 22225f8
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 14 deletions.
52 changes: 51 additions & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ variables:
ARCHES: amd64 arm64
APP_BUILD_VERSION: 0.0.0
APP_BUILD_MODE: development
IMAGE_KO: ghcr.io/ko-build/ko:v0.13.0
IMAGE_KO: ghcr.io/ko-build/ko:v0.14.1
IMAGE_GOLANG_ALPINE: docker.io/golang:1.20.5-alpine3.18
IMAGE_PYTHON: python:3.8-buster
IMAGE_GOLANGCI_LINT: docker.io/golangci/golangci-lint:v1.53.3
VERSION_COSIGN: v2.0.2
COSIGN_YES: "true"

Expand All @@ -36,6 +38,12 @@ variables:
curl -L https://github.com/sigstore/cosign/releases/download/$VERSION_COSIGN/cosign-linux-amd64 -o /usr/local/bin/cosign
chmod +x /usr/local/bin/cosign

build-backend-smoketest:
stage: build
image: $IMAGE_GOLANG_ALPINE
script:
- go build -o /dev/null .

build-container:
stage: build
image:
Expand Down Expand Up @@ -85,6 +93,48 @@ lint_backend:
script:
- golint -set_exit_status ./...

govet:
stage: lint
image: $IMAGE_GOLANG_ALPINE
script:
- go vet -v ./...

gofmt:
stage: lint
image: $IMAGE_GOLANG_ALPINE
script:
- find . -name "*.go" | grep -E -v vendor | xargs gofmt -s -l -d -w -s

goimports:
stage: lint
image: $IMAGE_GOLANG_ALPINE
before_script:
- apk add --no-cache git
- go install golang.org/x/tools/cmd/goimports@latest
script:
- find . -type f -name '*.go' -not -path './vendor/*' | xargs -I{} goimports -w {}
- |
if git diff --name-only --diff-filter=ACMRT | grep -E '(.*).go$'; then
echo "error: changes detected, run 'find . -type f -name '*.go' -not -path './vendor/*' | xargs -I{} goimports -w {}'"
exit 1
fi
golangci-lint:
stage: lint
image:
name: $IMAGE_GOLANGCI_LINT
entrypoint: [""]
script:
- golangci-lint run

govulncheck:
stage: lint
image: $IMAGE_GOLANG_ALPINE
before_script:
- go install golang.org/x/vuln/cmd/govulncheck@latest
script:
- govulncheck ./...

pages:
image: $IMAGE_PYTHON
stage: pages
Expand Down
2 changes: 2 additions & 0 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ func GetRedirectRoutesPath() (output string) {
return GetEnvOrDefault("APP_REDIRECT_ROUTES_PATH", "./redirects.yaml")
}

// GetHTTPAllowedOrigins ...
// returns a list of specified allowed origins for configuring CORS
func GetHTTPAllowedOrigins() (origins []string) {
for _, o := range strings.Split(GetEnvOrDefault("APP_HTTP_ALLOWED_ORIGINS", "*"), ",") {
if o == "" {
Expand Down
6 changes: 3 additions & 3 deletions pkg/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (h *Handler) serveHandlerVuejsHistoryMode() http.Handler {
handler := http.FileServer(http.Dir(h.ServeFolder))

return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if h.HeaderMapEnabled == true {
if h.HeaderMapEnabled {
w = common.WriteHeadersToResponse(w, h.HeaderMap)
}
isDisallowed := false
Expand Down Expand Up @@ -75,7 +75,7 @@ func (h *Handler) serveHandlerStandard() http.Handler {
handler := http.FileServer(http.Dir(h.ServeFolder))

return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if h.HeaderMapEnabled == true {
if h.HeaderMapEnabled {
w = common.WriteHeadersToResponse(w, h.HeaderMap)
}
isDisallowed := false
Expand All @@ -97,7 +97,7 @@ func (h *Handler) serveHandlerStandard() http.Handler {
// serves a folder
func (h *Handler) ServeHandler() (handler http.Handler) {
switch {
case h.VueJSHistoryMode == true:
case h.VueJSHistoryMode:
handler = h.serveHandlerVuejsHistoryMode()
default:
handler = h.serveHandlerStandard()
Expand Down
14 changes: 9 additions & 5 deletions pkg/httpserver/httpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,13 @@ func NewWebServer() *WebServer {
}
if w.HTTPSPortEnabled {
w.LoadTLS()
*w.serverTLS = *w.server
w.serverTLS.TLSConfig = w.TLSConfig
w.serverTLS = &http.Server{
Handler: c.Handler(router),
Addr: w.AppPort,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
TLSConfig: w.TLSConfig,
}
}

return w
Expand Down Expand Up @@ -201,13 +206,12 @@ func (w *WebServer) LoadTLS() *WebServer {
log.Panicf("[fatal] Error loading certs: %v\n", err)
}
w.TLSConfig.Certificates[0] = loadedCert
w.TLSConfig.BuildNameToCertificate()
return w
}

// LoadTemplateMap loads the template map from the path
func (w *WebServer) LoadTemplateMap() *WebServer {
if w.VueJSHistoryMode != true {
if w.VueJSHistoryMode {
return w
}
if w.TemplateMap == nil {
Expand All @@ -234,7 +238,7 @@ func (w *WebServer) SetTemplateMap(input map[string]string) *WebServer {

// LoadHeaderMap loads the header map from the path
func (w *WebServer) LoadHeaderMap() *WebServer {
if w.HeaderMapEnabled == false {
if !w.HeaderMapEnabled {
return w
}
if w.HeaderMap == nil {
Expand Down
39 changes: 34 additions & 5 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package metrics

import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"

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

// Metrics configures the metrics handler
Expand All @@ -15,11 +22,33 @@ type Metrics struct {
// Handle ...
// HTTP handler for metrics
func (m *Metrics) Handle() {
if m.Enabled == false {
if !m.Enabled {
return
}

http.Handle("/metrics", promhttp.Handler())
log.Printf("Metrics listening on %v\n", m.Port)
http.ListenAndServe(m.Port, nil)
router := mux.NewRouter().StrictSlash(true)
r := router.Handle("/metrics", promhttp.Handler())
server := &http.Server{
Handler: r.GetHandler(),
Addr: m.Port,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
ReadHeaderTimeout: 10 * time.Second,
}
log.Printf("Metrics listening on %v\n", server.Addr)
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

go func() {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
}()

<-done
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatalf("Server didn't exit gracefully %v", err)
}
}

0 comments on commit 22225f8

Please sign in to comment.