Skip to content

Commit

Permalink
[patch] removed platform and introduced pkg
Browse files Browse the repository at this point in the history
[patch] added info about SQL code generation
[patch] added info about SQL migrations
[patch] introduced Open telemetry usage (only in README, todo: update code)
  • Loading branch information
bnkamalesh committed Feb 22, 2022
1 parent 4a3e0b6 commit cf7161d
Show file tree
Hide file tree
Showing 21 changed files with 595 additions and 446 deletions.
41 changes: 23 additions & 18 deletions README.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ go 1.17

require (
github.com/Masterminds/squirrel v1.5.1
github.com/bnkamalesh/errors v0.4.0
github.com/bnkamalesh/webgo/v6 v6.2.2
github.com/bnkamalesh/errors v0.9.0
github.com/bnkamalesh/webgo/v6 v6.3.1
github.com/gomodule/redigo v1.8.5
github.com/jackc/pgx/v4 v4.13.0
go.elastic.co/apm v1.14.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI=
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/bnkamalesh/errors v0.4.0 h1:xVnTXXpNRYDCA0o6mI+C0yMxDkdvOQ0w7ead3sIKmsE=
github.com/bnkamalesh/errors v0.4.0/go.mod h1:FQgYzv2aq3DI5JNr+ICLl0PbPokCJY56C/+mhZ8TpaU=
github.com/bnkamalesh/errors v0.9.0 h1:O3upfWzpkCwiC9Bz4D5tmpp64ZFLZ1+ILVdM+Z/xE8o=
github.com/bnkamalesh/errors v0.9.0/go.mod h1:FQgYzv2aq3DI5JNr+ICLl0PbPokCJY56C/+mhZ8TpaU=
github.com/bnkamalesh/webgo/v6 v6.2.2 h1:uF51yYoiqPOXqPkg0Jmhq0wDb2STRY0xSktgDiqvqsU=
github.com/bnkamalesh/webgo/v6 v6.2.2/go.mod h1:2Y+dEdTp1xC/ra+3PAVZV6hh4sCI+iPK7mcHt+t9bfM=
github.com/bnkamalesh/webgo/v6 v6.3.1 h1:GtZWFrl48YQ1oKKgxhAcKRAK3yAcph1zfAniYIIYunQ=
github.com/bnkamalesh/webgo/v6 v6.3.1/go.mod h1:2Y+dEdTp1xC/ra+3PAVZV6hh4sCI+iPK7mcHt+t9bfM=
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
Expand Down
2 changes: 1 addition & 1 deletion internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package api
import (
"time"

"github.com/bnkamalesh/goapp/internal/platform/logger"
"github.com/bnkamalesh/goapp/internal/pkg/logger"
"github.com/bnkamalesh/goapp/internal/users"
)

Expand Down
4 changes: 2 additions & 2 deletions internal/configs/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strings"
"time"

"github.com/bnkamalesh/goapp/internal/platform/cachestore"
"github.com/bnkamalesh/goapp/internal/platform/datastore"
"github.com/bnkamalesh/goapp/internal/pkg/cachestore"
"github.com/bnkamalesh/goapp/internal/pkg/datastore"
"github.com/bnkamalesh/goapp/internal/server/http"
)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
133 changes: 133 additions & 0 deletions internal/server/http/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package http

import (
"bytes"
"fmt"
"html/template"
"net/http"
"runtime/debug"

"github.com/bnkamalesh/errors"
"github.com/bnkamalesh/goapp/internal/api"
"github.com/bnkamalesh/webgo/v6"
)

// Handlers struct has all the dependencies required for HTTP handlers
type Handlers struct {
api *api.API
home *template.Template
}

func (h *Handlers) routes() []*webgo.Route {
return []*webgo.Route{
{
Name: "helloworld",
Pattern: "",
Method: http.MethodGet,
Handlers: []http.HandlerFunc{errWrapper(h.HelloWorld)},
TrailingSlash: true,
},
{
Name: "health",
Pattern: "/-/health",
Method: http.MethodGet,
Handlers: []http.HandlerFunc{errWrapper(h.Health)},
TrailingSlash: true,
},
{
Name: "create-user",
Pattern: "/users",
Method: http.MethodPost,
Handlers: []http.HandlerFunc{errWrapper(h.CreateUser)},
TrailingSlash: true,
},
{
Name: "read-user-byemail",
Pattern: "/users/:email",
Method: http.MethodGet,
Handlers: []http.HandlerFunc{errWrapper(h.ReadUserByEmail)},
TrailingSlash: true,
},
}
}

