Skip to content

Commit 218be4c

Browse files
rewire main.go into a cli app
1 parent c71f111 commit 218be4c

File tree

5 files changed

+102
-8
lines changed

5 files changed

+102
-8
lines changed

backend/cmd/main.go

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,84 @@ import (
1414
"github.com/joshsoftware/code-curiosity-2025/internal/app"
1515
"github.com/joshsoftware/code-curiosity-2025/internal/app/cronJob"
1616
"github.com/joshsoftware/code-curiosity-2025/internal/config"
17+
"github.com/joshsoftware/code-curiosity-2025/internal/db"
18+
"github.com/rs/cors"
19+
"github.com/urfave/cli"
1720
)
1821

1922
func main() {
20-
ctx := context.Background()
21-
2223
cfg, err := config.LoadAppConfig()
2324
if err != nil {
2425
slog.Error("error loading app config", "error", err)
2526
return
2627
}
2728

29+
cliApp := cli.NewApp()
30+
cliApp.Name = cfg.AppName
31+
cliApp.Version = "1.0.0"
32+
cliApp.Commands = []cli.Command{
33+
{
34+
Name: "start",
35+
Usage: "Start HTTP server",
36+
Action: func(c *cli.Context) error {
37+
return startApp(cfg)
38+
},
39+
},
40+
{
41+
Name: "migrate",
42+
Usage: "Database migrations",
43+
Subcommands: []cli.Command{
44+
{
45+
Name: "up",
46+
Usage: "Apply migrations",
47+
Action: func(c *cli.Context) error {
48+
m, _ := db.InitMainDBMigrations(cfg)
49+
m.MigrationsUp(c.Args().First())
50+
return nil
51+
},
52+
},
53+
{
54+
Name: "down",
55+
Usage: "Rollback migrations",
56+
Action: func(c *cli.Context) error {
57+
m, _ := db.InitMainDBMigrations(cfg)
58+
m.MigrationsDown(c.Args().First())
59+
return nil
60+
},
61+
},
62+
{
63+
Name: "create",
64+
Usage: "Create a new migration file",
65+
Action: func(c *cli.Context) error {
66+
m, _ := db.InitMainDBMigrations(cfg)
67+
return m.CreateMigrationFile(c.Args().First())
68+
},
69+
},
70+
},
71+
},
72+
}
73+
74+
if err := cliApp.Run(os.Args); err != nil {
75+
panic(err)
76+
}
77+
}
78+
79+
func startApp(cfg config.AppConfig) error {
80+
ctx := context.Background()
81+
82+
slog.Info("Starting CodeCuriosity Application...")
83+
2884
db, err := config.InitDataStore(cfg)
2985
if err != nil {
3086
slog.Error("error initializing database", "error", err)
31-
return
87+
return err
3288
}
3389
defer db.Close()
3490

3591
bigqueryInstance, err := config.BigqueryInit(ctx, cfg)
3692
if err != nil {
3793
slog.Error("error initializing bigquery", "error", err)
38-
return
94+
return err
3995
}
4096

4197
httpClient := &http.Client{}
@@ -47,11 +103,20 @@ func main() {
47103
newCronSchedular := cronJob.NewCronSchedular()
48104
newCronSchedular.InitCronJobs(dependencies.ContributionService, dependencies.UserService)
49105

106+
c := cors.New(cors.Options{
107+
AllowedOrigins: []string{"*"},
108+
AllowCredentials: true,
109+
AllowedMethods: []string{http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete, http.MethodOptions},
110+
AllowedHeaders: []string{"*"},
111+
})
112+
50113
server := http.Server{
51114
Addr: fmt.Sprintf(":%s", cfg.HTTPServer.Port),
52115
Handler: router,
53116
}
54117

118+
server.Handler = c.Handler(server.Handler)
119+
55120
serverRunning := make(chan os.Signal, 1)
56121

57122
signal.Notify(
@@ -83,4 +148,5 @@ func main() {
83148
}
84149

85150
slog.Info("server shutdown successfully")
86-
}
151+
return nil
152+
}

backend/go.mod

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,24 @@ require (
1010
github.com/jmoiron/sqlx v1.4.0
1111
github.com/lib/pq v1.10.9
1212
github.com/robfig/cron/v3 v3.0.1
13+
golang.org/x/crypto v0.37.0
1314
golang.org/x/oauth2 v0.29.0
1415
google.golang.org/api v0.231.0
1516
)
1617

18+
require (
19+
github.com/cpuguy83/go-md2man/v2 v2.0.7 // indirect
20+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
21+
github.com/urfave/cli v1.22.17 // indirect
22+
)
23+
1724
require (
1825
cloud.google.com/go v0.121.0 // indirect
1926
cloud.google.com/go/auth v0.16.1 // indirect
2027
cloud.google.com/go/auth/oauth2adapt v0.2.8 // indirect
2128
cloud.google.com/go/compute/metadata v0.6.0 // indirect
2229
cloud.google.com/go/iam v1.5.2 // indirect
23-
github.com/BurntSushi/toml v1.2.1 // indirect
30+
github.com/BurntSushi/toml v1.5.0 // indirect
2431
github.com/apache/arrow/go/v15 v15.0.2 // indirect
2532
github.com/felixge/httpsnoop v1.0.4 // indirect
2633
github.com/go-logr/logr v1.4.2 // indirect
@@ -37,6 +44,7 @@ require (
3744
github.com/klauspost/compress v1.16.7 // indirect
3845
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
3946
github.com/pierrec/lz4/v4 v4.1.18 // indirect
47+
github.com/rs/cors v1.11.1
4048
github.com/zeebo/xxh3 v1.0.2 // indirect
4149
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
4250
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.60.0 // indirect
@@ -45,7 +53,6 @@ require (
4553
go.opentelemetry.io/otel/metric v1.35.0 // indirect
4654
go.opentelemetry.io/otel/trace v1.35.0 // indirect
4755
go.uber.org/atomic v1.11.0 // indirect
48-
golang.org/x/crypto v0.37.0 // indirect
4956
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
5057
golang.org/x/mod v0.23.0 // indirect
5158
golang.org/x/net v0.39.0 // indirect

backend/go.sum

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25
2626
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
2727
github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
2828
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
29+
github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
30+
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
2931
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.27.0 h1:ErKg/3iS1AKcTkf3yixlZ54f9U1rljCkQyEXWUnIUxc=
3032
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.27.0/go.mod h1:yAZHSGnqScoU556rBOVkwLze6WP5N+U11RHuWaGVxwY=
3133
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.51.0 h1:fYE9p3esPxA/C0rQ0AHhP0drtPXDRhaWiwg1DPqO7IU=
@@ -40,6 +42,9 @@ github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UF
4042
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
4143
github.com/cncf/xds/go v0.0.0-20250121191232-2f005788dc42 h1:Om6kYQYDUk5wWbT0t0q6pvyM49i9XZAv9dDrkDA7gjk=
4244
github.com/cncf/xds/go v0.0.0-20250121191232-2f005788dc42/go.mod h1:W+zGtBO5Y1IgJhy4+A9GOqVhqLpfZi+vwmdNXUehLA8=
45+
github.com/cpuguy83/go-md2man/v2 v2.0.7 h1:zbFlGlXEAKlwXpmvle3d8Oe3YnkKIK4xSRTd3sHPnBo=
46+
github.com/cpuguy83/go-md2man/v2 v2.0.7/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
47+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4348
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4449
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4550
github.com/dhui/dktest v0.4.5 h1:uUfYBIVREmj/Rw6MvgmqNAYzTiKOHJak+enB5Di73MM=
@@ -137,10 +142,23 @@ github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
137142
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
138143
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
139144
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
145+
github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA=
146+
github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
147+
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
148+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
140149
github.com/spiffe/go-spiffe/v2 v2.5.0 h1:N2I01KCUkv1FAjZXJMwh95KK1ZIQLYbPfhaxw8WS0hE=
141150
github.com/spiffe/go-spiffe/v2 v2.5.0/go.mod h1:P+NxobPc6wXhVtINNtFjNWGBTreew1GBUCwT2wPmb7g=
151+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
152+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
153+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
154+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
155+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
156+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
157+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
142158
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
143159
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
160+
github.com/urfave/cli v1.22.17 h1:SYzXoiPfQjHBbkYxbew5prZHS1TOLT3ierW8SYLqtVQ=
161+
github.com/urfave/cli v1.22.17/go.mod h1:b0ht0aqgH/6pBYzzxURyrM4xXNgsoT/n2ZzwQiEhNVo=
144162
github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
145163
github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
146164
github.com/zeebo/errs v1.4.0 h1:XNdoD/RRMKP7HD0UhJnIzUy74ISdGGxURlYG8HSWSfM=
@@ -207,6 +225,8 @@ google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/
207225
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
208226
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
209227
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
228+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
229+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
210230
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
211231
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
212232
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 h1:slmdOY3vp8a7KQbHkL+FLbvbkgMqmXojpFUO/jENuqQ=

backend/internal/config/app.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type BigqueryProject struct {
3030
}
3131

3232
type AppConfig struct {
33+
AppName string `yaml:"app_name"`
3334
IsProduction bool `yaml:"is_production"`
3435
HTTPServer HTTPServer `yaml:"http_server"`
3536
Database Database `yaml:"database"`

backend/internal/db/migrate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package db
22

33
import (
44
"errors"

0 commit comments

Comments
 (0)