add ability to set up a webapp

This commit is contained in:
2025-02-15 16:00:58 -07:00
parent e34da045bc
commit de771d83b1
15 changed files with 612 additions and 36 deletions

View File

@ -0,0 +1,84 @@
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"
pb "{{ .AppName }}/gen/go"
)
func main() {
logger := log.New(os.Stdout, "{{ .AppName }} ", log.LstdFlags)
primaryUrl := os.Getenv("LIBSQL_DATABASE_URL")
authToken := os.Getenv("LIBSQL_AUTH_TOKEN")
tdb, err := libsqldb.NewLibSqlDB(
primaryUrl,
//libsqldb.WithMigrationFiles(migrationFiles),
libsqldb.WithAuthToken(authToken),
libsqldb.WithLocalDBName("local.db"), // will not be used for remote-only
)
if err != nil {
logger.Printf("failed to open db %s: %s", primaryUrl, err)
log.Fatalln(err)
return
}
// instantiate the grom 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)
log.Fatalln(err)
return
}
// err = gormDB.AutoMigrate(&pb.UserORM{}) // TODO: figure out how to automate this part
// 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
// if err != nil {
// logger.Printf("failed to migrate product: %s", err)
// log.Fatalln(err)
// return
// }
handlers := pb.{{ .AppNameCaps }}DefaultServer{
DB: gormDB,
}
grpcServer := grpc.NewServer(grpc.ChainUnaryInterceptor(
func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err 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)
return handler(ctx, req)
},
))
pb.Register{{ .AppNameCaps }}Server(grpcServer, &handlers)
reflection.Register(grpcServer)
// start the server
listener, err := net.Listen("tcp", ":9000")
if err != nil {
logger.Printf("failed to listen: %s", err)
log.Fatalln(err)
return
}
logger.Printf("listening on %s", listener.Addr().String())
err = grpcServer.Serve(listener)
if err != nil {
logger.Printf("failed to serve: %s", err)
log.Fatalln(err)
return
}
}

View File

@ -0,0 +1,38 @@
package main
import (
"fmt"
"github.com/urfave/cli/v2"
"os"
"time"
)
func main() {
commands := []*cli.Command{
exampleCmd(),
// call your command functions from the commands.go file
}
app := &cli.App{
Name: "ExampleApp",
Usage: "An example CLI tool",
Version: "v1.0.0",
Description: "This is an example CLI tool",
Commands: commands,
Flags: nil,
Authors: []*cli.Author{
{
Name: "Mason Payne", // Update this to your name
Email: "mason@masonitestudios.com", // Update this to your email
},
},
Copyright: fmt.Sprintf("%d Example Corp", time.Now().Year()),
UseShortOptionHandling: true,
}
err := app.Run(os.Args)
if err != nil {
fmt.Println(fmt.Errorf("error running app | %w", err))
return
}
}

View File

@ -0,0 +1,20 @@
package main
import (
"fmt"
"github.com/urfave/cli/v2"
)
func exampleCmd() *cli.Command {
return &cli.Command{
Name: "example",
Aliases: []string{"e"},
Usage: "Run an example command",
Action: func(c *cli.Context) error {
fmt.Println("This is an example command")
return nil
},
}
}
// add commands here

View File

@ -0,0 +1,68 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
syntax = "proto3";
package {{ .AppName }};
import "gorm/options/gorm.proto";
//import "gorm/types/types.proto";
option go_package = "./;pb";
service {{ .AppNameCaps }} {
option (gorm.server).autogen = true;
// Add your service methods here
rpc Create{{ .ObjName }} (Create{{ .ObjName }}Request) returns (Create{{ .ObjName }}Response) {}
rpc Read{{ .ObjName }} (Read{{ .ObjName }}Request) returns (Read{{ .ObjName }}Response) {}
rpc List{{ .ObjName }}s (List{{ .ObjName }}sRequest) returns (List{{ .ObjName }}sResponse) {}
rpc Update{{ .ObjName }} (Update{{ .ObjName }}Request) returns (Update{{ .ObjName }}Response) {}
rpc Delete{{ .ObjName }} (Delete{{ .ObjName }}Request) returns (Delete{{ .ObjName }}Response) {
option (gorm.method).object_type = "{{ .ObjName }}";
}
}
message Create{{ .ObjName }}Request {
{{ .ObjName }} payload = 1;
}
message Create{{ .ObjName }}Response {
{{ .ObjName }} result = 1;
}
message Read{{ .ObjName }}Request {
uint64 id = 1;
}
message Read{{ .ObjName }}Response {
{{ .ObjName }} result = 1;
}
message List{{ .ObjName }}sRequest {}
message List{{ .ObjName }}sResponse {
repeated {{ .ObjName }} results = 1;
}
message Update{{ .ObjName }}Request {
{{ .ObjName }} payload = 1;
}
message Update{{ .ObjName }}Response {
{{ .ObjName }} result = 1;
}
message Delete{{ .ObjName }}Request {
uint64 id = 1;
}
message Delete{{ .ObjName }}Response {}
message {{ .ObjName }} {
option (gorm.opts).ormable = true;
uint64 id = 1;
// add object fields here
}

View File

@ -0,0 +1,15 @@
<template>
<div class="about">
<h1>This app was generated using the Masonry CLI tool.</h1>
</div>
</template>
<style>
@media (min-width: 1024px) {
.about {
min-height: 100vh;
display: flex;
align-items: center;
}
}
</style>

View File

@ -0,0 +1,8 @@
<script setup lang="ts">
</script>
<template>
<main>
</main>
</template>