Initial Commit

This commit is contained in:
2021-02-11 15:47:42 -05:00
commit fec590b16e
249 changed files with 42571 additions and 0 deletions

86
cmd/main.go Normal file
View File

@@ -0,0 +1,86 @@
package main
import (
"os"
"os/signal"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"reichard.io/imagini/cmd/server"
"github.com/99designs/gqlgen/api"
"github.com/99designs/gqlgen/codegen/config"
"reichard.io/imagini/plugin"
)
type UTCFormatter struct {
log.Formatter
}
func (u UTCFormatter) Format(e *log.Entry) ([]byte, error) {
e.Time = e.Time.UTC()
return u.Formatter.Format(e)
}
func main() {
log.SetFormatter(UTCFormatter{&log.TextFormatter{FullTimestamp: true}})
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",
Action: cmdGenerate,
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func cmdServer(ctx *cli.Context) error {
log.Info("Starting Imagini Server")
server := server.NewServer()
server.StartServer()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
server.StopServer()
os.Exit(0)
return nil
}
func cmdGenerate(ctx *cli.Context) error {
log.Info("Generating Imagini Models")
gqlgenConf, err := config.LoadConfigFromDefaultLocations()
if err != nil {
log.Panic("Failed to load config", err.Error())
os.Exit(2)
}
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
}

59
cmd/server/server.go Normal file
View File

@@ -0,0 +1,59 @@
package server
import (
"context"
"net/http"
"time"
log "github.com/sirupsen/logrus"
"reichard.io/imagini/internal/api"
"reichard.io/imagini/internal/auth"
"reichard.io/imagini/internal/config"
"reichard.io/imagini/internal/db"
)
type Server struct {
API *api.API
Auth *auth.AuthManager
Config *config.Config
Database *db.DBManager
httpServer *http.Server
}
func NewServer() *Server {
c := config.Load()
db := db.NewMgr(c)
auth := auth.NewMgr(db, c)
api := api.NewApi(db, c, auth)
return &Server{
API: api,
Auth: auth,
Config: c,
Database: db,
}
}
func (s *Server) StartServer() {
listenAddr := (":" + s.Config.ListenPort)
s.httpServer = &http.Server{
Handler: s.API.Router,
Addr: listenAddr,
}
go func() {
err := s.httpServer.ListenAndServe()
if err != nil {
log.Error("Error starting server ", err)
return
}
}()
}
func (s *Server) StopServer() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
s.httpServer.Shutdown(ctx)
}