|
1 | 1 | # net/http backend template for [Create Go App CLI](https://github.com/create-go-app/cli) |
2 | 2 |
|
3 | | -<img src="https://img.shields.io/badge/Go-1.11+-00ADD8?style=for-the-badge&logo=go" alt="go version" /> <a href="https://gocover.io/github.com/create-go-app/net_http-go-template/pkg/apiserver" target="_blank"><img src="https://img.shields.io/badge/Go_Cover-49%25-success?style=for-the-badge&logo=none" alt="go cover" /></a> <a href="https://goreportcard.com/report/github.com/create-go-app/net_http-go-template" target="_blank"><img src="https://img.shields.io/badge/Go_report-A+-success?style=for-the-badge&logo=none" alt="go report" /></a> <img src="https://img.shields.io/badge/license-mit-red?style=for-the-badge&logo=none" alt="license" /> |
| 3 | +<img src="https://img.shields.io/badge/Go-1.16+-00ADD8?style=for-the-badge&logo=go" alt="go version" /> <a href="https://goreportcard.com/report/github.com/create-go-app/net_http-go-template" target="_blank"><img src="https://img.shields.io/badge/Go_report-A+-success?style=for-the-badge&logo=none" alt="go report" /></a> <img src="https://img.shields.io/badge/license-mit-red?style=for-the-badge&logo=none" alt="license" /> |
4 | 4 |
|
5 | 5 | Package `net` provides a portable interface for network I/O, including TCP/IP, UDP, domain name resolution, and Unix domain sockets. Although the package provides access to low-level networking primitives. |
6 | 6 |
|
7 | 7 | Package [`net/http`](https://golang.org/pkg/net/http/) provides HTTP client and server implementations. |
8 | 8 |
|
9 | 9 | ## ⚡️ Quick start |
10 | 10 |
|
11 | | -1. Create a new project: |
| 11 | +1. Create a new project with Fiber: |
12 | 12 |
|
13 | 13 | ```bash |
14 | 14 | cgapp create |
| 15 | + |
| 16 | +# Choose a backend framework: |
| 17 | +# > net/http |
| 18 | +# Fiber |
15 | 19 | ``` |
16 | 20 |
|
17 | | -2. Run project by this command: |
| 21 | +2. Run Docker container with database (_by default, for PostgreSQL_): |
18 | 22 |
|
19 | 23 | ```bash |
20 | | -make run |
| 24 | +make docker.postgres |
21 | 25 | ``` |
22 | 26 |
|
23 | | -## ✅ Used packages |
| 27 | +3. Apply migrations: |
24 | 28 |
|
25 | | -- [gorilla/mux](https://github.com/gorilla/mux) `v1.7.4` |
26 | | -- [go-yaml/yaml](https://github.com/go-yaml/yaml) `v2.3.0` |
| 29 | +```bash |
| 30 | +make migration.up user=<db_user> pass=<db_pass> host=<db_host> table=<db_table> |
| 31 | +``` |
27 | 32 |
|
28 | | -## 🗄 Template structure |
| 33 | +4. Rename `.env.example` to `.env` and fill it with your environment values. |
| 34 | + |
| 35 | +5. Run project by this command: |
29 | 36 |
|
30 | 37 | ```bash |
31 | | -. |
32 | | -├── .dockerignore |
33 | | -├── .editorconfig |
34 | | -├── .gitignore |
35 | | -├── Dockerfile |
36 | | -├── Makefile |
37 | | -├── go.mod |
38 | | -├── go.sum |
39 | | -├── main.go |
40 | | -├── configs |
41 | | -│ └── apiserver.yml |
42 | | -├── static |
43 | | -│ └── index.html |
44 | | -└── pkg |
45 | | - └── apiserver |
46 | | - ├── config.go |
47 | | - ├── config_test.go |
48 | | - ├── error_checker.go |
49 | | - ├── error_checker_test.go |
50 | | - ├── new_server.go |
51 | | - ├── new_server_test.go |
52 | | - ├── routes.go |
53 | | - ├── utils.go |
54 | | - └── utils_test.go |
55 | | - |
56 | | -4 directories, 17 files |
| 38 | +make run |
57 | 39 | ``` |
58 | 40 |
|
| 41 | +6. Go to API Docs page (Swagger): [localhost:5000/swagger/index.html](http://localhost:5000/swagger/index.html). |
| 42 | + |
| 43 | +## 📦 Used packages |
| 44 | + |
| 45 | +| Name | Version | Type | |
| 46 | +| --------------------------------------------------------------------- | --------- | ---------- | |
| 47 | +| [net/http](https://golang.org/pkg/net/http/) | `v1.16.0` | core | |
| 48 | +| [auth0/go-jwt-middleware](https://github.com/auth0/go-jwt-middleware) | `v1.0.0` | middleware | |
| 49 | +| [swaggo/http-swagger](https://github.com/swaggo/http-swagger) | `v1.0.0` | middleware | |
| 50 | +| [stretchr/testify](https://github.com/stretchr/testify) | `v1.7.0` | tests | |
| 51 | +| [dgrijalva/jwt-go](https://github.com/dgrijalva/jwt-go) | `v3.2.0` | auth | |
| 52 | +| [joho/godotenv](https://github.com/joho/godotenv) | `v1.3.0` | config | |
| 53 | +| [jmoiron/sqlx](https://github.com/jmoiron/sqlx) | `v1.3.1` | database | |
| 54 | +| [jackc/pgx](https://github.com/jackc/pgx) | `v4.10.1` | database | |
| 55 | +| [swaggo/swag](https://github.com/swaggo/swag) | `v1.7.0` | utils | |
| 56 | +| [google/uuid](https://github.com/google/uuid) | `v1.2.0` | utils | |
| 57 | +| [go-playground/validator](https://github.com/go-playground/validator) | `v10.4.1` | utils | |
| 58 | + |
| 59 | +## 🗄 Template structure |
| 60 | + |
| 61 | +### ./app |
| 62 | + |
| 63 | +**Folder with business logic only**. This directory doesn't care about _what database driver you're using_ or _which caching solution your choose_ or any third-party things. |
| 64 | + |
| 65 | +- `./app/controllers` folder for functional controllers (used in routes) |
| 66 | +- `./app/models` folder for describe business models and methods of your project |
| 67 | +- `./app/queries` folder for describe queries for models of your project |
| 68 | +- `./app/validators` folder for describe validators for models fields |
| 69 | + |
| 70 | +### ./docs |
| 71 | + |
| 72 | +**Folder with API Documentation**. This directory contains config files for auto-generated API Docs by Swagger. |
| 73 | + |
| 74 | +### ./pkg |
| 75 | + |
| 76 | +**Folder with project-specific functionality**. This directory contains all the project-specific code tailored only for your business use case, like _configs_, _middleware_, _routes_ or _utils_. |
| 77 | + |
| 78 | +- `./pkg/configs` folder for configuration functions |
| 79 | +- `./pkg/middleware` folder for add middleware (Fiber built-in and yours) |
| 80 | +- `./pkg/routes` folder for describe routes of your project |
| 81 | +- `./pkg/utils` folder with utility functions (server starter, error checker, etc) |
| 82 | + |
| 83 | +### ./platform |
| 84 | + |
| 85 | +**Folder with platform-level logic**. This directory contains all the platform-level logic that will build up the actual project, like _setting up the database_ or _cache server instance_ and _storing migrations_. |
| 86 | + |
| 87 | +- `./platform/database` folder with database setup functions (by default, PostgreSQL) |
| 88 | +- `./platform/migrations` folder with migration files (used with [golang-migrate/migrate](https://github.com/golang-migrate/migrate) tool) |
| 89 | + |
59 | 90 | ## ⚙️ Configuration |
60 | 91 |
|
61 | | -```yaml |
62 | | -# ./configs/apiserver.yml |
63 | | - |
64 | | -# Server config |
65 | | -server: |
66 | | - host: 0.0.0.0 |
67 | | - port: 5000 |
68 | | - |
69 | | -# Database config |
70 | | -database: |
71 | | - host: 127.0.0.1 |
72 | | - port: 5432 |
73 | | - username: postgres |
74 | | - password: 1234 |
75 | | - |
76 | | -# Static files config |
77 | | -static: |
78 | | - prefix: / |
79 | | - path: ./static |
| 92 | +```ini |
| 93 | +# .env |
| 94 | + |
| 95 | +# Server settings: |
| 96 | +SERVER_URL="0.0.0.0:5000" |
| 97 | +SERVER_EMAIL="no-reply@example.com" |
| 98 | +SERVER_EMAIL_PASSWORD="secret" |
| 99 | + |
| 100 | +# JWT settings: |
| 101 | +JWT_SECRET_KEY="secret" |
| 102 | +JWT_REFRESH_KEY="refresh" |
| 103 | + |
| 104 | +# Database settings: |
| 105 | +DB_SERVER_URL="host=localhost port=5432 user=postgres password=password dbname=postgres sslmode=disable" |
| 106 | +DB_MAX_CONNECTIONS=100 |
| 107 | +DB_MAX_IDLE_CONNECTIONS=10 |
| 108 | +DB_MAX_LIFETIME_CONNECTIONS=2 |
| 109 | + |
| 110 | +# SMTP severs settings: |
| 111 | +SMTP_SERVER="smtp.example.com" |
| 112 | +SMTP_PORT=25 |
80 | 113 | ``` |
81 | 114 |
|
82 | 115 | ## ⚠️ License |
|
0 commit comments