Finally Framework
This commit is contained in:
80
cmd/cmd.go
80
cmd/cmd.go
@@ -1,80 +0,0 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"reichard.io/imagini/routes"
|
||||
"reichard.io/imagini/internal/context"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var CmdServe = cli.Command{
|
||||
Name: "serve",
|
||||
Aliases: []string{"s"},
|
||||
Usage: "Start Imagini web server.",
|
||||
Action: serveWeb,
|
||||
}
|
||||
|
||||
func serveWeb(cliCtx *cli.Context) error {
|
||||
log.Info("Serving Web")
|
||||
|
||||
ctx := context.NewImaginiContext()
|
||||
routes.RegisterRoutes(ctx)
|
||||
|
||||
if err := http.ListenAndServe(":" + ctx.Config.ListenPort, nil); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// func testDatabase(cliCtx *cli.Context) error {
|
||||
// log.Info("Testing Database")
|
||||
// c := config.NewConfig()
|
||||
// db.ConnectDB(c)
|
||||
//
|
||||
// err := auth.CreateUser(models.User{
|
||||
// Username: "User123",
|
||||
// Email: "user26@evan.pub",
|
||||
// FirstName: "User",
|
||||
// LastName: "Reichard",
|
||||
// AuthType: "Local",
|
||||
// }, "myPassword123")
|
||||
//
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// }
|
||||
//
|
||||
// resp := auth.AuthenticateUser(models.APICredentials{User:"User123", Password: "myPassword123"})
|
||||
// if resp == true {
|
||||
// log.Info("USER SUCCESSFULLY AUTHENTICATED BY USERNAME")
|
||||
// }else {
|
||||
// log.Info("USER NOT AUTHENTICATED")
|
||||
// }
|
||||
//
|
||||
// resp = auth.AuthenticateUser(models.APICredentials{User:"user26@evan.pub", Password: "myPassword123"})
|
||||
// if resp == true {
|
||||
// log.Info("USER SUCCESSFULLY AUTHENTICATED BY EMAIL")
|
||||
// }else {
|
||||
// log.Info("USER NOT AUTHENTICATED")
|
||||
// }
|
||||
//
|
||||
// resp = auth.AuthenticateUser(models.APICredentials{User:"user@evan.pub", Password: "myPassword12"})
|
||||
// if resp == true {
|
||||
// log.Info("USER SUCCESSFULLY AUTHENTICATED BY EMAIL")
|
||||
// }else {
|
||||
// log.Info("USER NOT AUTHENTICATED")
|
||||
// }
|
||||
//
|
||||
// // foundUser, err := db.GetUser(db.User{Username: "User123"})
|
||||
//
|
||||
// // if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// // log.Warn("RECORD NOT FOUND")
|
||||
// // } else {
|
||||
// // log.Info("FOUND USER", foundUser)
|
||||
// // }
|
||||
//
|
||||
// return nil
|
||||
// }
|
||||
BIN
cmd/imagini.db
Normal file
BIN
cmd/imagini.db
Normal file
Binary file not shown.
55
cmd/main.go
Normal file
55
cmd/main.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"github.com/urfave/cli/v2"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"reichard.io/imagini/cmd/server"
|
||||
)
|
||||
|
||||
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}})
|
||||
|
||||
log.Info("Starting Imagini")
|
||||
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,
|
||||
},
|
||||
},
|
||||
}
|
||||
err := app.Run(os.Args)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func cmdServer(ctx *cli.Context) error {
|
||||
server := server.NewServer()
|
||||
server.StartServer()
|
||||
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, os.Interrupt)
|
||||
<-c
|
||||
|
||||
server.StopServer()
|
||||
os.Exit(0)
|
||||
|
||||
return nil
|
||||
}
|
||||
57
cmd/server/server.go
Normal file
57
cmd/server/server.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"time"
|
||||
"context"
|
||||
"net/http"
|
||||
"reichard.io/imagini/internal/db"
|
||||
"reichard.io/imagini/internal/api"
|
||||
"reichard.io/imagini/internal/auth"
|
||||
"reichard.io/imagini/internal/config"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
API *api.API
|
||||
Auth *auth.AuthManager
|
||||
Config *config.Config
|
||||
Database *db.DBManager
|
||||
httpServer *http.Server
|
||||
}
|
||||
|
||||
func NewServer() *Server {
|
||||
config := config.Load()
|
||||
db := db.NewMgr(config)
|
||||
auth := auth.NewMgr(db)
|
||||
api := api.NewApi(db, auth)
|
||||
|
||||
return &Server{
|
||||
API: api,
|
||||
Auth: auth,
|
||||
Config: config,
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user