init
This commit is contained in:
45
template/services/api/cmd/configInit.go
Normal file
45
template/services/api/cmd/configInit.go
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
Copyright © 2026 NAME HERE <EMAIL ADDRESS>
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"<@var(context.project.goprefix)>/<@var(context.project.name)>/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// configInitCmd represents the configInit command
|
||||
var configInitCmd = &cobra.Command{
|
||||
Use: "configInit",
|
||||
Short: "A brief description of your command",
|
||||
Long: `A longer description that spans multiple lines and likely contains examples
|
||||
and usage of using your command. For example:
|
||||
|
||||
Cobra is a CLI library for Go that empowers applications.
|
||||
This application is a tool to generate the needed files
|
||||
to quickly create a Cobra application.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
err := config.Write(*config.DefaultConf)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(configInitCmd)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
|
||||
// Cobra supports Persistent Flags which will work for this command
|
||||
// and all subcommands, e.g.:
|
||||
// configInitCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||
|
||||
// Cobra supports local flags which will only run when this command
|
||||
// is called directly, e.g.:
|
||||
// configInitCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
||||
37
template/services/api/cmd/migrate.go
Normal file
37
template/services/api/cmd/migrate.go
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
Copyright © 2026 NAME HERE <EMAIL ADDRESS>
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"<@var(context.project.goprefix)>/<@var(context.project.name)>/db"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// migrateCmd represents the migrate command
|
||||
var migrateCmd = &cobra.Command{
|
||||
Use: "migrate",
|
||||
Short: "run database migrations",
|
||||
Long: ``,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
err := db.RunDbMigrations()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(migrateCmd)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
|
||||
// Cobra supports Persistent Flags which will work for this command
|
||||
// and all subcommands, e.g.:
|
||||
// migrateCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||
|
||||
// Cobra supports local flags which will only run when this command
|
||||
// is called directly, e.g.:
|
||||
// migrateCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
||||
45
template/services/api/cmd/root.go
Normal file
45
template/services/api/cmd/root.go
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
Copyright © 2026 NAME HERE <EMAIL ADDRESS>
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"os"
|
||||
)
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "<@var(context.project.name)>",
|
||||
Short: "A brief description of your application",
|
||||
Long: `A longer description that spans multiple lines and likely contains
|
||||
examples and usage of using your application. For example:
|
||||
|
||||
Cobra is a CLI library for Go that empowers applications.
|
||||
This application is a tool to generate the needed files
|
||||
to quickly create a Cobra application.`,
|
||||
// Uncomment the following line if your bare application
|
||||
// has an action associated with it:
|
||||
// Run: func(cmd *cobra.Command, args []string) { },
|
||||
}
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||
func Execute() {
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Here you will define your flags and configuration settings.
|
||||
// Cobra supports persistent flags, which, if defined here,
|
||||
// will be global for your application.
|
||||
|
||||
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.<@var(context.project.name)>.yaml)")
|
||||
|
||||
// Cobra also supports local flags, which will only run
|
||||
// when this action is called directly.
|
||||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
||||
81
template/services/api/cmd/serve.go
Normal file
81
template/services/api/cmd/serve.go
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
Copyright © 2026 NAME HERE <EMAIL ADDRESS>
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"<@var(context.project.goprefix)>/<@var(context.project.name)>/config"
|
||||
"<@var(context.project.goprefix)>/<@var(context.project.name)>/db"
|
||||
"<@var(context.project.goprefix)>/<@var(context.project.name)>/server/todo"
|
||||
connectors "connectrpc.com/cors"
|
||||
"context"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/rs/cors"
|
||||
"github.com/spf13/cobra"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// serveCmd represents the serve command
|
||||
var serveCmd = &cobra.Command{
|
||||
Use: "serve",
|
||||
Short: "start server",
|
||||
Long: ``,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
err := config.Load()
|
||||
if err != nil {
|
||||
err := config.Write(*config.DefaultConf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
conf := *config.Conf
|
||||
ctx := context.Background()
|
||||
db_url := "postgres://" + conf.Database.User + ":" + conf.Database.Password + "@" + conf.Database.Host + ":" + strconv.Itoa(conf.Database.Port) + "/" + conf.Database.Name
|
||||
db_url = db_url + "?sslmode=disable"
|
||||
conn, err := pgxpool.New(ctx, db_url)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
err = db.RunDbMigrations()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
db.Q = db.New(conn)
|
||||
middleware := cors.New(cors.Options{
|
||||
AllowedOrigins: conf.Server.FrontendUrls,
|
||||
AllowedMethods: connectors.AllowedMethods(),
|
||||
AllowedHeaders: connectors.AllowedHeaders(),
|
||||
ExposedHeaders: connectors.ExposedHeaders(),
|
||||
Debug: true,
|
||||
})
|
||||
mux := http.NewServeMux()
|
||||
todoPath, todoHandler := todo.GetPathHandler()
|
||||
mux.Handle(todoPath, todoHandler)
|
||||
p := new(http.Protocols)
|
||||
p.SetHTTP1(true)
|
||||
p.SetUnencryptedHTTP2(true)
|
||||
s := http.Server{
|
||||
Addr: strings.Join([]string{conf.Server.Host, strconv.Itoa(conf.Server.Port)}, ":"),
|
||||
Handler: middleware.Handler(mux),
|
||||
Protocols: p,
|
||||
}
|
||||
s.ListenAndServe()
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(serveCmd)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
|
||||
// Cobra supports Persistent Flags which will work for this command
|
||||
// and all subcommands, e.g.:
|
||||
// serveCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||
|
||||
// Cobra supports local flags which will only run when this command
|
||||
// is called directly, e.g.:
|
||||
// serveCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
||||
Reference in New Issue
Block a user