This is a RESTful API that allows the creation and management of VPNs for cloud interconnection.
- Standard responses for success and fail requests
- Swagger API documentation
- Sqlx DB with Postgres - but can be changed as needed.
- Standard for custom errors
- Logger for console and external file.
- Migrations setup
- Hot Reload
- Docker setup
- Intuitive, clean and scalabe structure
- /cmd --> Contains the app's entry-points
|- /server
|- /docs
|- main.go
|- Makefile
|- /another_binary
- /config --> Contains the config structures that the server uses.
- internal --> Contains the app's code
|- /errors
|- /handlers
|- /middleware
|- /model
|- /storage
|- server.go
- /logs --> The folder's files are not in version control. Here you'll have the logs of the apps (the ones you specify to be external)
- /migrations --> Migrations to set up the database schema in your db.
- /pkg --> Packages used in /internal
|- /httputils
|- /logger
- .air.toml
- .env --> Not in version control. Need to create your own - see below.
- .gitignore
- docker-compose.yml
- Dockerfile
- go.mod
- go.sum
- LICENSE
- README.md
Make sure to first install the binaries that will generate the api docs and hot-reload the app.
go install github.com/swaggo/swag/cmd/swag@latest
and
go install github.com/cosmtrek/air@latest
Download the libs
go mod download
go mod tidy
Create an .env file in the root folder and use this template:
# DEV, STAGE, PROD
ENV=DEV
PORT=8080
VERSION=0.0.1
DB_HOST=localhost #when running the app without docker
# DB_HOST=postgres # when running the app in docker
DB_USER=postgres
DB_NAME=postgres
DB_PASSWORD=postgres
DB_PORT=5432
If you start the app locally without docker make sure your Postgres DB is up.
Write air in terminal and the app will be up and running listening on whatever port you set in .env.
Don't forget to rename the module and all the imports in the code from my github link to yours.
To run the app in docker, you need to have docker installed on your machine.
Create an .env file in the root folder and use this template:
# DEV, STAGE, PROD
ENV=DEV
PORT=8080
VERSION=0.0.1
# DB_HOST=localhost #when running the app without docker
DB_HOST=postgres # when running the app in docker
DB_USER=postgres
DB_NAME=postgres
DB_PASSWORD=postgres
DB_PORT=5432
Then run the following command to build the image and start the container:
docker-compose up --build
To stop the app running in docker, run the following command:
docker-compose down
Navigate to the cmd/server directory and run the following command:
make swag
Navigate to the cmd/server directory and run the following command:
make build
To run the tests, navigate to the cmd/server directory and run the following command:
make test
After the app is up and running, you can access the API docs by going to http://localhost:8080/api/docs/index.html.
-
/cmd
- The entry point of the server is in
main.go, where the app loads environment variables and config from/config. - The config is passed to the server and can be expanded as needed.
- A goroutine runs an
OnShutdownfunction for server crashes or panics. - A
Makefileis included for building the app.
- The entry point of the server is in
-
/internal
- Contains the main code.
server.godefines theAppServerstruct with db, handlers, and server setup.Runfunction configures the server, database, router, middlewares, handlers, and migrations.SenderandStorageare injected fromhandlers.Handlers.- Functions:
OnShutdown,NotFoundHandler(404), andNotAllowedHandler(405).
-
/errors
- Defines custom error structs for response handling.
- The
Senderformats responses based on these errors. - Accepts errors as strings, structs, or custom
Errstructs.
-
/handlers
handlers.gosets up the handlers object with its dependencies.- Specific handler groups (e.g.,
books.go,users.go) are added here. - Functions in these files are received by the
Handlersstruct.
-
/middlewares
- Contains custom middlewares added to
negroniinserver.go.
- Contains custom middlewares added to
-
/model
base_model.goserves as the base for other models.- Each file (e.g.,
books.go) defines database models and request/response structs.
-
/storage
storage.godefines the storage interface for database switching.postgres_db.gocreates a PostgreSQL storage instance.- New databases can be added by implementing the
StorageInterface.
-
/pkg
httputilscontainshttp_response.gofor response standards.- The
Senderstruct handles automated responses (e.g., JSON). loggerprovides logging to console or/logsdirectory, configurable to a different path (e.g.,/var/log/myappfor Unix/Linux).
Create a new VPN:
curl -X 'POST' 'http://0.0.0.0:8080/api/vpn/add' -H 'accept: application/json' -H 'Content-Type: application/json' -d '{"type": "vpn", "local_as_number": 65000, "remote_as_number": 65001, "VNI": 1000, "name": "vpn1"}'This project is licensed under the AGPLv3 License - see the LICENSE file for details.