Loggest thine Stuff
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

156 lines
4.2 KiB

package main
import (
"context"
"flag"
"git.aiterp.net/stufflog3/stufflog3/ports/cognitoauth"
"git.aiterp.net/stufflog3/stufflog3/ports/httpapi"
"git.aiterp.net/stufflog3/stufflog3/ports/mysql"
"git.aiterp.net/stufflog3/stufflog3/usecases/auth"
"git.aiterp.net/stufflog3/stufflog3/usecases/items"
"git.aiterp.net/stufflog3/stufflog3/usecases/projects"
"git.aiterp.net/stufflog3/stufflog3/usecases/scopes"
"git.aiterp.net/stufflog3/stufflog3/usecases/sprints"
"git.aiterp.net/stufflog3/stufflog3/usecases/stats"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
ginadapter "github.com/awslabs/aws-lambda-go-api-proxy/gin"
"github.com/gin-gonic/gin"
"log"
"os"
"os/signal"
"strconv"
"syscall"
)
var flagLocal = flag.Bool("local", false, "If set, run locally")
func main() {
flag.Parse()
db, err := mysql.Connect(
os.Getenv("STUFFLOG3_MYSQL_HOST"),
envInt("STUFFLOG3_MYSQL_PORT"),
os.Getenv("STUFFLOG3_MYSQL_USERNAME"),
os.Getenv("STUFFLOG3_MYSQL_PASSWORD"),
os.Getenv("STUFFLOG3_MYSQL_SCHEMA"),
)
if err != nil {
log.Println("Failed to open database:", err)
os.Exit(1)
}
cognitoAuth, err := cognitoauth.New(
os.Getenv("STUFFLOG3_AWS_REGION"),
os.Getenv("STUFFLOG3_AWS_CLIENT_ID"),
os.Getenv("STUFFLOG3_AWS_CLIENT_SECRET"),
os.Getenv("STUFFLOG3_AWS_POOL_ID"),
os.Getenv("STUFFLOG3_AWS_POOL_CLIENT_ID"),
os.Getenv("STUFFLOG3_AWS_POOL_CLIENT_SECRET"),
)
if err != nil {
log.Println("Failed to setup cognito:", err)
os.Exit(1)
}
authService := &auth.Service{
Provider: cognitoAuth,
}
scopesService := &scopes.Service{
Auth: authService,
Repository: db.Scopes(),
StatsLister: db.Stats(),
}
statsService := &stats.Service{
Scopes: scopesService,
Repository: db.Stats(),
}
itemsService := &items.Service{
Auth: authService,
Scopes: scopesService,
Stats: statsService,
Repository: db.Items(),
RequirementFetcher: db.Projects(),
ProjectFetcher: db.Projects(),
}
projectsService := &projects.Service{
Auth: authService,
Scopes: scopesService,
Stats: statsService,
Items: itemsService,
Repository: db.Projects(),
}
sprintsService := &sprints.Service{
Scopes: scopesService,
Items: itemsService,
Projects: projectsService,
Repository: db.Sprints(),
}
server := gin.New()
httpapi.Auth(server.Group("/api/v1/auth"), authService)
apiV1 := server.Group("/api/v1")
if os.Getenv("STUFFLOG3_USE_DUMMY_USER") == "true" {
log.Println("Using dummy UUID")
apiV1.Use(httpapi.DummyMiddleware(authService, "c11230be-4912-4313-83b0-410a248b5bd1"))
} else {
apiV1.Use(httpapi.JwtValidatorMiddleware(authService))
}
apiV1Scopes := apiV1.Group("/scopes")
httpapi.Scopes(apiV1Scopes, scopesService)
apiV1ScopesSub := apiV1Scopes.Group("/:scope_id")
apiV1ScopesSub.Use(httpapi.ScopeMiddleware(scopesService, authService))
httpapi.Projects(apiV1ScopesSub.Group("/projects"), projectsService)
httpapi.Items(apiV1ScopesSub.Group("/items"), itemsService)
httpapi.Sprints(apiV1ScopesSub.Group("/sprints"), sprintsService)
httpapi.Stats(apiV1ScopesSub.Group("/stats"), statsService)
apiV1AllScopes := apiV1.Group("/all-scopes")
apiV1AllScopes.Use(httpapi.AllScopeMiddleware(scopesService, authService))
httpapi.ItemsAllScopes(apiV1AllScopes.Group("/items"), itemsService)
httpapi.SprintsAllScopes(apiV1AllScopes.Group("/sprints"), sprintsService)
if *flagLocal {
exitSignal := make(chan os.Signal)
signal.Notify(exitSignal, os.Interrupt, os.Kill, syscall.SIGTERM)
errCh := make(chan error)
go func() {
err := server.Run(":8239")
if err != nil {
errCh <- err
}
}()
select {
case sig := <-exitSignal:
{
log.Println("Received signal", sig)
os.Exit(0)
}
case err := <-errCh:
{
log.Println("Server run failed:", err)
os.Exit(2)
}
}
} else {
ginLambda := ginadapter.New(server)
lambda.Start(func(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
return ginLambda.ProxyWithContext(ctx, request)
})
}
}
func envInt(key string) int {
value := os.Getenv(key)
if value == "" {
return 0
}
intValue, err := strconv.Atoi(value)
if err != nil {
return 0
}
return intValue
}