Skip to content

Commit

Permalink
feat: first
Browse files Browse the repository at this point in the history
  • Loading branch information
rensawamo committed Jul 28, 2024
0 parents commit 5bc7b65
Show file tree
Hide file tree
Showing 135 changed files with 5,237 additions and 0 deletions.
59 changes: 59 additions & 0 deletions backend/api/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package api

import (
"fmt"
"log"
"net/http"

"github.com/RecepieApp/server/models"
"github.com/gin-gonic/gin"
"github.com/redis/go-redis/v9"
)

func getUserCache(ctx *gin.Context, client *redis.Client) string {
userID := ctx.Query("userID")
authToken, err := models.GetUserCacheToken(ctx, client, userID)
if err != nil {
log.Printf("Issues retriving Cached Token %v", err)
return ""
}

return authToken
}

func setUserCache(ctx *gin.Context, client *redis.Client) {
var userCache models.UserCache

err := models.UnmarshallRequestBodyToAPIData(ctx.Request.Body, &userCache)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"error": "Unable to parse data",
})
return
}

key := fmt.Sprintf("user:%s", userCache.UserID)
fmt.Println("Key", key)

headerall, notExists := client.HGetAll(ctx, key).Result()
fmt.Println("headerall", headerall)
if notExists == nil {
userCache.SetCachedToken(ctx, client, key)
return
}
}


func checkTokenExpiration(ctx *gin.Context, client *redis.Client) {
//userID := ctx.Query("userID")
//key := fmt.Sprintf("user:%s", userID)
//var expired bool
//
//expirationTime := client.ExpireTime(ctx, key).Val()
//currentTime := time.Now()
//
//expired := expirationTime
//ctx.JSON(200, gin.H{
// "message": expired,
//})
}
70 changes: 70 additions & 0 deletions backend/api/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package api

import (
"net/http"

"cloud.google.com/go/firestore"
"firebase.google.com/go/auth"
md "github.com/RecepieApp/server/middleware"
"github.com/gin-gonic/gin"
"github.com/redis/go-redis/v9"
)

func SetCache(router *gin.Engine, client *redis.Client) {
router.POST("/set-cache", func(c *gin.Context) {
print("Setting cache")
setUserCache(c, client)
})

router.GET("/check-expiration", func(c *gin.Context) {
checkTokenExpiration(c, client)
})

}

func CreateCustomToken(ctx *gin.Context, auth *auth.Client) {
claims := map[string]interface{}{
"premiumAccount": true,
}

token, err := auth.CustomTokenWithClaims(ctx, "some-uid", claims)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"error": "error minting custom token",
})
}
ctx.JSON(http.StatusOK, gin.H{
"token": token,
})
}

// custom

func SetRoutes(router *gin.Engine, client *firestore.Client, auth *auth.Client, redisClient *redis.Client) {
router.OPTIONS("/*any", func(c *gin.Context) {
c.Status(http.StatusOK)
})

router.Use(func(c *gin.Context) {
authToken := c.GetHeader("AuthToken")
md.AuthJWT(auth, authToken)(c)
})

router.GET("/", func(c *gin.Context) {
showRecepies(c, client)
})

router.POST("/recipe", func(c *gin.Context) {
addRecepie(c, client)
})

router.PATCH("/recipe/:id", func(c *gin.Context) {
updateRecepie(c, client)
})

router.DELETE("/recipes/:id", func(c *gin.Context) {
deleteRecepie(c, client)
})

router.Run()
}
73 changes: 73 additions & 0 deletions backend/api/recepies.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package api

import (
"net/http"

"cloud.google.com/go/firestore"
"github.com/RecepieApp/server/models"
"github.com/gin-gonic/gin"
)

func showRecepies(ctx *gin.Context, client *firestore.Client) {
// コンテキストからuser_idを取得
userID, exists := ctx.Get("userID")
if !exists {
// user_idが見つからない場合、エラーを返す
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "internal server error"})
return
}
data, err := models.ReadUserCollection(ctx, client, userID.(string))
if err != nil {
ctx.JSON(404, gin.H{
"Message": "Unable to retrieve data",
})
}
ctx.JSON(200, data)

}

