wip
This commit is contained in:
11
go.mod
11
go.mod
@@ -1,11 +1,12 @@
|
|||||||
module GoMembership
|
module GoMembership
|
||||||
|
|
||||||
go 1.18
|
go 1.22.2
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gorilla/mux v1.8.0
|
github.com/go-sql-driver/mysql v1.8.1
|
||||||
golang.org/x/crypto v0.0.0-20220411214040-896ed404bb0e
|
github.com/gorilla/mux v1.8.1
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.22
|
||||||
|
golang.org/x/crypto v0.24.0
|
||||||
)
|
)
|
||||||
|
|
||||||
replace golang.org/x/crypto => golang.org/x/crypto v0.0.0-20220411214040-896ed404bb0e
|
require filippo.io/edwards25519 v1.1.0 // indirect
|
||||||
|
|
||||||
|
|||||||
15
go.sum
15
go.sum
@@ -1,5 +1,10 @@
|
|||||||
github.com/gorilla/mux v1.8.0 h1:vGHr6G7CRByRCFwUmpxP8Cj8ljy1OV8ZrRH6qrOEzXM=
|
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||||
github.com/gorilla/mux v1.8.0/go.mod h1:vGHr6G7CRByRCFwUmpxP8Cj8ljy1OV8ZrRH6qrOEzXM=
|
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||||
golang.org/x/crypto v0.0.0-20220411214040-896ed404bb0e h1:aIjQLr9igSCqCxL1cOsTSG91TtOGStlwrPvHxGpQuWk=
|
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
|
||||||
golang.org/x/crypto v0.0.0
|
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
|
||||||
|
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
|
||||||
|
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||||
|
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
|
||||||
|
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
|
||||||
|
|||||||
@@ -1,20 +1,38 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type DatabaseConfig struct {
|
||||||
|
DBPath string `json:"DBPath"`
|
||||||
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
DBUser string
|
DB DatabaseConfig `json:"db"`
|
||||||
DBPassword string
|
|
||||||
DBName string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadConfig() *Config {
|
func LoadConfig() *Config {
|
||||||
return &Config{
|
path, err := os.Getwd()
|
||||||
DBUser: os.Getenv("DB_USER"),
|
if err != nil {
|
||||||
DBPassword: os.Getenv("DB_PASSWORD"),
|
log.Fatalf("could not get working directory: %v", err)
|
||||||
DBName: os.Getenv("DB_NAME"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
configFile, err := os.Open(filepath.Join(path, "configs", "config.json"))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("could not open config file: %v", err)
|
||||||
|
}
|
||||||
|
defer configFile.Close()
|
||||||
|
|
||||||
|
decoder := json.NewDecoder(configFile)
|
||||||
|
config := &Config{}
|
||||||
|
err = decoder.Decode(config)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("could not decode config file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return config
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
|
|
||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
|
||||||
"GoMembership/internal/models"
|
"GoMembership/internal/models"
|
||||||
"GoMembership/internal/services"
|
"GoMembership/internal/services"
|
||||||
|
"encoding/json"
|
||||||
|
// "github.com/gorilla/mux"
|
||||||
|
"net/http"
|
||||||
|
// "strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UserController struct {
|
type UserController struct {
|
||||||
@@ -29,3 +30,28 @@ func (uc *UserController) RegisterUser(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.WriteHeader(http.StatusCreated)
|
w.WriteHeader(http.StatusCreated)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* func (uc *UserController) LoginUser(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var credentials struct {
|
||||||
|
Email string `json:"email"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
}
|
||||||
|
user, err := uc.service.AuthenticateUser(credentials.Email, credentials.Password)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
json.NewEncoder(w).Encode(user)
|
||||||
|
} */
|
||||||
|
|
||||||
|
/* func (uc *UserController) GetUserID(w http.ResponseWriter, r *http.Request) {
|
||||||
|
vars := mux.Vars(r)
|
||||||
|
id, err := strconv.Atoi(vars["id"])
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Invalid user ID", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
user, err := uc.service.GetUserByID(id)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "User not found: "+err.Error(), http.StatusNotFound)
|
||||||
|
}
|
||||||
|
} */
|
||||||
|
|||||||
@@ -1,17 +1,36 @@
|
|||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"GoMembership/internal/config"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"log"
|
"log"
|
||||||
"GoMembership/internal/config"
|
"os"
|
||||||
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func initializeDB(dbPath string, schemaPath string) error {
|
||||||
|
db, err := sql.Open("sqlite3", dbPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
schema, err := os.ReadFile(schemaPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = db.Exec(string(schema))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func Connect() *sql.DB {
|
func Connect() *sql.DB {
|
||||||
cfg := config.LoadConfig()
|
cfg := config.LoadConfig()
|
||||||
dsn := cfg.DBUser + ":" + cfg.DBPassword + "@/" + cfg.DBName
|
dsn := cfg.DB.DBPath
|
||||||
db, err := sql.Open("mysql", dsn)
|
db, err := sql.Open("sqlite3", dsn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -20,4 +39,3 @@ func Connect() *sql.DB {
|
|||||||
}
|
}
|
||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,12 +3,15 @@ package models
|
|||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
ID int `json:"id"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
MandateDateSigned time.Time `json:"mandate_date_signed"`
|
||||||
FirstName string `json:"first_name"`
|
FirstName string `json:"first_name"`
|
||||||
LastName string `json:"last_name"`
|
LastName string `json:"last_name"`
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
IBAN string `json:"iban"`
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
BIC string `json:"bic"`
|
||||||
|
MandateReference string `json:"mandate_reference"`
|
||||||
|
ID int `json:"id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
package repositories
|
package repositories
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
|
||||||
"GoMembership/internal/models"
|
"GoMembership/internal/models"
|
||||||
|
"GoMembership/pkg/errors"
|
||||||
|
"database/sql"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UserRepository interface {
|
type UserRepository interface {
|
||||||
CreateUser(user *models.User) error
|
CreateUser(user *models.User) error
|
||||||
|
FindUserByID(id int) (*models.User, error)
|
||||||
|
// FindUserByEmail(email string) (*models.User, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type userRepository struct {
|
type userRepository struct {
|
||||||
@@ -18,8 +21,20 @@ func NewUserRepository(db *sql.DB) UserRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *userRepository) CreateUser(user *models.User) error {
|
func (r *userRepository) CreateUser(user *models.User) error {
|
||||||
query := "INSERT INTO users (first_name, last_name, email, password, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)"
|
query := "INSERT INTO users (first_name, last_name, email, password, iban, bic, mandate_reference, mandate_date_signed, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
|
||||||
_, err := r.db.Exec(query, user.FirstName, user.LastName, user.Email, user.Password, user.CreatedAt, user.UpdatedAt)
|
_, err := r.db.Exec(query, user.FirstName, user.LastName, user.Email, user.Password, user.CreatedAt, user.UpdatedAt)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *userRepository) FindUserByID(id int) (*models.User, error) {
|
||||||
|
var user models.User
|
||||||
|
query := "SELECT id, first_name, last_name, email, iban, bic, mandate_reference FROM users WHERE id = ?"
|
||||||
|
err := r.db.QueryRow(query, id).Scan(&user.ID, &user.FirstName, &user.LastName, &user.Email, &user.IBAN, &user.BIC, &user.MandateReference)
|
||||||
|
if err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return nil, errors.ErrUserNotFound
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &user, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,23 +1,21 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
// "GoMembership/internal/config"
|
||||||
"log"
|
|
||||||
"GoMembership/internal/controllers"
|
"GoMembership/internal/controllers"
|
||||||
|
"GoMembership/internal/database"
|
||||||
"GoMembership/internal/repositories"
|
"GoMembership/internal/repositories"
|
||||||
"GoMembership/internal/routes"
|
"GoMembership/internal/routes"
|
||||||
"GoMembership/internal/services"
|
"GoMembership/internal/services"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Run() {
|
func Run() {
|
||||||
db, err := sql.Open("mysql", "user:password@/dbname")
|
// cfg := config.LoadConfig()
|
||||||
if err != nil {
|
db := database.Connect()
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
userRepo := repositories.NewUserRepository(db)
|
userRepo := repositories.NewUserRepository(db)
|
||||||
@@ -27,6 +25,8 @@ func Run() {
|
|||||||
router := mux.NewRouter()
|
router := mux.NewRouter()
|
||||||
routes.RegisterRoutes(router, userController)
|
routes.RegisterRoutes(router, userController)
|
||||||
|
|
||||||
log.Fatal(http.ListenAndServe(":8080", router))
|
log.Println("Starting server on :8080")
|
||||||
|
if err := http.ListenAndServe(":8080", router); err != nil {
|
||||||
|
log.Fatalf("could not start server: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"GoMembership/internal/models"
|
"GoMembership/internal/models"
|
||||||
"GoMembership/internal/repositories"
|
"GoMembership/internal/repositories"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UserService interface {
|
type UserService interface {
|
||||||
@@ -24,6 +25,8 @@ func (s *userService) RegisterUser(user *models.User) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
user.Password = string(hashedPassword)
|
user.Password = string(hashedPassword)
|
||||||
|
user.CreatedAt = time.Now()
|
||||||
|
user.UpdatedAt = time.Now()
|
||||||
|
user.MandateDateSigned = time.Now()
|
||||||
return s.repo.CreateUser(user)
|
return s.repo.CreateUser(user)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
3
src/go.mod
Normal file
3
src/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module git.stoelti.land/Alex/GoMembership
|
||||||
|
|
||||||
|
go 1.22.4
|
||||||
57
src/main.go
Normal file
57
src/main.go
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
// main.go
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/astaxie/beego"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
/* This would match routes like the following:
|
||||||
|
/sum/3/5
|
||||||
|
/product/6/23
|
||||||
|
...
|
||||||
|
*/
|
||||||
|
beego.Router("/:operation/:num1:int/:num2:int", &mainController{})
|
||||||
|
beego.Run()
|
||||||
|
}
|
||||||
|
|
||||||
|
type mainController struct {
|
||||||
|
beego.Controller
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (c *mainController) Get() {
|
||||||
|
|
||||||
|
//Obtain the values of the route parameters defined in the route above
|
||||||
|
operation := c.Ctx.Input.Param(":operation")
|
||||||
|
num1, _ := strconv.Atoi(c.Ctx.Input.Param(":num1"))
|
||||||
|
num2, _ := strconv.Atoi(c.Ctx.Input.Param(":num2"))
|
||||||
|
|
||||||
|
//Set the values for use in the template
|
||||||
|
c.Data["operation"] = operation
|
||||||
|
c.Data["num1"] = num1
|
||||||
|
c.Data["num2"] = num2
|
||||||
|
c.TplName = "result.html"
|
||||||
|
|
||||||
|
// Perform the calculation depending on the 'operation' route parameter
|
||||||
|
switch operation {
|
||||||
|
case "sum":
|
||||||
|
c.Data["result"] = add(num1, num2)
|
||||||
|
case "product":
|
||||||
|
c.Data["result"] = multiply(num1, num2)
|
||||||
|
default:
|
||||||
|
c.TplName = "invalid-route.html"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func add(n1, n2 int) int {
|
||||||
|
return n1 + n2
|
||||||
|
}
|
||||||
|
|
||||||
|
func multiply(n1, n2 int) int {
|
||||||
|
return n1 * n2
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user