-
Notifications
You must be signed in to change notification settings - Fork 0
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 5bc7b65
Showing
135 changed files
with
5,237 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,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, | ||
//}) | ||
} |
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,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() | ||
} |
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,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) | ||
|
||
} |
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,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) | ||
} | ||
} |
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,3 @@ | ||
module github/rensawamo/authentication_app/server | ||
|
||
go 1.22.3 |
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,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) | ||
} |
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,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 | ||
} |
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,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 |
Oops, something went wrong.