2021-01-03 03:21:11 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-02-04 10:16:13 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2021-01-08 02:45:59 +00:00
|
|
|
|
2021-02-04 10:16:13 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/urfave/cli/v2"
|
2021-02-02 20:34:10 +00:00
|
|
|
|
2021-02-04 10:16:13 +00:00
|
|
|
"reichard.io/imagini/cmd/server"
|
|
|
|
|
|
|
|
"github.com/99designs/gqlgen/api"
|
2021-02-02 20:34:10 +00:00
|
|
|
"github.com/99designs/gqlgen/codegen/config"
|
2021-02-04 10:16:13 +00:00
|
|
|
"reichard.io/imagini/plugin"
|
2021-01-03 03:21:11 +00:00
|
|
|
)
|
|
|
|
|
2021-01-10 00:44:02 +00:00
|
|
|
type UTCFormatter struct {
|
2021-02-04 10:16:13 +00:00
|
|
|
log.Formatter
|
2021-01-10 00:44:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u UTCFormatter) Format(e *log.Entry) ([]byte, error) {
|
2021-02-04 10:16:13 +00:00
|
|
|
e.Time = e.Time.UTC()
|
|
|
|
return u.Formatter.Format(e)
|
2021-01-10 00:44:02 +00:00
|
|
|
}
|
|
|
|
|
2021-01-03 03:21:11 +00:00
|
|
|
func main() {
|
2021-02-04 10:16:13 +00:00
|
|
|
log.SetFormatter(UTCFormatter{&log.TextFormatter{FullTimestamp: true}})
|
2021-01-10 00:44:02 +00:00
|
|
|
|
2021-02-04 10:16:13 +00:00
|
|
|
app := &cli.App{
|
|
|
|
Name: "Imagini",
|
|
|
|
Usage: "A self hosted photo library.",
|
|
|
|
Commands: []*cli.Command{
|
|
|
|
{
|
|
|
|
Name: "serve",
|
|
|
|
Aliases: []string{"s"},
|
|
|
|
Usage: "Start Imagini web server.",
|
|
|
|
Action: cmdServer,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "generate",
|
|
|
|
Usage: "generate graphql schema",
|
2021-02-02 20:34:10 +00:00
|
|
|
Action: cmdGenerate,
|
|
|
|
},
|
2021-02-04 10:16:13 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
err := app.Run(os.Args)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-01-03 03:21:11 +00:00
|
|
|
}
|
2021-01-16 22:00:17 +00:00
|
|
|
|
|
|
|
func cmdServer(ctx *cli.Context) error {
|
2021-02-04 10:16:13 +00:00
|
|
|
log.Info("Starting Imagini Server")
|
|
|
|
server := server.NewServer()
|
|
|
|
server.StartServer()
|
2021-01-16 22:00:17 +00:00
|
|
|
|
2021-02-04 10:16:13 +00:00
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
<-c
|
2021-01-16 22:00:17 +00:00
|
|
|
|
2021-02-04 10:16:13 +00:00
|
|
|
server.StopServer()
|
|
|
|
os.Exit(0)
|
2021-01-16 22:00:17 +00:00
|
|
|
|
2021-02-04 10:16:13 +00:00
|
|
|
return nil
|
2021-01-16 22:00:17 +00:00
|
|
|
}
|
2021-02-02 20:34:10 +00:00
|
|
|
|
|
|
|
func cmdGenerate(ctx *cli.Context) error {
|
2021-02-04 10:16:13 +00:00
|
|
|
log.Info("Generating Imagini Models")
|
|
|
|
gqlgenConf, err := config.LoadConfigFromDefaultLocations()
|
|
|
|
if err != nil {
|
|
|
|
log.Panic("Failed to load config", err.Error())
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
2021-02-02 20:34:10 +00:00
|
|
|
|
2021-02-04 10:16:13 +00:00
|
|
|
log.Info("Generating Schema...")
|
|
|
|
err = api.Generate(gqlgenConf,
|
|
|
|
api.AddPlugin(plugin.New()),
|
|
|
|
)
|
|
|
|
log.Info("Schema Generation Done")
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err.Error())
|
|
|
|
os.Exit(3)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
return nil
|
2021-02-02 20:34:10 +00:00
|
|
|
}
|