get frontend code generating and usable

This commit is contained in:
2025-02-22 17:08:26 -07:00
parent 6a8a4a13ba
commit 9fe376d3ea
101 changed files with 16455 additions and 37 deletions

View File

@ -1,15 +1,21 @@
package main
import (
"context"
"github.com/payne8/go-libsql-dual-driver"
sqlite "github.com/ytsruh/gorm-libsql"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"gorm.io/gorm"
"log"
"net"
"os"
"context"
"log"
"net"
"net/http"
"os"
"time"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/payne8/go-libsql-dual-driver"
"github.com/rs/cors"
sqlite "github.com/ytsruh/gorm-libsql"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/reflection"
"gorm.io/gorm"
pb "{{ .AppName }}/gen/go"
)
@ -20,7 +26,7 @@ func main() {
tdb, err := libsqldb.NewLibSqlDB(
primaryUrl,
//libsqldb.WithMigrationFiles(migrationFiles),
// libsqldb.WithMigrationFiles(migrationFiles),
libsqldb.WithAuthToken(authToken),
libsqldb.WithLocalDBName("local.db"), // will not be used for remote-only
)
@ -30,7 +36,7 @@ func main() {
return
}
// instantiate the grom ORM
// Instantiate the gorm ORM
gormDB, err := gorm.Open(sqlite.New(sqlite.Config{Conn: tdb.DB}), &gorm.Config{})
if err != nil {
logger.Printf("failed to open gorm db %s: %s", primaryUrl, err)
@ -38,13 +44,14 @@ func main() {
return
}
// err = gormDB.AutoMigrate(&pb.UserORM{}) // TODO: figure out how to automate this part
// Uncomment these lines if you need automatic migration
// err = gormDB.AutoMigrate(&pb.UserORM{})
// if err != nil {
// logger.Printf("failed to migrate user: %s", err)
// log.Fatalln(err)
// return
// }
// err = gormDB.AutoMigrate(&pb.ProductORM{}) // TODO: figure out how to automate this part
// err = gormDB.AutoMigrate(&pb.ProductORM{})
// if err != nil {
// logger.Printf("failed to migrate product: %s", err)
// log.Fatalln(err)
@ -55,8 +62,9 @@ func main() {
DB: gormDB,
}
// Create the gRPC server with a middleware for logging (or future authentication)
grpcServer := grpc.NewServer(grpc.ChainUnaryInterceptor(
func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
// TODO: this is an example of a middleware - we will use this for authentication
// we will have a function that checks the token and a switch statement that checks if the method is public or not
logger.Printf("request: %s", info.FullMethod)
@ -66,19 +74,50 @@ func main() {
pb.Register{{ .AppNameCaps }}Server(grpcServer, &handlers)
reflection.Register(grpcServer)
// start the server
listener, err := net.Listen("tcp", ":9000")
// Start the gRPC server on port 9000
grpcAddr := ":9000"
lis, err := net.Listen("tcp", grpcAddr)
if err != nil {
logger.Printf("failed to listen: %s", err)
log.Fatalln(err)
return
logger.Fatalf("failed to listen on %s: %v", grpcAddr, err)
}
logger.Printf("listening on %s", listener.Addr().String())
go func() {
logger.Printf("gRPC server listening on %s", grpcAddr)
if err := grpcServer.Serve(lis); err != nil {
logger.Fatalf("failed to serve gRPC: %v", err)
}
}()
err = grpcServer.Serve(listener)
// Set up the grpc-gateway mux
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
gwmux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
grpcEndpoint := "localhost" + grpcAddr
err = pb.Register{{ .AppNameCaps }}HandlerFromEndpoint(ctx, gwmux, grpcEndpoint, opts)
if err != nil {
logger.Printf("failed to serve: %s", err)
log.Fatalln(err)
return
logger.Fatalf("failed to register gateway: %v", err)
}
withCors := cors.New(cors.Options{
AllowedOrigins: []string{"http://localhost:*"}, // TODO: change this to the actual domain
AllowedMethods: []string{"GET", "POST", "PATCH", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"ACCEPT", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link"},
AllowCredentials: true,
MaxAge: 300,
}).Handler(gwmux)
// Start the HTTP gateway on port 8080
httpAddr := ":8080"
logger.Printf("HTTP gateway listening on %s", httpAddr)
server := &http.Server{
Addr: httpAddr,
Handler: gwmux,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Fatalf("failed to serve HTTP gateway: %v", err)
}
}