add ability to set up a webapp
This commit is contained in:
84
cmd/cli/templates/backend/main.go.tmpl
Normal file
84
cmd/cli/templates/backend/main.go.tmpl
Normal 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
|
||||
}
|
||||
}
|
38
cmd/cli/templates/cli/cli.go
Normal file
38
cmd/cli/templates/cli/cli.go
Normal 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
|
||||
}
|
||||
}
|
20
cmd/cli/templates/cli/commands.go
Normal file
20
cmd/cli/templates/cli/commands.go
Normal 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
|
68
cmd/cli/templates/proto/application.proto.tmpl
Normal file
68
cmd/cli/templates/proto/application.proto.tmpl
Normal 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
|
||||
}
|
15
cmd/cli/templates/vue/AboutView.vue.tmpl
Normal file
15
cmd/cli/templates/vue/AboutView.vue.tmpl
Normal 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>
|
8
cmd/cli/templates/vue/HomeView.vue.tmpl
Normal file
8
cmd/cli/templates/vue/HomeView.vue.tmpl
Normal file
@ -0,0 +1,8 @@
|
||||
<script setup lang="ts">
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<main>
|
||||
|
||||
</main>
|
||||
</template>
|
Reference in New Issue
Block a user