Go API Core
Based on common API stack, here is a list of enhanced packages to simplify your go (or 'golang') API development.
Contents
Getting started
You have an option, you can use core as framework modular, or your own config with manual use of package, or event use directly enhanced package.
- Download core by using:
$ go get -u github.com/tossaro/go-api-core
-
Add file .env
on your main.go
folder, see .env
-
Import the following package:
import core "github.com/tossaro/go-api-core"
Modular Framework example
On your main.go
initialize every package before, and inject to ModuleParams:
//...
captcha := true
privateKeyPath := "./key_private.pem"
publicKeyPath := "./key_public.pem"
core.NewHttp(core.Options{
EnvPath: "./.env",
AuthType: gin.AuthTypeJwt,
// if AuthTypeJwt is AuthTypeJwt you need to generate rsa key
PrivateKeyPath: &privateKeyPath,
PublicKeyPath: &publicKeyPath,
// if AuthType type is AuthTypeGrpc
// AuthUrl localhost:50051
I18n: bI18n,
Captcha: &captcha,
Modules: []func(...interface{}){module1.NewHttpV1},
ModuleParams: append(make([]interface{}, 0), param1, param2, ...),
})
Core config with manual use of package example
- Initial the config in
main.go
code:
func main() {
cfg, err := core.NewConfig()
if err != nil {
log.Fatal("Config error: %s", err)
}
//...
}
- Add every package that you need for your API as example
gin
:
//...
captcha := true
g := gin.New(&gin.Options{
I18n: bI18n,
Mode: cfg.HTTP.Mode,
Version: cfg.App.Version,
BaseUrl: cfg.App.Name,
Log: log,
AuthType: gin.AuthTypeJwt,
// if AuthType type is AuthTypeJwt
Jwt: jwt,
// if AuthType type is AuthTypeGrpc
// AuthService: &cfg.Services[0].Url,
Captcha: &captcha,
})
//...
Enhanced Packages
Commands
Here are some useful commands for executing example usage:
- cd example/manual && go run cmd/http/main.go
- cd example/manual && go run cmd/grpc/main.go
- cd example/modular && go run cmd/http/main.go
- go install github.com/swaggo/swag/cmd/swag@latest
- cd example/manual/cmd/http && swag init -o ../../docs
- cd example/modular/cmd/http && swag init -o ../../docs
The MIT License (MIT)
Copyright © 2022
Hamzah Tossaro
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the “Software”), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.