Skip to content

Commit

Permalink
Merge pull request #1098 from sundowndev/refact/api
Browse files Browse the repository at this point in the history
Put client and api to new web package
  • Loading branch information
sundowndev committed Jul 23, 2022
2 parents 269400d + f1f3933 commit fb98d99
Show file tree
Hide file tree
Showing 57 changed files with 330 additions and 306 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
node-version: 15.11.x

- name: Building static assets
run: (cd client && yarn install --immutable && yarn build)
run: (cd web/client && yarn install --immutable && yarn build)

- name: Enforce Go formatted code
run: |
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/client.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ jobs:
node-version: ${{ matrix.node-version }}
- name: Install & lint
run: |
cd client
cd web/client
yarn install --immutable
yarn lint
- name: Build
run: |
cd client
cd web/client
yarn build
- name: Test
run: |
cd client
cd web/client
yarn test:unit
# - name: Upload coverage to Codecov
# uses: codecov/codecov-action@v1.0.2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
go-version: 1.17.8

- name: Building static assets
run: (cd client && yarn install --immutable && yarn build)
run: (cd web/client && yarn install --immutable && yarn build)

- name: Install tools
run: make install-tools
Expand Down
10 changes: 0 additions & 10 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,3 @@ coverage
coverage.*
unit-tests.xml
.idea
api/assets.go

# Yarn
client/.pnp.*
client/.yarn/*
!client/.yarn/patches
!client/.yarn/plugins
!client/.yarn/releases
!client/.yarn/sdks
!client/.yarn/versions
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ lint:
install-tools:
$(GOINSTALL) gotest.tools/gotestsum@v1.6.3
$(GOINSTALL) github.com/vektra/mockery/v2@v2.8.0
$(GOINSTALL) github.com/jessevdk/go-assets-builder@latest
$(GOINSTALL) github.com/swaggo/swag/cmd/swag@v1.7.0

go.mod: FORCE
Expand Down
7 changes: 5 additions & 2 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/gin-gonic/gin"
"github.com/spf13/cobra"
"github.com/sundowndev/phoneinfoga/v2/api"
"github.com/sundowndev/phoneinfoga/v2/web"
)

var httpPort int
Expand All @@ -29,7 +29,10 @@ var serveCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
router := gin.Default()

api.Serve(router, disableClient)
_, err := web.Serve(router, disableClient)
if err != nil {
log.Fatal(err)
}

httpPort := ":" + strconv.Itoa(httpPort)

Expand Down
29 changes: 15 additions & 14 deletions docs/contribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ The project is maintained by a single person: [sundowndev](https://github.com/su
```shell
# Build static assets
# This will create dist directory containing client's static files
$ (cd client && yarn && yarn build)
$ (cd web/client && yarn && yarn build)

# Generate in-memory assets
# This will put content of dist directory in memory. It's usually needed to build but
Expand All @@ -42,12 +42,16 @@ $ go run main.go
### File structure

```shell
api # REST API code
client # web client code
cmd # Command-line app code
docs # Documentation
pkg # Code base for scanners, utils ...
scripts # Development & deployment scripts
bin/ # Local binaries
build/ # Build package providing info about the current build
cmd/ # Command-line app code
docs/ # Documentation
examples/ # Some code examples
lib/ # Libraries
mocks/ # Mocks
support/ # Utilities, manifests for production and local env
test/ # Utilities for testing purposes
web/ # Web server, including REST API and web client
go.mod # Go modules file
main.go # Application entrypoint
```
Expand All @@ -72,7 +76,7 @@ go tool cover -html=coverage.out
Developping on the web client.

```shell
cd client
cd web/client

yarn test
yarn test:unit
Expand All @@ -85,19 +89,16 @@ If you're developing on the client, you can watch changes with `yarn build:watch

### Go code

We use a shell script to format Go files.
We use gofmt to format Go files.

```shell
sh ./scripts/format.sh

# You can also use GolangCI
golangci-lint run -D errcheck
make fmt
```

### Typescript code

```shell
cd client
cd web/client

yarn lint
yarn lint:fix
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/fatih/color v1.10.0
github.com/gin-gonic/gin v1.6.3
github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15
github.com/nyaruka/phonenumbers v1.0.66
github.com/onlinecity/go-phone-iso3166 v0.0.1
github.com/sirupsen/logrus v1.8.1
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2p
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15 h1:cW/amwGEJK5MSKntPXRjX4dxs/nGxGT8gXKIsKFmHGc=
github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15/go.mod h1:Fdm/oWRW+CH8PRbLntksCNtmcCBximKPkVQYvmMl80k=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
Expand Down
72 changes: 72 additions & 0 deletions web/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package web

import (
"embed"
"fmt"
"github.com/gin-gonic/gin"
"io/ioutil"
"mime"
"net/http"
"path"
"strings"
)

//go:embed client/dist/*
var clientFS embed.FS

const (
staticPath = "/"
)

func detectContentType(path string, data []byte) string {
arr := strings.Split(path, ".")
ext := arr[len(arr)-1]

if mimeType := mime.TypeByExtension(fmt.Sprintf(".%s", ext)); mimeType != "" {
return mimeType
}

return http.DetectContentType(data)
}

func walkEmbededClient(dir string, router *gin.Engine) error {
files, err := clientFS.ReadDir(dir)
if err != nil {
return err
}
for _, file := range files {
if file.IsDir() {
err := walkEmbededClient(path.Join(dir, file.Name()), router)
if err != nil {
return err
}
continue
}

assetPath := strings.ReplaceAll(path.Join(dir, file.Name()), "client/dist/", staticPath)
f, err := clientFS.Open(path.Join(dir, file.Name()))
if err != nil {
return err
}
data, err := ioutil.ReadAll(f)
if err != nil {
return err
}

if assetPath == staticPath+"index.html" {
assetPath = staticPath
}

router.GET(assetPath, func(c *gin.Context) {
c.Header("Content-Type", detectContentType(assetPath, data))
c.Writer.WriteHeader(http.StatusOK)
_, _ = c.Writer.Write(data)
c.Abort()
})
}
return nil
}

func registerClientRoute(router *gin.Engine) error {
return walkEmbededClient("client/dist", router)
}
3 changes: 3 additions & 0 deletions client/.gitignore → web/client/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ yarn-error.log*
*.njsproj
*.sln
*.sw?
.yarn/cache
.yarn/*.gz
!.yarn/releases/*.cjs
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions api/controllers.go → web/controllers.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package api
package web

import (
"github.com/gin-gonic/gin"
"github.com/sundowndev/phoneinfoga/v2/api/errors"
"github.com/sundowndev/phoneinfoga/v2/build"
"github.com/sundowndev/phoneinfoga/v2/lib/number"
"github.com/sundowndev/phoneinfoga/v2/lib/remote"
"github.com/sundowndev/phoneinfoga/v2/lib/remote/suppliers"
"github.com/sundowndev/phoneinfoga/v2/web/errors"
"net/http"
)

Expand Down
Loading

0 comments on commit fb98d99

Please sign in to comment.