bingo/cmd/run.go

83 lines
2.2 KiB
Go
Raw Permalink Normal View History

2024-09-07 02:34:17 -04:00
/*
* Copyright (C) 2021-2024 Seraphim R. Pardee
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cmd
import (
"fmt"
"html/template"
"log"
"time"
"git.hl.srp.life/srp/bingo/routes"
2024-09-07 02:34:17 -04:00
"github.com/briandowns/spinner"
"github.com/dgraph-io/badger/v4"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html/v2"
"github.com/mingrammer/cfmt"
"github.com/spf13/cobra"
)
var (
ListenAddress string
DisplayURL string
)
func init() {
runCmd.Flags().StringVarP(&ListenAddress, "listen", "l", "0.0.0.0:3005", "address:port to listen from")
rootCmd.AddCommand(runCmd)
}
var runCmd = &cobra.Command{
Use: "run",
Short: "Run the bingo server",
TraverseChildren: true,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("🗑️ bingo by srp - https://sr.ht/~seraphimrp/bingo")
s := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
s.Suffix = " Running pre-flight checks"
s.FinalMSG = cfmt.Ssuccessln("✅ [info] Pre-flight checks complete")
s.Start()
engine := html.New("./views", ".html")
engine.AddFunc("safe", func(s string) template.HTML {
return template.HTML(s)
})
app := fiber.New(fiber.Config{
DisableStartupMessage: true,
Views: engine,
ViewsLayout: "layouts/main",
})
app.Static("/", "./static")
db, err := badger.Open(badger.DefaultOptions("./.bingodb").WithLogger(nil))
if err != nil {
s.FinalMSG = cfmt.Serrorln("😵 [err] couldn't establish the DB")
s.Stop()
log.Fatal(err)
}
defer db.Close()
routes.SetupRoutes(app, db)
s.Stop()
cfmt.Successf("🚀 [info] We have liftoff, bingo running at %s\n", ListenAddress)
app.Listen(ListenAddress)
},
}