mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
starter
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-pg/pg/v10"
|
||||
)
|
||||
|
||||
func Init(s *gin.Engine, db *pg.DB) {
|
||||
Routes(s, db)
|
||||
}
|
||||
|
||||
type API struct {
|
||||
Api string `json:"api"`
|
||||
}
|
||||
|
||||
15
pkg/api/routes.go
Normal file
15
pkg/api/routes.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"wallet-api/pkg/controllers"
|
||||
"wallet-api/pkg/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-pg/pg/v10"
|
||||
)
|
||||
|
||||
func Routes(s *gin.Engine, db *pg.DB) {
|
||||
apiService := services.ApiService{Db: db}
|
||||
apiController := controllers.ApiController{ApiService: &apiService}
|
||||
apiController.Init(s)
|
||||
}
|
||||
21
pkg/controllers/api.go
Normal file
21
pkg/controllers/api.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"wallet-api/pkg/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ApiController struct {
|
||||
ApiService *services.ApiService
|
||||
}
|
||||
|
||||
func (ac *ApiController) Init(s *gin.Engine) {
|
||||
apiGroup := s.Group("/api")
|
||||
apiGroup.GET("", ac.getFirst)
|
||||
}
|
||||
|
||||
func (ac *ApiController) getFirst(c *gin.Context) {
|
||||
apiModel := ac.ApiService.GetFirst()
|
||||
c.JSON(200, apiModel)
|
||||
}
|
||||
13
pkg/migrate/migrate.go
Normal file
13
pkg/migrate/migrate.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"wallet-api/pkg/migrate/migrations"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
)
|
||||
|
||||
func Start(conn *pg.DB) {
|
||||
apiMigration := migrations.ApiMigration{Db: conn}
|
||||
|
||||
apiMigration.Create()
|
||||
}
|
||||
31
pkg/migrate/migrations/api.go
Normal file
31
pkg/migrate/migrations/api.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"wallet-api/pkg/models"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
"github.com/go-pg/pg/v10/orm"
|
||||
)
|
||||
|
||||
type ApiMigration struct {
|
||||
Db *pg.DB
|
||||
}
|
||||
|
||||
func (am *ApiMigration) Create() {
|
||||
models := []interface{}{
|
||||
(*models.ApiModel)(nil),
|
||||
}
|
||||
|
||||
for _, model := range models {
|
||||
err := am.Db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||
IfNotExists: true,
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error Creating Table: %s", err)
|
||||
} else {
|
||||
fmt.Println("Table created successfully")
|
||||
}
|
||||
}
|
||||
}
|
||||
6
pkg/models/api.go
Normal file
6
pkg/models/api.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package models
|
||||
|
||||
type ApiModel struct {
|
||||
tableName struct{} `pg:"api,alias:api"`
|
||||
Api string `json:"api"`
|
||||
}
|
||||
17
pkg/services/api.go
Normal file
17
pkg/services/api.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"wallet-api/pkg/models"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
)
|
||||
|
||||
type ApiService struct {
|
||||
Db *pg.DB
|
||||
}
|
||||
|
||||
func (as *ApiService) GetFirst() models.ApiModel {
|
||||
apiModel := models.ApiModel{Api: "Works"}
|
||||
as.Db.Model(&apiModel).First()
|
||||
return apiModel
|
||||
}
|
||||
@@ -4,6 +4,6 @@ import "log"
|
||||
|
||||
func CheckError(err error) {
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to execute the query. %v", err)
|
||||
log.Fatalf("Error occured. %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1 @@
|
||||
package configs
|
||||
|
||||
const (
|
||||
DbConfig = ""
|
||||
)
|
||||
|
||||
17
pkg/utl/db/db.go
Normal file
17
pkg/utl/db/db.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
)
|
||||
|
||||
func CreateConnection(dbUrl string) *pg.DB {
|
||||
opt, err := pg.ParseURL(dbUrl)
|
||||
common.CheckError(err)
|
||||
conn := pg.Connect(opt)
|
||||
|
||||
fmt.Println("Successfully connected!")
|
||||
return conn
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package pg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
)
|
||||
|
||||
func CreateConnection() func() *pg.DB {
|
||||
opt, err := pg.ParseURL(os.Getenv("DATABASE_URL"))
|
||||
common.CheckError(err)
|
||||
|
||||
db := pg.Connect(opt)
|
||||
fmt.Println("Successfully connected!")
|
||||
return func() *pg.DB {
|
||||
return db
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Start() *gin.Engine {
|
||||
r := gin.Default()
|
||||
func Start(r *gin.Engine) *gin.Engine {
|
||||
r.GET("/ping", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{
|
||||
"message": "pong",
|
||||
|
||||
Reference in New Issue
Block a user