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.
104 lines
2.4 KiB
104 lines
2.4 KiB
package main
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"database/sql"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
"git.aiterp.net/lucifer/lucifer/light"
|
|
"git.aiterp.net/lucifer/lucifer/models"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"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/middlewares"
|
|
|
|
_ "git.aiterp.net/lucifer/lucifer/light/hue"
|
|
)
|
|
|
|
func main() {
|
|
// Setup
|
|
conf, err := config.Load("./config.yaml", "/etc/lucifer/lucifer.yaml")
|
|
if err != nil {
|
|
log.Fatalln("Failed to load configuration:", err)
|
|
}
|
|
|
|
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)
|
|
|
|
// Controllers
|
|
userController := controllers.NewUserController(sqlite.UserRepository, sqlite.SessionRepository)
|
|
|
|
// Router
|
|
router := mux.NewRouter()
|
|
router.Use(middlewares.Session(sqlite.SessionRepository))
|
|
userController.Mount(router, "/api/user/")
|
|
|
|
// Background Tasks
|
|
go lightService.SyncLoop(context.TODO())
|
|
//go lightService.DiscoverLoop(context.TODO())
|
|
|
|
// TODO: Listen in another goroutine and have SIGINT/SIGTERM handlers with graceful shutdown.
|
|
http.ListenAndServe(conf.Server.Address, router)
|
|
}
|
|
|
|
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,
|
|
})
|
|
}
|