func main() { tracelog.StartFile(tracelog.LevelTrace, "logs", 1) tracelog.Started("main", "Initializing Postgres") var confName string = os.Getenv("BOOKS_CONF") if len(confName) == 0 { confName = "default" } s := []string{"conf/db/", confName, ".json"} jsonconf, err := config.NewConfig("json", strings.Join(s, "")) if err != nil { tracelog.Errorf(err, "main", "config.NewConfig", "Failed to find config", strings.Join(s, "")) } var User string = jsonconf.String("User") var Pass string = jsonconf.String("Pass") var DBName string = jsonconf.String("DBName") var buffer bytes.Buffer buffer.WriteString("user="******" password="******" dbname=") buffer.WriteString(DBName) s2 := []string{"user="******" password="******" dbname=", DBName, " sslmode=disable"} orm.RegisterDriver("postgres", orm.DR_Postgres) orm.RegisterDataBase("default", "postgres", strings.Join(s2, "")) beego.SetStaticPath("/images", "static/images") beego.SetStaticPath("/css", "static/css") beego.SetStaticPath("/js", "static/js") beego.SetStaticPath("/fonts", "static/fonts") beego.SetStaticPath("/partials", "static/partials") beego.EnableAdmin = true orm.RunCommand() beego.Run() }
// Main go routine func main() { // Start logger tracelog.StartFile(1, utils.ConfigEntry("LogDir"), 1) // Get new cookie store store := sessions.NewCookieStore([]byte(utils.ConfigEntry("SecretKey"))) store.Options = &sessions.Options{ Path: "/", MaxAge: 3600, HttpOnly: true, } // Create DB connection dbHandle := utils.DbHandle() // Close DB defer dbHandle.Close() // Init Models sm := &models.Song{DbHandle: dbHandle} nm := &models.Nonce{DbHandle: dbHandle} um := models.NewUser(dbHandle) // Init Controllers sc := &controllers.Song{SM: sm} nc := &controllers.Nonce{NM: nm} vc := &controllers.View{Store: store} uc := &controllers.User{UM: um, Store: store} // Init Gin mux := gin.Default() // Load templates mux.LoadHTMLGlob("app/views/*") // Serve static files mux.Static("/public", utils.ConfigEntry("StaticDir")) // Routes for static pages static := mux.Group("/") { static.GET("/", vc.Index) static.GET("/about", vc.About) static.GET("/tos", vc.Tos) static.GET("/privacy-policy", vc.PrivacyPolicy) static.GET("/credits", vc.Credits) } // Routes that don't need authorization basic := mux.Group("/api/v1") basic.Use(middlewares.Session(store)) { basic.GET("/nonce", nc.Create) basic.GET("/users/check-username/:username", uc.CheckUsername) basic.GET("/users/check-email/:email", uc.CheckEmail) basic.POST("/users/login", uc.Login) basic.POST("/users/register", uc.Register) } // Routes that need authorization auth := mux.Group("/api/v1") auth.Use(middlewares.Session(store), middlewares.UserAuth(store), middlewares.Nonce(nm)) { auth.GET("/songs", sc.Index) auth.GET("/users/delete/:nonce", uc.ConfirmDelete) auth.POST("/users/delete", uc.Delete) auth.POST("/users/update", uc.Update) auth.POST("/users/logout", uc.Logout) } // Listen and serve on 0.0.0.0:8080 mux.Run(":9000") tracelog.Stop() }