https://github.com/kmilodenisglez/github.template-srv.restapi.iris-go
github template for restapi server using golang iris framework
https://github.com/kmilodenisglez/github.template-srv.restapi.iris-go
cronjob github-template golang iris-golang rest-api
Last synced: 10 days ago
JSON representation
github template for restapi server using golang iris framework
- Host: GitHub
- URL: https://github.com/kmilodenisglez/github.template-srv.restapi.iris-go
- Owner: kmilodenisglez
- License: lgpl-2.1
- Created: 2022-09-26T18:36:26.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-10-11T17:03:47.000Z (over 3 years ago)
- Last Synced: 2024-11-13T15:57:44.361Z (over 1 year ago)
- Topics: cronjob, github-template, golang, iris-golang, rest-api
- Language: Go
- Homepage:
- Size: 175 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# π° GitHub Template API server
REST API that allows clients to communicate with * (i.e. **dispatch controller**).
> **NOTE**: Drones app has been tested on **Ubuntu 18.04** and on **Windows 10 with WSL** and Golang 1.18 was used.
## Table of Contents
- [API specification](#api_spec)
- [Configuration file](#config_file)
- [Get Started](#get_started)
* [Deployment ways (2 ways)](#deploy_ways)
- [Docker way](#docker_way)
- [Manual way](#manual_way)
- [Tech and packages](#tech)
- [Architecture](#arch)
- [SWAGGER](#swagger)
## βοΈAPI specification
The **GitHub Template API server** provides the following Example API with communicating the **DB**:
| Tag | Title | URL | Query | Method |
| ------------- | ---------------------------------- | ---------------------------------------- | ----- | ---- |
| Auth | user authentication (Using JWT) | `/api/v1/auth` | - |`POST`|
| Auth | user logout | `/api/v1/auth/logout` | - |`GET` |
| Auth | get user authenticated | `/api/v1/auth/user` | - |`GET` |
| Database | Populate DB with fake data | `/api/v1/database/populate` | - |`POST`|
| Drones | Get all drones or filters for State| `/api/v1/drones` |?state=|`GET` |
| Drones | Registers or update a drone | `/api/v1/drones` | - |`POST`|
| Drones | Get a drone by serialNumber | `/api/v1/drones/:serialNumber` | - |`GET` |
| Medications | Get medications | `/api/v1/medications` | - |`GET` |
| Medications | Checking loaded items for a drone | `/api/v1/medications/items/:serialNumber`| - |`GET` |
| Medications | Load a drone with medication items | `/api/v1/medications/items/:serialNumber`| - |`POST`|
To see the API specifications in more detail, run the app and visit the swagger docs:
> http://localhost:7001/swagger/index.html

## π οΈοΈ Configuration file (conf.yaml)
ππΎ [The config file](/conf/conf.yaml)
| Param | Description | default value |
| ----------- | -----------|------------------------- |
| APIDocIP | IP to expose the api (unused) | 127.0.0.1
| DappPort | app PORT | 7001
| StoreDBPath | DB file location | ./db/data.db
| CronEnabled | active the cron job | true
| LogDBPath | DB file event logs | ./db/event_log.db
| EveryTime | time interval (in seconds) that the cron task is executed | 300 seconds (every 5 minutes)
By default, **StoreDBPath** generates the database file in the /db folder at the root of the project.
The server exposes the `/api/v1/database/populate` POST endpoint to generate and repopulate the database whenever necessary.
## β‘ Get Started
Download the github.template-srv.restapi.iris-go project and move to root of project:
```bash
git clone https://github.com/kmilodenisglez/github.template-srv.restapi.iris-go.git && cd github.template-srv.restapi.iris-go
```
### π Deployment ways (2 ways)
You can start the server in 2 ways, the first is using **docker** and **docker-compose** and the second is **manually**
#### π¦ Docker way
You will need docker and docker-compose in your system.
To builds Docker image from Dockerfile, run:
```bash
docker build --no-cache --force-rm --tag app_restapi .
```
Use docker-compose to start the container:
```bash
docker-compose up
```
Run:
```bash
go mod download
go mod vendor
```
If you make changes to the Endpoint you must generate Swagger API Spec:

Build:
```bash
go build
```
#### π Environment variables
The environment variable is exported with the location of the server configuration file.
If you have π§Linux or πDash, run:
```bash
export SERVER_CONFIG=$PWD/conf/conf.yaml
```
but if it is in the windows cmd, then run:
```bash
set SERVER_CONFIG=%cd%/conf/conf.yaml
```
#### ππ½ββοΈ Start the server
Before it is recommended that you read more about the server configuration file in the section ππΎ .
Run the server:
```bash
./restapi.app
```
and visit the swagger docs:
> http://localhost:7001/swagger/index.html
The first endpoint to execute must be /api/v1/database/populate [POST], to populate the database. That endpoint does not need authentication.

You can then authenticate and test the remaining endpoints.
### π§ͺ Unit or End-To-End Testing
Run:
```bash
go test -v
```
## π¨ Tech and packages
* [Iris Web Framework](https://github.com/kataras/iris)
* [validator/v10](https://github.com/go-playground/validator)
* [Buntdb](https://github.com/tidwall/buntdb)
* [govalidator](https://github.com/asaskevich/govalidator)
* [gocron](https://github.com/go-co-op/gocron)
* [swag](https://github.com/swaggo/swag)
* [Docker](https://docs.docker.com)
* [docker-compose](https://docs.docker.com/compose/)
## π Architecture
This project has 3 layer :
- Controller Layer (Presentation)
- Service Layer (Business)
- Repository Layer (Persistence)
Tag | Path | Layer |
--- | ---- | ----- |
Auth | [end_auth.go](/api/endpoints/end_auth.go) | Controller |
Drones | [end_drones.go](/api/endpoints/end_drones.go) | Controller |
EventLog | [end_eventlog.go](/api/endpoints/end_eventlog.go) | Controller |
| | |
Auth | [svc_authentication.go](/service/auth/svc_authentication.go) | Service |
Drones | [svc_drones.go](/service/svc_drones.go) | Service |
EventLog | [svc_eventlog.go](/service/cron/svc_eventlog.go) | Service |
| | |
Auth | [repo_drones.go](/repo/db/repo_drones.go) | Repository |
Drones | [repo_drones.go](/repo/db/repo_drones.go) | Repository |
EventLog | [repo_eventlog.go](/repo/db/repo_eventlog.go) | Repository |