Skip to content

Commit

Permalink
project setup
Browse files Browse the repository at this point in the history
  • Loading branch information
bhattaraibishal50 committed Aug 27, 2020
0 parents commit cff08bb
Show file tree
Hide file tree
Showing 20 changed files with 756 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
serviceAccountKey.json
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
serve:
fresh
dev:
go run main.go
39 changes: 39 additions & 0 deletions common/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package common

import (
"fmt"
"github/bhattaraibishal50/blog/config"
"log"

"github.com/jinzhu/gorm"
)

/*
Database struct
@description
- Database has attribute DB which is equvalent to grom.DB
- Struct for gorm DB
*/
type Database struct {
*gorm.DB
}

// DB database
var DB *gorm.DB

// DbConnection - Opening a database and save the reference to `Database` struct.
func DbConnection() *gorm.DB {
dataSourceName := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?parseTime=true", config.DbUser, config.DbPassword, config.DbHost, config.Db)
db, err := gorm.Open("mysql", dataSourceName)
if err != nil {
log.Fatal(err.Error())
}
db.DB().SetMaxIdleConns(10)
DB = db
return DB
}

// MigrateDatabase database tables
func MigrateDatabase(db *gorm.DB) {
db.AutoMigrate()
}
17 changes: 17 additions & 0 deletions common/firestore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package common

import (
firebase "firebase.google.com/go"
"golang.org/x/net/context"
"google.golang.org/api/option"
)

// InitFirebase - Opening a database and save the reference to `Database` struct.
func InitFirebase() *firebase.App {
opt := option.WithCredentialsFile("../serviceAccountKey.json")
app, err := firebase.NewApp(context.Background(), nil, opt)
if err != nil {
panic(err.Error())
}
return app
}
21 changes: 21 additions & 0 deletions common/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package common

import (
"log"

"github.com/google/uuid"
)

// GetNewUUID returns newId
func GetNewUUID() uuid.UUID {
return uuid.New()
}

// ConvertStringToID returns Id
func ConvertStringToID(s string) uuid.UUID {
uuid, err := uuid.Parse(s)
if err != nil {
log.Fatal(err.Error)
}
return uuid
}
9 changes: 9 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package config

// database config
const (
DbUser = "admin"
DbPassword = "password"
Db = "go_test"
DbHost = "127.0.0.1"
)
28 changes: 28 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module github/bhattaraibishal50/blog

go 1.15

require (
cloud.google.com/go/firestore v1.3.0 // indirect
firebase.google.com/go v3.13.0+incompatible
github.com/gin-gonic/gin v1.6.3
github.com/go-playground/validator/v10 v10.3.0 // indirect
github.com/go-sql-driver/mysql v1.5.0
github.com/golang/protobuf v1.4.2 // indirect
github.com/google/uuid v1.1.1
github.com/howeyc/fsnotify v0.9.0 // indirect
github.com/jinzhu/gorm v1.9.16
github.com/json-iterator/go v1.1.10 // indirect
github.com/mattn/go-colorable v0.1.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/pilu/config v0.0.0-20131214182432-3eb99e6c0b9a // indirect
github.com/pilu/fresh v0.0.0-20190826141211-0fa698148017 // indirect
github.com/thinkerou/favicon v0.1.0
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/net v0.0.0-20200707034311-ab3426394381
golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6 // indirect
google.golang.org/api v0.29.0
google.golang.org/protobuf v1.25.0 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)
447 changes: 447 additions & 0 deletions go.sum

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"github/bhattaraibishal50/blog/middleware"

"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
"github.com/thinkerou/favicon"
)

func main() {
//setting up the application
r := gin.Default()
r.Use(favicon.New("./public/favicon.ico")) // set favicon middleware

// set routes
middleware.SetRoutes(r)
PORT := ":8080"
r.Run(PORT) //listen and serve on 0.0.0.0:8080 by default
}
18 changes: 18 additions & 0 deletions middleware/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package middleware

import (
postRouter "github/bhattaraibishal50/blog/post/route"
userRouter "github/bhattaraibishal50/blog/user/route"

"github.com/gin-gonic/gin"
)

// SetRoutes sets routes
func SetRoutes(router *gin.Engine) {
// user router
usersGroup := router.Group("/users")
userRouter.UserRoute(usersGroup)
// post router
postGroup := router.Group("/posts")
postRouter.PostRoute(postGroup)
}
11 changes: 11 additions & 0 deletions post/model/postModel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package model