// Health is the HTTP handler to return the status of the app including the version, and other details
// This handler uses webgo to respond to the http request
func (h *Handlers) Health(w http.ResponseWriter, r *http.Request) error {
out, err := h.api.Health()
if err != nil {
return err
}
webgo.R200(w, out)
return nil
}

// HelloWorld is a helloworld HTTP handler
func (h *Handlers) HelloWorld(w http.ResponseWriter, r *http.Request) error {
contentType := r.Header.Get("Content-Type")
switch contentType {
case "application/json":
{
webgo.SendResponse(w, "hello world", http.StatusOK)
}
default:
{
buff := bytes.NewBufferString("")
err := h.home.Execute(
buff,
struct {
Message string
}{
Message: "welcome to the home page!",
},
)
if err != nil {
return errors.InternalErr(err, "Inter server error")
}

w.Header().Set("Content-Type", "text/html; charset=UTF-8")
_, err = w.Write(buff.Bytes())
if err != nil {
return err
}
}
}
return nil
}

func errWrapper(h func(w http.ResponseWriter, r *http.Request) error) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
err := h(w, r)
if err == nil {
return
}

status, msg, _ := errors.HTTPStatusCodeMessage(err)
webgo.SendError(w, msg, status)
}
}

func panicRecoverer(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
defer func() {
p := recover()
if p == nil {
return
}
fmt.Println(string(debug.Stack()))
webgo.R500(w, errors.DefaultMessage)
}()

next(w, r)
}

func loadHomeTemplate(basePath string) (*template.Template, error) {
t := template.New("index.html")
home, err := t.ParseFiles(
fmt.Sprintf("%s/index.html", basePath),
)
if err != nil {
return nil, err
}

return home, nil
}
23 changes: 11 additions & 12 deletions internal/server/http/handlers_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,45 @@ import (
"net/http"

"github.com/bnkamalesh/errors"
"github.com/bnkamalesh/goapp/internal/users"
"github.com/bnkamalesh/webgo/v6"

"github.com/bnkamalesh/goapp/internal/users"
)

