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.
169 lines
4.4 KiB
169 lines
4.4 KiB
package main
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"database/sql"
|
|
"encoding/hex"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"path"
|
|
"syscall"
|
|
"time"
|
|
|
|
"git.aiterp.net/lucifer/lucifer/controllers"
|
|
"git.aiterp.net/lucifer/lucifer/database/sqlite"
|
|
"git.aiterp.net/lucifer/lucifer/internal/config"
|
|
"git.aiterp.net/lucifer/lucifer/light"
|
|
"git.aiterp.net/lucifer/lucifer/middlewares"
|
|
"git.aiterp.net/lucifer/lucifer/models"
|
|
"github.com/gorilla/mux"
|
|
|
|
_ "git.aiterp.net/lucifer/lucifer/light/hue"
|
|
)
|
|
|
|
func main() {
|
|
// Flag Variables
|
|
var uiDir string
|
|
|
|
// Parse Flags
|
|
flag.StringVar(&uiDir, "uidir", "/var/www/", "UI directory")
|
|
flag.Parse()
|
|
|
|
// Configuration
|
|
conf, err := config.Load("./config.yaml", "/etc/lucifer/lucifer.yaml")
|
|
if err != nil {
|
|
log.Fatalln("Failed to load configuration:", err)
|
|
}
|
|
|
|
// Database
|
|
err = sqlite.Initialize(conf.DB.FileName)
|
|
if err != nil {
|
|
log.Fatalln("Failed to set up database:", err)
|
|
}
|
|
|
|
// Initialize
|
|
setupAdmin(sqlite.UserRepository, sqlite.GroupRepository)
|
|
|
|
// Services
|
|
lightService := light.NewService(sqlite.BridgeRepository, sqlite.LightRepository, sqlite.GroupRepository, sqlite.ButtonRepository)
|
|
|
|
// Controllers
|
|
userController := controllers.NewUserController(sqlite.UserRepository, sqlite.SessionRepository)
|
|
groupController := controllers.NewGroupController(sqlite.GroupRepository, sqlite.UserRepository, sqlite.LightRepository)
|
|
lightController := controllers.NewLightController(lightService, sqlite.GroupRepository, sqlite.UserRepository, sqlite.LightRepository)
|
|
bridgeController := controllers.NewBridgeController(lightService, sqlite.GroupRepository)
|
|
|
|
// Router
|
|
router := mux.NewRouter()
|
|
router.Use(middlewares.Session(sqlite.SessionRepository, sqlite.UserRepository))
|
|
groupController.Mount(router, "/api/group/")
|
|
userController.Mount(router, "/api/user/")
|
|
lightController.Mount(router, "/api/light/")
|
|
bridgeController.Mount(router, "/api/bridge/")
|
|
|
|
// Background Tasks
|
|
go lightService.SyncLoop(context.TODO())
|
|
|
|
// Server UI in production
|
|
stat, err := os.Stat(uiDir)
|
|
if err != nil || !stat.IsDir() {
|
|
log.Println("ui directory not found, skipping UI routes")
|
|
} else {
|
|
// Static files
|
|
router.PathPrefix("/static/").Handler(http.FileServer(http.Dir(uiDir)))
|
|
router.PathPrefix("/favicon.ico").Handler(http.FileServer(http.Dir(uiDir)))
|
|
router.PathPrefix("/manifest.json").Handler(http.FileServer(http.Dir(uiDir)))
|
|
router.PathPrefix("/index.html").Handler(http.FileServer(http.Dir(uiDir)))
|
|
|
|
// Serve index on any other path
|
|
router.PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
http.ServeFile(w, r, path.Join(uiDir, "index.html"))
|
|
})
|
|
}
|
|
|
|
// Setup webserver
|
|
server := http.Server{Addr: conf.Server.Address, Handler: router}
|
|
webserverErrCh := make(chan error)
|
|
go func() {
|
|
log.Println("Listening on", conf.Server.Address)
|
|
webserverErrCh <- server.ListenAndServe()
|
|
}()
|
|
|
|
// Setup quit signal listener
|
|
signalCh := make(chan os.Signal)
|
|
signal.Notify(signalCh, os.Interrupt, os.Kill, syscall.SIGTERM)
|
|
|
|
// Run until interrupt or server death
|
|
select {
|
|
case signal := <-signalCh:
|
|
{
|
|
log.Println("Stopping due to signal:", signal)
|
|
|
|
timeout, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
|
defer cancel()
|
|
|
|
err := server.Shutdown(timeout)
|
|
if err != nil {
|
|
log.Println("Warning: graceful shutdown timed out.")
|
|
}
|
|
|
|
os.Exit(0)
|
|
}
|
|
case err := <-webserverErrCh:
|
|
{
|
|
log.Println("Listening failed:", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
}
|
|
|
|
func setupAdmin(users models.UserRepository, groups models.GroupRepository) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
|
|
defer cancel()
|
|
|
|
admin, err := users.FindByName(ctx, "Admin")
|
|
if err != nil {
|
|
if err != sql.ErrNoRows {
|
|
log.Fatalln("Could not check for admin user:", err)
|
|
}
|
|
|
|
admin = models.User{Name: "Admin"}
|
|
admin.SetPassword("123456")
|
|
|
|
admin, err = users.Insert(ctx, admin)
|
|
if err != nil {
|
|
fmt.Println("Failed to insert admin username:", err)
|
|
}
|
|
}
|
|
|
|
buf := make([]byte, 16)
|
|
_, err = rand.Read(buf)
|
|
if err != nil {
|
|
log.Fatalln("Could not get random bytes:", err)
|
|
}
|
|
|
|
password := hex.EncodeToString(buf)
|
|
admin.SetPassword(password)
|
|
|
|
err = users.Update(ctx, admin)
|
|
if err != nil {
|
|
log.Println("Could not update admin password:", err)
|
|
} else {
|
|
log.Println("Administrator: Admin /", password)
|
|
}
|
|
|
|
groups.UpdatePermissions(ctx, models.GroupPermission{
|
|
UserID: admin.ID,
|
|
GroupID: 0,
|
|
Read: true,
|
|
Write: true,
|
|
Create: true,
|
|
Delete: true,
|
|
Manage: true,
|
|
})
|
|
}
|