import "github.com/google/uuid"

// Post struct with the post attributes
type Post struct {
ID uuid.UUID
Title string
Description string
CreatedAt string
}
47 changes: 47 additions & 0 deletions post/repo/postRepo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package repo

import (
"context"
"fmt"
"github/bhattaraibishal50/blog/common"
"github/bhattaraibishal50/blog/post/model"
"log"
"time"
)

// PostRepository interface
type PostRepository interface {
Save(post *model.Post) *model.Post
}

// Repo struct implements the interface
type repo struct{}

const collectionName string = "posts"

// NewPostRepository constructor returns the postRepositoory
func NewPostRepository() PostRepository {
return &repo{}
}

func (r *repo) Save(post *model.Post) *model.Post {
app := common.InitFirebase()
ctx := context.Background()
client, err := app.Firestore(ctx)
defer client.Close()
if err != nil {
log.Printf("An error has occurred: %s", err)
}
results, err := client.Collection(collectionName).Doc("LA").Set(ctx, map[string]interface{}{
"ID": post.ID,
"Title": post.Title,
"Description": post.Description,
"CreatedAt": time.Now(),
})
if err != nil {
// Handle any errors in an appropriate way, such as returning them.
log.Printf("An error has occurred: %s", err)
}
fmt.Println(results)
return post
}
13 changes: 13 additions & 0 deletions post/route/postRoute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package route

import (
postUsecase "github/bhattaraibishal50/blog/post/usecase"

"github.com/gin-gonic/gin"
)

// PostRoute exports user routes
func PostRoute(routerGroup *gin.RouterGroup) {
routerGroup.GET("/", postUsecase.GetPosts)
routerGroup.GET("/create", postUsecase.AddPosts)
}
29 changes: 29 additions & 0 deletions post/usecase/postUsecase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Package usecase is a business logics aread
*/
package usecase

import (
"github/bhattaraibishal50/blog/common"
"github/bhattaraibishal50/blog/post/model"
postRepo "github/bhattaraibishal50/blog/post/repo"
"net/http"

"github.com/gin-gonic/gin"
)

// GetPosts gets the posts
func GetPosts(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "on posts"})
}

// AddPosts add the post
func AddPosts(c *gin.Context) {
post := model.Post{}
post.ID = common.GetNewUUID()
post.Title = "TitleFromCode"
post.Description = "Description from code"
postRepo := postRepo.NewPostRepository()
res := postRepo.Save(&post)
c.JSON(http.StatusOK, gin.H{"status": &res})
}
Binary file added public/favicon.ico
Binary file not shown.
14 changes: 14 additions & 0 deletions runner.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
root: .
tmp_path: ./tmp
build_name: runner-build
build_log: runner-build-errors.log
valid_ext: .go, .tpl, .tmpl, .html
no_rebuild_ext: .tpl, .tmpl, .html
ignored: assets, tmp
build_delay: 600
colors: 1
log_color_main: cyan
log_color_build: yellow
log_color_runner: green
log_color_watcher: magenta
log_color_app:
Binary file added tmp/runner-build
Binary file not shown.
11 changes: 11 additions & 0 deletions user/model/userModel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package model

// UserModel usermodel
type UserModel struct {
ID uint `gorm:"primary_key"`
Username string `gorm:"column:username"`
Email string `gorm:"column:email;unique_index"`
Bio string `gorm:"column:bio;size:1024"`
Image *string `gorm:"column:image"`
PasswordHash string `gorm:"column:password;not null"`
}
12 changes: 12 additions & 0 deletions user/route/userRoute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package route

import (
"github/bhattaraibishal50/blog/user/usecase"

"github.com/gin-gonic/gin"
)

// UserRoute exports user routes
func UserRoute(routerGroup *gin.RouterGroup) {
routerGroup.GET("/", usecase.GetUsers)
}
15 changes: 15 additions & 0 deletions user/usecase/userUsecase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
Package usecase is a business logics aread
*/
package usecase

import (
"net/http"

"github.com/gin-gonic/gin"
)

// GetUsers gets the users
func GetUsers(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "on users"})
}

0 comments on commit cff08bb

Please sign in to comment.