Artifact is a web framework written in Go (Golang) based on Gin, MongoDB.
Example Repo: Golang Gin Boilerplate
go get -u github.com/shipu/artifact
$ cat .env
APP_NAME=app
APP_ENV=local
APP_DEBUG=true
APP_URL=http://localhost
APP_PORT=8080
# assume the following codes in example.go file
$ cat example.go
package main
import (
. "github.com/shipu/artifact"
"github.com/gin-gonic/gin"
)
type AppConfig struct {
Name string `mapstructure:"APP_NAME" default:"Artifact"`
Environment string `mapstructure:"APP_ENV" default:"local"`
Debug bool `mapstructure:"APP_DEBUG" default:"true"`
Url string `mapstructure:"APP_URL" default:"http://localhost"`
Port int `mapstructure:"APP_PORT" default:"8098"`
TimeZone string `mapstructure:"APP_TIMEZONE" default:"UTC"`
Locale string `mapstructure:"APP_LOCALE" default:"en"`
GinMode string `mapstructure:"GIN_MODE" default:"debug"`
}
func main() {
// Initialize the application
New()
Config.AddConfig("App", new(AppConfig)).Load()
// artifact.Start() // Database connection will be established here
Router.GET("/", func(c *gin.Context) {
data := map[string]interface{}{
"app": Config.GetString("App.Name"),
}
//or
//data := gin.H{
// "message": "Hello World",
//}
Res.Status(200).
Message("success").
Data(data).Json(c)
})
Run()
}
# run example.go and visit 0.0.0.0:8080 (for windows "localhost:8080") on browser
$ go run example.go
go run ./art crud package_name crud_module_name
for relational crud generator
go run ./art crud package_name crud_module_name relational
please change the package_name
and crud_module_name
according to your need.
for example:
$ cat art/main.go
package main
import "github.com/shipu/artifact/cmd"
func main() {
cmd.Execute()
}
Run below command to generate crud.
go run ./art crud github.com/shipu/golang-gin-boilerplate notice
Below Folder structure will generate:
src/notice
├── controllers
│ └── notice_controller.go
├── models
│ └── notice.go
├── routes
│ └── api.go
└── services
└── notice_service.go
More information about crud generator can be found in Golang Gin Boilerplate
Suppose your config is config/db.go
:
package config
type DatabaseConfig struct {
Username string `mapstructure:"DB_USER" default:""`
Password string `mapstructure:"DB_PASS" default:""`
Host string `mapstructure:"DB_HOST" default:""`
Port string `mapstructure:"DB_PORT" default:""`
Database string `mapstructure:"DB_DATABASE" default:""`
Connection string `mapstructure:"DB_CONNECTION" default:""`
}
and your .env
is:
DB_CONNECTION=mongodb
DB_HOST=mongodb.host
DB_PORT=
DB_USER=user
DB_PASS=password
DB_DATABASE=collection
For initialization DatabaseConfig
config.
artifact.Config.AddConfig("NoSql", new(DatabaseConfig)).Load()
artifact.Config.GetString("NoSql.Host")
Config Method List:
GetString("key")
GetInt("key")
Get("key")
artifact.Router.GET("/", func(c *gin.Context) {
data := map[string]interface{}{
"app": Config.GetString("App.Name"),
}
//or
//data := gin.H{
// "message": "Hello World",
//}
Res.Status(200).
Message("success").
Data(data).Json(c)
})
artifact.Router.GET("/someGet", getting)
artifact.Router.POST("/somePost", posting)
artifact.Router.PUT("/somePut", putting)
artifact.Router.DELETE("/someDelete", deleting)
artifact.Router.PATCH("/somePatch", patching)
artifact.Router.HEAD("/someHead", head)
artifact.Router.OPTIONS("/someOptions", options)
And all Gin router support.
In Gin
Where c
is the *gin.Context
context.
data := map[string]interface{}{
"app": "Golang",
}
c.JSON(200, gin.H{
"status_code": 200,
"message": "Success",
"data": data,
})
In artifact
data := map[string]interface{}{
"app": "Golang",
}
Res.Code(200).
Message("Success").
Data(data).
Json(c)
for set custom key value in response
paginate := your paginate data
Res.Code(200).
Message("Success").
Data(data).
Raw(map[string]interface{}{
"meta": paginate,
}).
Json(c)
Res
Api Methods:
Json
PureJSON
JsonP
AsciiJSON
IndentedJSON
Html
Xml
Yaml
ProtoBuf
AbortWithStatusJSON
Abort
AbortWithError
Redirect
var TodoCollection artifact.MongoCollection = artifact.Mongo.Collection("todos")
TodoCollection.Find(bson.M{})
// Define the struct
var TestCmd = &cobra.Command{
Use: "test",
RunE: testCommand,
}
// Write the command function
func testCommand(cmd *cobra.Command, args []string) error {
log.Print("test command")
return nil
}
func main() {
cmd.AddCommand(TestCmd) // Add the command
cmd.Execute()
}
You can run command to generate Custom Command file
go run ./art make:command module_name command_name
And register in ./art/main.go
package main
import (
"github.com/shipu/artifact/cmd"
"github.com/tenminschool/enrolment-service/art/test/commands"
)
func init() {
cmd.AddCommand(commands.TestCmd) // Register the Custom command
}
func main() {
cmd.Execute()
}
All Go Mongo Driver Support.