-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit cff08bb
Showing
20 changed files
with
756 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
serviceAccountKey.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
serve: | ||
fresh | ||
dev: | ||
go run main.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}) | ||
} |