-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: major refactor which introduces new practices (#5)
[major] v1.0 1. Removed `vendor` directory for Go 2. Introduced interface in API layer for different cmd implementations (server, subscriber, cli etc.) 3. Using OTEL for instrumentation 4. Upgraded all dependencies, including Go version 5. Updated docs to use gonew instead of custom bash script for reusing goapp to start a new project 6. Updated overall README to possibly make things easier to understand and clearer
- Loading branch information
1 parent
17d30c0
commit af12a99
Showing
1,255 changed files
with
1,880 additions
and
460,636 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package grpc | ||
|
||
import "github.com/bnkamalesh/goapp/internal/api" | ||
|
||
type GRPC struct { | ||
apis api.Server | ||
} | ||
|
||
func New(apis api.Server) *GRPC { | ||
return &GRPC{ | ||
apis: apis, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package http | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/bnkamalesh/errors" | ||
"github.com/bnkamalesh/goapp/internal/usernotes" | ||
"github.com/bnkamalesh/webgo/v7" | ||
) | ||
|
||
func (h *Handlers) CreateUserNote(w http.ResponseWriter, r *http.Request) error { | ||
unote := new(usernotes.Note) | ||
err := json.NewDecoder(r.Body).Decode(unote) | ||
if err != nil { | ||
return errors.InputBodyErr(err, "invalid JSON provided") | ||
} | ||
|
||
un, err := h.apis.CreateUserNote(r.Context(), unote) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
webgo.R200(w, un) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package http | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/bnkamalesh/errors" | ||
"github.com/bnkamalesh/goapp/internal/api" | ||
"github.com/bnkamalesh/goapp/internal/pkg/apm" | ||
"github.com/bnkamalesh/webgo/v7" | ||
"github.com/bnkamalesh/webgo/v7/middleware/accesslog" | ||
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" | ||
) | ||
|
||
// Config holds all the configuration required to start the HTTP server | ||
type Config struct { | ||
Host string | ||
Port uint16 | ||
|
||
ReadTimeout time.Duration | ||
WriteTimeout time.Duration | ||
DialTimeout time.Duration | ||
|
||
TemplatesBasePath string | ||
EnableAccessLog bool | ||
} | ||
|
||
type HTTP struct { | ||
listener string | ||
server *webgo.Router | ||
} | ||
|
||
// Start starts the HTTP server | ||
func (h *HTTP) Start() error { | ||
h.server.Start() | ||
return nil | ||
} | ||
|
||
func (h *HTTP) Shutdown(ctx context.Context) error { | ||
err := h.server.Shutdown() | ||
if err != nil { | ||
return errors.Wrap(err, "failed shutting down HTTP server") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// NewService returns an instance of HTTP with all its dependencies set | ||
func NewService(cfg *Config, apis api.Server) (*HTTP, error) { | ||
home, err := loadHomeTemplate(cfg.TemplatesBasePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
handlers := &Handlers{ | ||
apis: apis, | ||
home: home, | ||
} | ||
|
||
router := webgo.NewRouter( | ||
&webgo.Config{ | ||
Host: cfg.Host, | ||
Port: strconv.Itoa(int(cfg.Port)), | ||
ReadTimeout: cfg.ReadTimeout, | ||
WriteTimeout: cfg.WriteTimeout, | ||
ShutdownTimeout: cfg.WriteTimeout * 2, | ||
}, | ||
handlers.routes()..., | ||
) | ||
|
||
if cfg.EnableAccessLog { | ||
router.Use(accesslog.AccessLog) | ||
router.UseOnSpecialHandlers(accesslog.AccessLog) | ||
} | ||
router.Use(panicRecoverer) | ||
|
||
otelopts := []otelhttp.Option{ | ||
// in this app, /-/ prefixed routes are used for healthchecks, readiness checks etc. | ||
otelhttp.WithFilter(func(req *http.Request) bool { | ||
return !strings.HasPrefix(req.URL.Path, "/-/") | ||
}), | ||
// the span name formatter is used to reduce the cardinality of metrics generated | ||
// when using URIs with variables in it | ||
otelhttp.WithSpanNameFormatter(func(operation string, r *http.Request) string { | ||
wctx := webgo.Context(r) | ||
if wctx == nil { | ||
return r.URL.Path | ||
} | ||
return wctx.Route.Pattern | ||
}), | ||
} | ||
|
||
apmMw := apm.NewHTTPMiddleware(otelopts...) | ||
router.Use(func(w http.ResponseWriter, r *http.Request, hf http.HandlerFunc) { | ||
apmMw(hf).ServeHTTP(w, r) | ||
}) | ||
|
||
return &HTTP{ | ||
server: router, | ||
listener: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Hello world</title> | ||
<style> | ||
html, | ||
body { | ||
font-size: 16px; | ||
line-height: 1.5em; | ||
min-width: 100vw; | ||
min-height: 100vh; | ||
} | ||
body { | ||
font-family: sans-serif; | ||
background-color: #efefef; | ||
color: #222; | ||
height: 100vh; | ||
width: 100vw; | ||
overflow: hidden; | ||
} | ||
main { | ||
margin: 40vh auto 0; | ||
width: 35rem; | ||
} | ||
h1, | ||
h2, | ||
h3, | ||
h4, | ||
h5, | ||
h6 { | ||
font-family: "Roboto", sans-serif; | ||
font-weight: 400; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<main> | ||
<h3 id="colorize" style="text-align: center">{{.Message}}</h3> | ||
</main> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Package kafka implements the Kafka subscription functionality | ||
package kafka | ||
|
||
import "github.com/bnkamalesh/goapp/internal/api" | ||
|
||
type Kafka struct { | ||
apis api.Subscriber | ||
} | ||
|
||
func New(apis api.Subscriber) *Kafka { | ||
return &Kafka{ | ||
apis: apis, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM golang:1.20 AS builder | ||
FROM golang:1.22 AS builder | ||
|
||
COPY ../ /app | ||
WORKDIR /app | ||
|
@@ -13,10 +13,10 @@ LABEL MAINTAINER Author <[email protected]> | |
|
||
# Following commands are for installing CA certs (for proper functioning of HTTPS and other TLS) | ||
RUN apt-get update && apt-get install -y --no-install-recommends \ | ||
ca-certificates \ | ||
netbase \ | ||
&& rm -rf /var/lib/apt/lists/ \ | ||
&& apt-get autoremove -y && apt-get autoclean -y | ||
ca-certificates \ | ||
netbase \ | ||
&& rm -rf /var/lib/apt/lists/ \ | ||
&& apt-get autoremove -y && apt-get autoclean -y | ||
|
||
# Add new user 'appuser'. App should be run without root privileges as a security measure | ||
RUN adduser --home "/appuser" --disabled-password appuser \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.