Skip to content
This repository has been archived by the owner on Jun 2, 2023. It is now read-only.

Commit

Permalink
Enables all other routers (#5)
Browse files Browse the repository at this point in the history
* Remove unsupported flag see: golang/go#50501
* Sync general project structure from mp3binder
* Introduce a simple health endpoint
* Simple health service
* Change permanent redirect to temporary
* Fixes a known issue with httprouter
* Fixes parameter access for httprouter
* Updates inspiration references and golang 1.18b2
* Closed todo about other routers
  • Loading branch information
crra committed Feb 10, 2022
1 parent 7beecfc commit bc49d46
Show file tree
Hide file tree
Showing 20 changed files with 722 additions and 137 deletions.
22 changes: 18 additions & 4 deletions .golangci.yaml
Expand Up @@ -27,22 +27,32 @@ linters:
enable:
- asciicheck
- bodyclose
- cyclop
- deadcode
- decorder
- depguard
- depguard
- dogsled
- dupl
- dupl
- errcheck
- errname
- errorlint
- exhaustive
- funlen
- go-critic
- gochecknoglobals
- gochecknoinits
- gocognit
- gocognit
- goconst
- goconst
- gocritic
- gocognit
- gocyclo
- godot
- godox
- godox
- goerr113
- errorlint
- gofumpt
- goimports
- golint
Expand All @@ -51,15 +61,19 @@ linters:
- gosec
- gosimple
- govet
- ineffassign
- ifshort
- ifshort
- importas
- ineffassign
- ireturn
- lll
- maintidx
- megacheck
- misspell
- nakedret
- nestif
- noctx
- nolintlint
- nestif
- rowserrcheck
- scopelint
- staticcheck
Expand Down
29 changes: 17 additions & 12 deletions README.md
Expand Up @@ -6,26 +6,31 @@ Tries to give an answer if in an example code one can find: "error handling omit

The service represents a very simple URL shortener service. Offers basic CRD (create, read, delete) operations via REST.

Based on the series from [tensor-programming](https://github.com/tensor-programming/hex-microservice.git) and recommendations from [Kat Zień](https://github.com/katzien/go-structure-examples).
Based on the series from [tensor-programming](https://github.com/tensor-programming/hex-microservice.git) and recommendations from:

- [How do you structure your Go apps? - Kat Zień](https://github.com/katzien/go-structure-examples)
- [Improving the code from the official Go RESTful API tutorial - Ben Hoyt](https://benhoyt.com/writings/web-service-stdlib/)

# Disclaimer

The implementation in this repository ~~could be a little~~ _is_ over-engineered for such a simple project. It exists mostly for its educational purpose and as an experiment to break with traditional approaches (e.g. the active record pattern, ORM, storing JSON in redis, coupling of the domain and the entities).

# Golang 1.18

As of today (January, 2022), golang 1.18 with generics is not yet released. `gotip` installs the latest go build (which includes 1.18):
## Golang 1.18

[Source](https://gist.github.com/nikgalushko/e1b5c85c64653dd554a7a904bbef4eee):
As of today (January, 2022), golang 1.18 with generics is not yet released, but betas are available. Please refer to: https://go.dev/blog/go1.18beta2

## Install gotip
### Install gotip

```
go install golang.org/dl/gotip@latest
gotip download
go install golang.org/dl/go1.18beta2@latest
go1.18beta2 download
```

## Install latest gopls
### Install latest gopls

1. Either in Codium / VS Studio Code: "Go: Install/Update Tools"

2. Or manually. Example for POSIX based systems:

```
mkdir /tmp/gopls && cd "$_"
Expand All @@ -34,11 +39,11 @@ gotip get golang.org/x/tools/gopls@master golang.org/x/tools@master
gotip install golang.org/x/tools/gopls
```

## Configure VSCode
### Configure VSCode/Codium

1. View > Command Palette
2. Go: choose Go environment
3. select gotip
3. select go1.18beta2

# Structure of the project

Expand Down Expand Up @@ -98,7 +103,7 @@ Another architectural style was defined by Jeffrey Palermo a few years later in
# Todo and Ideas

- implement and test mongo backend
- implement and test other routers than `chi`
- ~~implement and test other routers than `chi`~~
- implement the code generator that creates the conversion code that performs the conversion without runtime inspection (reflection)
- compare this custom golang lib version (this) with an existing framework like spring boot (e.g. input validation)
- handle key collisions
Expand Down
7 changes: 4 additions & 3 deletions Taskfile.yml
@@ -1,5 +1,7 @@
version: "3"

dotenv: ["taskfile.env"]

vars:
FOLDER_DIST: "dist"

Expand Down Expand Up @@ -81,7 +83,7 @@ tasks:
test:
desc: Perform all tests
cmds:
- go test -cover -race ./...
- ${GO} test -cover -race ./...

build:
desc: Build the binary for the current architecture.
Expand Down Expand Up @@ -122,14 +124,13 @@ tasks:
cmds:
- mkdir -p "{{.FOLDER}}"
- >-
go build -trimpath
CGO_ENABLED=1 ${GO} build -trimpath
-ldflags="-w -s
-X main.name="{{.NAME}}{{.EXTENSION}}"
-X main.version="{{.GIT_COMMIT}}"
-X main.realm="{{.REALM}}"
-extldflags '-static'" -a
-buildvcs=false
-buildinfo=false
-o {{.OUTPUT}}
{{.MAIN}}
Expand Down
32 changes: 0 additions & 32 deletions cmd/service/chi.go

This file was deleted.

38 changes: 38 additions & 0 deletions cmd/service/chirouter/chi.go
@@ -0,0 +1,38 @@
package chirouter

import (
"fmt"
"hex-microservice/adder"
"hex-microservice/deleter"
"hex-microservice/health"
"hex-microservice/http/rest"
"hex-microservice/lookup"
"net/http"

chi "github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-logr/logr"
)

// New returns a http.Handler that exposes the service with the chi router.
func New(log logr.Logger, mappedURL string, h health.Service, a adder.Service, l lookup.Service, d deleter.Service) http.Handler {
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(middleware.StripSlashes)

r.NotFound(http.NotFound)
r.MethodNotAllowed(http.NotFound)

s := rest.New(log, h, a, l, d, chi.URLParam)

r.Get("/health", s.Health())

r.Get(fmt.Sprintf("/{%s}", rest.UrlParameterCode), s.RedirectGet(mappedURL))
r.Post("/", s.RedirectPost(mappedURL))
r.Delete(fmt.Sprintf("/{%s}/{%s}", rest.UrlParameterCode, rest.UrlParameterToken), s.RedirectDelete(mappedURL))

return r
}
23 changes: 0 additions & 23 deletions cmd/service/gorillamux.go

This file was deleted.

0 comments on commit bc49d46

Please sign in to comment.