better directory structure
Marco Andronaco andronacomarco@gmail.com
Thu, 10 Oct 2024 14:07:40 +0200
7 files changed,
102 insertions(+),
93 deletions(-)
M
functions.go
→
src/app/functions.go
@@ -1,4 +1,4 @@
-package main +package app import ( "context"@@ -9,7 +9,7 @@ "net/http"
"os" "time" - "github.com/birabittoh/auth-boilerplate/email" + "github.com/birabittoh/auth-boilerplate/src/email" ) func login(w http.ResponseWriter, userID uint, remember bool) {
M
main.go
→
main.go
@@ -1,96 +1,9 @@
package main import ( - "html/template" - "log" - "net/http" - "os" - "path/filepath" - "time" - - "github.com/birabittoh/auth-boilerplate/auth" - "github.com/birabittoh/auth-boilerplate/email" - "github.com/birabittoh/myks" - "github.com/glebarez/sqlite" - "github.com/joho/godotenv" - "gorm.io/gorm" -) - -type key int - -type User struct { - gorm.Model - Username string `gorm:"unique"` - Email string `gorm:"unique"` - PasswordHash string - Salt string -} - -const ( - dataDir = "data" - dbName = "app.db" + "github.com/birabittoh/auth-boilerplate/src/app" ) -var ( - db *gorm.DB - g *auth.Auth - m *email.Client - - baseUrl string - port string - - ks = myks.New[uint](0) - durationDay = 24 * time.Hour - durationWeek = 7 * durationDay - templates = template.Must(template.ParseGlob("templates/*.html")) -) - -const userContextKey key = 0 - func main() { - err := godotenv.Load() - if err != nil { - log.Println("Error loading .env file") - } - - port = os.Getenv("APP_PORT") - if port == "" { - port = "3000" - } - - baseUrl = os.Getenv("APP_BASE_URL") - if baseUrl == "" { - baseUrl = "http://localhost:" + port - } - - // Init auth and email - g = auth.NewAuth(os.Getenv("APP_PEPPER")) - m = loadEmailConfig() - - os.MkdirAll(dataDir, os.ModePerm) - dbPath := filepath.Join(dataDir, dbName) + "?_pragma=foreign_keys(1)" - db, err = gorm.Open(sqlite.Open(dbPath), &gorm.Config{}) - if err != nil { - log.Fatal(err) - } - - db.AutoMigrate(&User{}) - - // Handle routes - http.HandleFunc("GET /", loginRequired(examplePage)) - http.HandleFunc("GET /register", getRegisterHandler) - http.HandleFunc("GET /login", getLoginHandler) - http.HandleFunc("GET /reset-password", getResetPasswordHandler) - http.HandleFunc("GET /reset-password-confirm", getResetPasswordConfirmHandler) - http.HandleFunc("GET /logout", logoutHandler) - - http.HandleFunc("POST /login", postLoginHandler) - http.HandleFunc("POST /register", postRegisterHandler) - http.HandleFunc("POST /reset-password", postResetPasswordHandler) - http.HandleFunc("POST /reset-password-confirm", postResetPasswordConfirmHandler) - - // Start serving - log.Println("Port: " + port) - log.Println("Server started: " + baseUrl) - log.Fatal(http.ListenAndServe(":"+port, nil)) + app.Main() }
A
src/app/init.go
@@ -0,0 +1,96 @@
+package app + +import ( + "html/template" + "log" + "net/http" + "os" + "path/filepath" + "time" + + "github.com/birabittoh/auth-boilerplate/src/auth" + "github.com/birabittoh/auth-boilerplate/src/email" + "github.com/birabittoh/myks" + "github.com/glebarez/sqlite" + "github.com/joho/godotenv" + "gorm.io/gorm" +) + +type key int + +type User struct { + gorm.Model + Username string `gorm:"unique"` + Email string `gorm:"unique"` + PasswordHash string + Salt string +} + +const ( + dataDir = "data" + dbName = "app.db" +) + +var ( + db *gorm.DB + g *auth.Auth + m *email.Client + + baseUrl string + port string + + ks = myks.New[uint](0) + durationDay = 24 * time.Hour + durationWeek = 7 * durationDay + templates = template.Must(template.ParseGlob("templates/*.html")) +) + +const userContextKey key = 0 + +func Main() { + err := godotenv.Load() + if err != nil { + log.Println("Error loading .env file") + } + + port = os.Getenv("APP_PORT") + if port == "" { + port = "3000" + } + + baseUrl = os.Getenv("APP_BASE_URL") + if baseUrl == "" { + baseUrl = "http://localhost:" + port + } + + // Init auth and email + g = auth.NewAuth(os.Getenv("APP_PEPPER")) + m = loadEmailConfig() + + os.MkdirAll(dataDir, os.ModePerm) + dbPath := filepath.Join(dataDir, dbName) + "?_pragma=foreign_keys(1)" + db, err = gorm.Open(sqlite.Open(dbPath), &gorm.Config{}) + if err != nil { + log.Fatal(err) + } + + db.AutoMigrate(&User{}) + + // Handle routes + http.HandleFunc("GET /", loginRequired(examplePage)) + http.HandleFunc("GET /register", getRegisterHandler) + http.HandleFunc("GET /login", getLoginHandler) + http.HandleFunc("GET /reset-password", getResetPasswordHandler) + http.HandleFunc("GET /reset-password-confirm", getResetPasswordConfirmHandler) + http.HandleFunc("GET /logout", logoutHandler) + + http.HandleFunc("POST /login", postLoginHandler) + http.HandleFunc("POST /register", postRegisterHandler) + http.HandleFunc("POST /reset-password", postResetPasswordHandler) + http.HandleFunc("POST /reset-password-confirm", postResetPasswordConfirmHandler) + + // Start serving + log.Println("Port: " + port) + log.Println("Server started: " + baseUrl) + log.Fatal(http.ListenAndServe(":"+port, nil)) +}
M
templates/example.html
→
templates/example.html
@@ -4,7 +4,7 @@ <head>
<title>Pagina Protetta</title> </head> <body> - <h1>Benvenuto, {{.User.Username}}</h1> + <h1>Benvenuto, {{.User.Username}}!</h1> <p>Questa รจ una pagina protetta visibile solo agli utenti loggati.</p> <a href="/logout">Logout</a> </body>