// CreateUser is the HTTP handler to create a new user
// This handler does not use any framework, instead just the standard library
func (h *Handlers) CreateUser(w http.ResponseWriter, r *http.Request) {
func (h *Handlers) CreateUser(w http.ResponseWriter, r *http.Request) error {
u := new(users.User)
err := json.NewDecoder(r.Body).Decode(u)
if err != nil {
errResponder(w, errors.InputBodyErr(err, "invalid JSON provided"))
return
return errors.InputBodyErr(err, "invalid JSON provided")
}

createdUser, err := h.api.CreateUser(r.Context(), u)
if err != nil {
errResponder(w, err)
return
return err
}

b, err := json.Marshal(createdUser)
if err != nil {
errResponder(w, err)
return
return err
}

w.Header().Set("Content-Type", "application/json")
w.Write(b)
_, err = w.Write(b)
return err
}

// ReadUserByEmail is the HTTP handler to read an existing user by email
func (h *Handlers) ReadUserByEmail(w http.ResponseWriter, r *http.Request) {
func (h *Handlers) ReadUserByEmail(w http.ResponseWriter, r *http.Request) error {
wctx := webgo.Context(r)
email := wctx.Params()["email"]

out, err := h.api.ReadUserByEmail(r.Context(), email)
if err != nil {
errResponder(w, err)
return
return err
}

webgo.R200(w, out)
return nil
}
112 changes: 0 additions & 112 deletions internal/server/http/http.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package http

import (
"bytes"
"fmt"
"html/template"
"net/http"
"runtime/debug"
"time"

"github.com/bnkamalesh/errors"
"github.com/bnkamalesh/webgo/v6"
"github.com/bnkamalesh/webgo/v6/middleware/accesslog"
"go.elastic.co/apm"
Expand All @@ -17,89 +13,6 @@ import (
"github.com/bnkamalesh/goapp/internal/api"
)

func errResponder(w http.ResponseWriter, err error) {
status, msg, _ := errors.HTTPStatusCodeMessage(err)
webgo.SendError(w, msg, status)
}

// Handlers struct has all the dependencies required for HTTP handlers
type Handlers struct {
api *api.API
home *template.Template
}

func (h *Handlers) routes() []*webgo.Route {
return []*webgo.Route{
&webgo.Route{
Name: "helloworld",
Pattern: "",
Method: http.MethodGet,
Handlers: []http.HandlerFunc{h.HelloWorld},
TrailingSlash: true,
},
&webgo.Route{
Name: "health",
Pattern: "/-/health",
Method: http.MethodGet,
Handlers: []http.HandlerFunc{h.Health},
TrailingSlash: true,
},
&webgo.Route{
Name: "create-user",
Pattern: "/users",
Method: http.MethodPost,
Handlers: []http.HandlerFunc{h.CreateUser},
TrailingSlash: true,
},
&webgo.Route{
Name: "read-user-byemail",
Pattern: "/users/:email",
Method: http.MethodGet,
Handlers: []http.HandlerFunc{h.ReadUserByEmail},
TrailingSlash: true,
},
}
}

// Health is the HTTP handler to return the status of the app including the version, and other details
// This handler uses webgo to respond to the http request
func (h *Handlers) Health(w http.ResponseWriter, r *http.Request) {
out, err := h.api.Health()
if err != nil {
errResponder(w, err)
return
}
webgo.R200(w, out)
}

// HelloWorld is a helloworld HTTP handler
func (h *Handlers) HelloWorld(w http.ResponseWriter, r *http.Request) {
contentType := r.Header.Get("Content-Type")
switch contentType {
case "application/json":
{
webgo.SendResponse(w, "hello world", http.StatusOK)
}
default:
{
buff := bytes.NewBufferString("")
err := h.home.Execute(
buff,
struct {
Message string
}{
Message: "welcome to the home page!",
},
)
if err != nil {
webgo.Send(w, contentType, err.Error(), http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "text/html; charset=UTF-8")
w.Write(buff.Bytes())
}
}
}

// HTTP struct holds all the dependencies required for starting HTTP server
type HTTP struct {
server *http.Server
Expand All @@ -122,31 +35,6 @@ type Config struct {
DialTimeout time.Duration
}

func panicRecoverer(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
defer func() {
p := recover()
if p == nil {
return
}
fmt.Println(string(debug.Stack()))
webgo.R500(w, errors.DefaultMessage)
}()

next(w, r)
}

func loadHomeTemplate(basePath string) (*template.Template, error) {
t := template.New("index.html")
home, err := t.ParseFiles(
fmt.Sprintf("%s/index.html", basePath),
)
if err != nil {
return nil, err
}

return home, nil
}

// NewService returns an instance of HTTP with all its dependencies set
func NewService(cfg *Config, a *api.API) (*HTTP, error) {
home, err := loadHomeTemplate(cfg.TemplatesBasePath)
Expand Down
4 changes: 2 additions & 2 deletions internal/users/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"encoding/json"
"fmt"

"github.com/bnkamalesh/errors"
"github.com/gomodule/redigo/redis"

"github.com/bnkamalesh/errors"
"github.com/bnkamalesh/goapp/internal/platform/cachestore"
"github.com/bnkamalesh/goapp/internal/pkg/cachestore"
)

type userCachestore interface {
Expand Down
4 changes: 2 additions & 2 deletions internal/users/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/gomodule/redigo/redis"
"github.com/jackc/pgx/v4/pgxpool"

"github.com/bnkamalesh/goapp/internal/platform/cachestore"
"github.com/bnkamalesh/goapp/internal/platform/logger"
"github.com/bnkamalesh/goapp/internal/pkg/cachestore"
"github.com/bnkamalesh/goapp/internal/pkg/logger"
)

// User holds all data required to represent a user
Expand Down
Loading

0 comments on commit cf7161d

Please sign in to comment.