cup/README.md

92 lines
4.2 KiB
Markdown
Raw Normal View History

2024-09-15 11:06:38 -04:00
![gocondor logo](https://git.smarteching.com/avatars/cd7cd5b690adc8e5ec6d6cdb117f1bf5a9e9353dae111bfbb394d2c3d4497537?size=200)
2024-09-12 19:15:08 -04:00
# Cup of Goffee
2024-09-15 12:50:36 -04:00
## What is Goffee?
2024-09-15 11:06:38 -04:00
Cup is a skeleton project for the Goffee [Go](https://go.dev) framework made for building web APIs, suitable for small, medium size and microservices projects. With it's simple structure, and developer friendly experience it helps with increasing the productivity.
2024-09-12 19:15:08 -04:00
## Main Features
- Routing
- Middlewares
- Data Validation
- Databases ORM ([GORM](https://gorm.io/) integrated)
- Emails
- JWT tokens
- Cache (Redis)
- HTTPS (TLS)
## Installation
2024-09-15 11:06:38 -04:00
To create a new `cup` project you need to install the `Goffee's cli` first
2024-09-12 19:15:08 -04:00
2024-09-15 11:06:38 -04:00
##### Install Goffee [cli] tool
To install the `goffee` globally open up your terminal and run the following command:
2024-09-12 19:15:08 -04:00
```bash
2024-09-15 11:06:38 -04:00
go install git.smarteching.com/goffee/goffee@latest
2024-09-12 19:15:08 -04:00
```
##### Create new project using a Cup of Goffee
2024-09-15 11:06:38 -04:00
Here is how you can create new `Goffee` projects
2024-09-12 19:15:08 -04:00
```bash
2024-09-15 11:06:38 -04:00
goffee new [project-name] [project-remote-repository]
2024-09-12 19:15:08 -04:00
```
Example
```bash
2024-09-15 11:06:38 -04:00
goffee new myapp git.smarteching.com/goffee/myapp
2024-09-12 19:15:08 -04:00
```
where:
`project-name` is the name of your project
2024-09-15 11:06:38 -04:00
`remote-repository` is the remote repository that will host the project.
2024-09-12 19:15:08 -04:00
## Getting started
2024-09-15 11:06:38 -04:00
First make sure you have [Goffee](https://git.smarteching.com/goffee/goffee) installed, then use it to create a new project, [here is how](https://git.smarteching.com/goffee/goffee/docs/gaffer#create-new-project-using-gaffer)
2024-09-12 19:15:08 -04:00
Let's create a route that returns `hello world`
Open up the file `routes.go` in the root directory of your project and add the following code:
```go "defining a route"
router.Get("/greeting", func(c *core.Context) *core.Response {
JsonString := `{"message": "hello world"}`
return c.Response.Json(JsonString)
})
```
Next, in your terminal navigate to the project dir and run the following command to start the `live reloading`:
```go
2024-09-15 11:06:38 -04:00
goffee run:dev
2024-09-12 19:15:08 -04:00
```
Finally, open up your browser and navigate to `http://localhost/greeting`
2024-09-15 11:06:38 -04:00
To learn more check the [routing docs section](https://git.smarteching.com/goffee/goffee/docs/routing)
2024-09-12 19:15:08 -04:00
## Architecture
The architecture is similar to `MVC`, where there is a routes file `./routes.go` in which you can map all your app routes to their controllers which resides in the directory `./controllers`. Controllers are simply methods that handles requests (GET, POST, ... etch) to the given routes.
## The request journey:
The first component that receive's the request in `Cup` is the `Router`,
then `Goffee` locates the matching [handler](https://gocondor.github.io/docs/handlers) of the request and it check's if there are any [middlewares](https://gocondor.github.io/docs/middlewares) to be executed either before or after the [handler](https://gocondor.github.io/docs/handlers), if so, it executes them in the right order, then at the final stage it returns the response to the user.
`Request -> Router -> Optional Middlewares -> Handler -> Optional Middlewares -> Response`
## Folder structure
```bash
├── cup
│ ├── config/ --------------------------> main configs
│ ├── events/ --------------------------> contains events
│ │ ├── jobs/ ------------------------> contains the event jobs
│ ├── controllers/ ------------------------> route's controllers
│ ├── logs/ ----------------------------> app log files
│ ├── hooks/ ---------------------> app hooks
│ ├── models/ --------------------------> database models
│ ├── storage/ -------------------------> a place to store files
│ ├── tls/ -----------------------------> tls certificates
│ ├── .env -----------------------------> environment variables
│ ├── .gitignore -----------------------> .gitignore
│ ├── go.mod ---------------------------> Go modules
│ ├── LICENSE --------------------------> license
│ ├── main.go --------------------------> go main file
│ ├── README.md ------------------------> readme file
│ ├── register-events.go ---------------> register events and jobs
│ ├── register-global-hooks.go ---> register global middlewares
│ ├── routes.go ------------------------> app routes
│ ├── run-auto-migrations.go -----------> database migrations
```