This commit is contained in:
Fran Jurmanović
2021-05-06 20:09:29 +02:00
parent 3a5138eb83
commit 20894ee42e
18 changed files with 197 additions and 28 deletions

3
.gitignore vendored
View File

@@ -8,6 +8,9 @@
_obj
_test
vendor
bin
.env
# Architecture specific extensions/prefixes
*.[568vq]

12
Taskfile.yml Normal file
View File

@@ -0,0 +1,12 @@
version: '3'
tasks:
build:
cmds:
- go build -o "./bin/api.exe" "./cmd/api/main.go"
run:
cmds:
- go run cmd/api/main.go
migrate:
cmds:
- go run cmd/migrate/main.go

View File

@@ -1,9 +1,25 @@
package main
import (
"os"
"wallet-api/pkg/api"
"wallet-api/pkg/utl/common"
"wallet-api/pkg/utl/db"
"wallet-api/pkg/utl/server"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
func main() {
server.Start()
err := godotenv.Load()
common.CheckError(err)
dbUrl := os.Getenv("DATABASE_URL")
r := gin.Default()
conn := db.CreateConnection(dbUrl)
api.Init(r, conn)
server.Start(r)
}

View File

@@ -0,0 +1,23 @@
package main
import (
"log"
"os"
"wallet-api/pkg/migrate"
"wallet-api/pkg/utl/db"
"github.com/joho/godotenv"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
dbUrl := os.Getenv("DATABASE_URL")
conn := db.CreateConnection(dbUrl)
migrate.Start(conn)
}

2
go.mod
View File

@@ -7,6 +7,8 @@ require (
github.com/go-pg/pg/v10 v10.9.1
github.com/go-playground/validator/v10 v10.5.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/joho/godotenv v1.3.0
github.com/json-iterator/go v1.1.11 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect

4
go.sum
View File

@@ -50,9 +50,13 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.11 h1:uVUAXhF2To8cbw/3xN3pxj6kk7TYKs98NIrTqPlMWAQ=

View File

@@ -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
View 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
View 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
View 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()
}

View 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
View 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
View 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
}

View File

@@ -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)
}
}

View File

@@ -1,5 +1 @@
package configs
const (
DbConfig = ""
)

17
pkg/utl/db/db.go Normal file
View 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
}

View File

@@ -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
}
}

View File

@@ -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",