Refactor project structure and update configurations. Now first working version
- Updated `.air.conf` for Nix compatibility and simplified build commands. - Enhanced `.gitignore` to include `tmp` directory. - Improved `README.md` with clearer instructions and added language details. - Refined CSS styles for better UI consistency and added alert styles. - Upgraded `flake.nix` to use Go 1.24 and improved shell environment setup. - Modified authentication logic in `auth.go` for better user handling. - Updated `main.go` to dynamically set the listening port and improved logging. - Added new `routes.go` file for handling game entry and history. - Enhanced user models and added statistics tracking in `models.go`. - Improved template rendering and added user feedback messages in HTML templates. - Removed obsolete build error logs and binaries.
This commit is contained in:
51
src/main.go
51
src/main.go
@@ -4,8 +4,9 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -16,46 +17,44 @@ var (
|
||||
db *gorm.DB
|
||||
baseURL string
|
||||
cookieDomain string
|
||||
|
||||
// Global logger for this package
|
||||
lg = log.Default()
|
||||
)
|
||||
|
||||
const (
|
||||
PORT = "18765"
|
||||
// Default port if APP_PORT is not set
|
||||
defaultPort = 18765
|
||||
)
|
||||
|
||||
var logger = log.Default()
|
||||
|
||||
// ===================== Main =====================
|
||||
|
||||
func main() {
|
||||
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||
|
||||
// Get the listening port
|
||||
port := os.Getenv("APP_PORT")
|
||||
port = strconv.Itoa(defaultPort)
|
||||
|
||||
// Set the base URL
|
||||
baseURL = os.Getenv("APP_BASE_URL")
|
||||
if baseURL == "" {
|
||||
baseURL = "http://localhost:" + PORT
|
||||
baseURL = "http://localhost:" + port
|
||||
}
|
||||
cookieDomain = os.Getenv("APP_COOKIE_DOMAIN")
|
||||
|
||||
// DB connect
|
||||
// Open connection to SQLite
|
||||
var err error
|
||||
dsn := os.Getenv("DATABASE_URL")
|
||||
if dsn != "" {
|
||||
db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
||||
if err != nil {
|
||||
log.Fatal("postgres connect:", err)
|
||||
}
|
||||
log.Println("using Postgres")
|
||||
} else {
|
||||
db, err = gorm.Open(sqlite.Open("qrank.db"), &gorm.Config{})
|
||||
if err != nil {
|
||||
log.Fatal("sqlite connect:", err)
|
||||
}
|
||||
log.Println("using SQLite qrank.db")
|
||||
db, err = gorm.Open(sqlite.Open("qrank.db"), &gorm.Config{})
|
||||
if err != nil {
|
||||
lg.Fatal("sqlite connect:", err)
|
||||
}
|
||||
lg.Println("using SQLite qrank.db")
|
||||
|
||||
if err := db.AutoMigrate(&User{}, &Session{}, &LoginToken{}, &Table{}, &Game{}, &GamePlayer{}); err != nil {
|
||||
log.Fatal("migrate:", err)
|
||||
lg.Fatal("migrate:", err)
|
||||
}
|
||||
|
||||
// Create engine
|
||||
r := gin.Default()
|
||||
|
||||
// Serve static files from the current directory
|
||||
@@ -82,12 +81,10 @@ func main() {
|
||||
r.GET("/me", requireAuth(), getMe)
|
||||
r.POST("/me", requireAuth(), postMe)
|
||||
|
||||
bind := os.Getenv("APP_BIND")
|
||||
if bind == "" {
|
||||
bind = ":" + PORT
|
||||
}
|
||||
log.Println("listening on", bind, "base:", baseURL)
|
||||
// Start application with port
|
||||
bind := ":" + port
|
||||
lg.Println("listening on", bind, "base:", baseURL)
|
||||
if err := r.Run(bind); err != nil {
|
||||
log.Fatal(err)
|
||||
lg.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user