func addRecepie(ctx *gin.Context, client *firestore.Client) {
data := models.Recepie{}

err := models.UnmarshallRequestBodyToAPIData(ctx.Request.Body, &data)
if err != nil {
ctx.JSON(400, gin.H{
"message": "Unable to parse data",
})
}

msg, status := data.AddRecepie(client)

ctx.JSON(status, gin.H{
"message": msg,
})

}

func updateRecepie(ctx *gin.Context, client *firestore.Client) {
data := models.Recepie{}

err := models.UnmarshallRequestBodyToAPIData(ctx.Request.Body, &data)
if err != nil {
ctx.JSON(400, gin.H{
"message": "Unable to parse data",
})
}

data.UpdateRecepie(ctx, client)

}

func deleteRecepie(ctx *gin.Context, client *firestore.Client) {
data := models.Recepie{}

err := models.UnmarshallRequestBodyToAPIData(ctx.Request.Body, &data)
if err != nil {
ctx.JSON(400, gin.H{
"Message": "Unable to parse data",
})
}

data.DeleteUserRecepie(ctx, client)

}
56 changes: 56 additions & 0 deletions backend/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"database/sql"
"flag"
"fmt"
"log" // Import the log package
"os"

_ "github.com/go-sql-driver/mysql"

"github.com/RecepieApp/server/app"
"github.com/RecepieApp/server/runtime"
)

var _ = flag.Bool("debug", false, "Enable Bun Debug log")

func main() {
flag.Parse()

// mysql 接続
dsn, ok := os.LookupEnv("DSN")
dieFalse(ok, "env DSN not found")

db, err := sql.Open("mysql", dsn)
dieIf(err)

err = db.Ping()
dieIf(err)

fmt.Println("connected")


a := app.Application{}

if err := a.LoadConfigurations(); err != nil {
log.Fatalf("Failed to load configurations: %v", err)
}

if err := runtime.Start(&a); err != nil {
log.Fatalf("Failed to start the application: %v", err)
}
}


func dieFalse(ok bool, msg string) {
if !ok {
panic(msg)
}
}

func dieIf(err error) {
if err != nil {
panic(err)
}
}
3 changes: 3 additions & 0 deletions backend/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github/rensawamo/authentication_app/server

go 1.22.3
25 changes: 25 additions & 0 deletions backend/pkg/server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package app

import (
pb "github.com/RecepieApp/server/gen/buf/proto"
"github.com/RecepieApp/server/middleware"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)

// serverはpb.ExampleServiceServerに対する実装です。
type server struct {
pb.UnimplementedHelloServiceServer
}
type AnnouncementServer struct {
pb.UnimplementedAnnouncementServiceServer
}

func (a *Application) setupGrpcServer() {
// 認証インターセプターを使用してgRPCサーバーを設定
interceptor := middleware.AuthInterceptor(a.FireAuth)
a.GrpcServer = grpc.NewServer(grpc.UnaryInterceptor(interceptor))
pb.RegisterHelloServiceServer(a.GrpcServer, &server{})
pb.RegisterAnnouncementServiceServer(a.GrpcServer, &AnnouncementServer{})
reflection.Register(a.GrpcServer)
}
25 changes: 25 additions & 0 deletions backend/pkg/service/hello_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package app

import (
"context"

pb "github.com/RecepieApp/server/gen/buf/proto"
)

// SayHelloはExampleServiceのSayHello RPCを実装します。
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
return &pb.HelloReply{Message: "Hello " + in.Name}, nil
}


/// ユーザがお知らせを取得するためのRPC
func (s *AnnouncementServer) GetAnnouncement(ctx context.Context, in *pb.GetAnnouncementRequest) (*pb.Announcement, error) {
// ダミーデータのお知らせを作成
announcement := &pb.Announcement{
Id: 1,
Title: "サンプルお知らせ",
Content: "これはサンプルのお知らせです。",
}
// お知らせをレスポンスに含める
return announcement, nil
}
43 changes: 43 additions & 0 deletions frontend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/
migrate_working_dir/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
**/doc/api/
**/ios/Flutter/.last_build_id
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.pub-cache/
.pub/
/build/

# Symbolication related
app.*.symbols

# Obfuscation related
app.*.map.json

# Android Studio will place build artifacts here
/android/app/debug
/android/app/profile
/android/app/release
Loading

0 comments on commit 5bc7b65

Please sign in to comment.