From 7e8788f7f82ecccaff657d8517a7b851b046aa3d Mon Sep 17 00:00:00 2001 From: darius Date: Wed, 15 May 2024 15:27:18 +0200 Subject: [PATCH] folder structure refactor volgens doc --- .gitignore | 2 +- .idea/vcs.xml | 1 + Dockerfile => api/Dockerfile | 2 +- api/{ => api}/apiRoutes.go | 1 + api/api/handler/authHandler.go | 51 ++++++ api/{ => api}/handler/errorHandlers.go | 0 api/{ => api}/handler/mainHandler.go | 0 api/{ => api}/handler/projectHandler.go | 0 api/{ => api}/handler/userHandler.go | 0 api/{ => api}/webHandler/aboutpageHandler.go | 0 api/{ => api}/webHandler/homepageHandler.go | 0 api/api/webHandler/loginpageHandler.go | 24 +++ .../webHandler/projectpageHandler.go | 0 api/{ => api}/webRoutes.go | 1 + {database => api/database}/database.go | 0 {database => api/database}/ent/client.go | 0 {database => api/database}/ent/ent.go | 0 .../database}/ent/enttest/enttest.go | 0 {database => api/database}/ent/generate.go | 0 {database => api/database}/ent/hook/hook.go | 0 .../database}/ent/migrate/migrate.go | 0 .../database}/ent/migrate/schema.go | 3 +- {database => api/database}/ent/mutation.go | 56 ++++++- .../database}/ent/predicate/predicate.go | 0 {database => api/database}/ent/project.go | 0 .../database}/ent/project/project.go | 0 .../database}/ent/project/where.go | 0 .../database}/ent/project_create.go | 0 .../database}/ent/project_delete.go | 0 .../database}/ent/project_query.go | 0 .../database}/ent/project_update.go | 0 api/database/ent/runtime.go | 9 + .../database}/ent/runtime/runtime.go | 0 .../database}/ent/schema/project.go | 0 {database => api/database}/ent/schema/team.go | 0 {database => api/database}/ent/schema/user.go | 3 +- {database => api/database}/ent/team.go | 0 {database => api/database}/ent/team/team.go | 0 {database => api/database}/ent/team/where.go | 0 {database => api/database}/ent/team_create.go | 0 {database => api/database}/ent/team_delete.go | 0 {database => api/database}/ent/team_query.go | 0 {database => api/database}/ent/team_update.go | 0 {database => api/database}/ent/tx.go | 0 {database => api/database}/ent/user.go | 13 +- {database => api/database}/ent/user/user.go | 13 +- {database => api/database}/ent/user/where.go | 70 ++++++++ {database => api/database}/ent/user_create.go | 25 ++- {database => api/database}/ent/user_delete.go | 0 {database => api/database}/ent/user_query.go | 0 {database => api/database}/ent/user_update.go | 34 ++++ api/database/query/authQuery.go | 23 +++ {database => api/database}/query/userQuery.go | 0 go.mod => api/go.mod | 1 + go.sum => api/go.sum | 2 + main.go => api/main.go | 2 +- api/service/bcrypt/password.go | 15 ++ .../service}/validate/validateUser.go | 0 {templates => api/templates}/about/index.html | 0 .../brand/Darius_pasfoto_2023_Large.png | Bin .../assets/brand/bootstrap-logo-white.svg | 0 .../assets/brand/bootstrap-logo.svg | 0 .../assets/dist/css/bootstrap.min.css | 0 .../assets/dist/css/bootstrap.min.css.map | 0 .../assets/dist/css/bootstrap.rtl.min.css | 0 .../assets/dist/css/bootstrap.rtl.min.css.map | 0 .../assets/dist/js/bootstrap.bundle.min.js | 0 .../dist/js/bootstrap.bundle.min.js.map | 0 .../templates}/assets/js/color-modes.js | 0 .../templates}/homepage/index.html | 0 {templates => api/templates}/index.html | 0 .../templates}/jumbotron/index.html | 0 api/templates/login/index.html | 157 ++++++++++++++++++ .../templates}/navbar/index.html | 5 +- .../templates}/sign-in/index.html | 0 .../templates}/sign-in/sign-in.css | 0 .../templates}/themeSelector/index.html | 0 {types => api/types}/userTypes.go | 0 database/ent/runtime.go | 20 --- docker-compose.yml | 2 +- 80 files changed, 483 insertions(+), 52 deletions(-) rename Dockerfile => api/Dockerfile (97%) rename api/{ => api}/apiRoutes.go (86%) create mode 100644 api/api/handler/authHandler.go rename api/{ => api}/handler/errorHandlers.go (100%) rename api/{ => api}/handler/mainHandler.go (100%) rename api/{ => api}/handler/projectHandler.go (100%) rename api/{ => api}/handler/userHandler.go (100%) rename api/{ => api}/webHandler/aboutpageHandler.go (100%) rename api/{ => api}/webHandler/homepageHandler.go (100%) create mode 100644 api/api/webHandler/loginpageHandler.go rename api/{ => api}/webHandler/projectpageHandler.go (100%) rename api/{ => api}/webRoutes.go (90%) rename {database => api/database}/database.go (100%) rename {database => api/database}/ent/client.go (100%) rename {database => api/database}/ent/ent.go (100%) rename {database => api/database}/ent/enttest/enttest.go (100%) rename {database => api/database}/ent/generate.go (100%) rename {database => api/database}/ent/hook/hook.go (100%) rename {database => api/database}/ent/migrate/migrate.go (100%) rename {database => api/database}/ent/migrate/schema.go (96%) rename {database => api/database}/ent/mutation.go (96%) rename {database => api/database}/ent/predicate/predicate.go (100%) rename {database => api/database}/ent/project.go (100%) rename {database => api/database}/ent/project/project.go (100%) rename {database => api/database}/ent/project/where.go (100%) rename {database => api/database}/ent/project_create.go (100%) rename {database => api/database}/ent/project_delete.go (100%) rename {database => api/database}/ent/project_query.go (100%) rename {database => api/database}/ent/project_update.go (100%) create mode 100644 api/database/ent/runtime.go rename {database => api/database}/ent/runtime/runtime.go (100%) rename {database => api/database}/ent/schema/project.go (100%) rename {database => api/database}/ent/schema/team.go (100%) rename {database => api/database}/ent/schema/user.go (92%) rename {database => api/database}/ent/team.go (100%) rename {database => api/database}/ent/team/team.go (100%) rename {database => api/database}/ent/team/where.go (100%) rename {database => api/database}/ent/team_create.go (100%) rename {database => api/database}/ent/team_delete.go (100%) rename {database => api/database}/ent/team_query.go (100%) rename {database => api/database}/ent/team_update.go (100%) rename {database => api/database}/ent/tx.go (100%) rename {database => api/database}/ent/user.go (89%) rename {database => api/database}/ent/user/user.go (91%) rename {database => api/database}/ent/user/where.go (68%) rename {database => api/database}/ent/user_create.go (93%) rename {database => api/database}/ent/user_delete.go (100%) rename {database => api/database}/ent/user_query.go (100%) rename {database => api/database}/ent/user_update.go (92%) create mode 100644 api/database/query/authQuery.go rename {database => api/database}/query/userQuery.go (100%) rename go.mod => api/go.mod (94%) rename go.sum => api/go.sum (97%) rename main.go => api/main.go (97%) create mode 100644 api/service/bcrypt/password.go rename {service => api/service}/validate/validateUser.go (100%) rename {templates => api/templates}/about/index.html (100%) rename {templates => api/templates}/assets/brand/Darius_pasfoto_2023_Large.png (100%) rename {templates => api/templates}/assets/brand/bootstrap-logo-white.svg (100%) rename {templates => api/templates}/assets/brand/bootstrap-logo.svg (100%) rename {templates => api/templates}/assets/dist/css/bootstrap.min.css (100%) rename {templates => api/templates}/assets/dist/css/bootstrap.min.css.map (100%) rename {templates => api/templates}/assets/dist/css/bootstrap.rtl.min.css (100%) rename {templates => api/templates}/assets/dist/css/bootstrap.rtl.min.css.map (100%) rename {templates => api/templates}/assets/dist/js/bootstrap.bundle.min.js (100%) rename {templates => api/templates}/assets/dist/js/bootstrap.bundle.min.js.map (100%) rename {templates => api/templates}/assets/js/color-modes.js (100%) rename {templates => api/templates}/homepage/index.html (100%) rename {templates => api/templates}/index.html (100%) rename {templates => api/templates}/jumbotron/index.html (100%) create mode 100644 api/templates/login/index.html rename {templates => api/templates}/navbar/index.html (82%) rename {templates => api/templates}/sign-in/index.html (100%) rename {templates => api/templates}/sign-in/sign-in.css (100%) rename {templates => api/templates}/themeSelector/index.html (100%) rename {types => api/types}/userTypes.go (100%) delete mode 100644 database/ent/runtime.go diff --git a/.gitignore b/.gitignore index f10862a..72932e1 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -/.env +/api/.env diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 306239b..9070a20 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,6 +1,7 @@ + \ No newline at end of file diff --git a/Dockerfile b/api/Dockerfile similarity index 97% rename from Dockerfile rename to api/Dockerfile index 7828b04..7f9a59a 100644 --- a/Dockerfile +++ b/api/Dockerfile @@ -4,7 +4,7 @@ FROM golang:latest # Set the working directory to /app WORKDIR . # Copy the current directory contents into the container at /app -COPY . . +COPY .. . # Download and install any required dependencies RUN go mod download diff --git a/api/apiRoutes.go b/api/api/apiRoutes.go similarity index 86% rename from api/apiRoutes.go rename to api/api/apiRoutes.go index 9bf294a..5ba8f1c 100644 --- a/api/apiRoutes.go +++ b/api/api/apiRoutes.go @@ -11,4 +11,5 @@ func ApiRoutes(mux **http.ServeMux) { m.HandleFunc("/api/", handler.CatchAllHandler) m.HandleFunc("POST /api/user", handler.CreateUser) m.HandleFunc("GET /api/user/{id}", handler.GetUser) + m.HandleFunc("POST /api/login", handler.Login) } diff --git a/api/api/handler/authHandler.go b/api/api/handler/authHandler.go new file mode 100644 index 0000000..1720e57 --- /dev/null +++ b/api/api/handler/authHandler.go @@ -0,0 +1,51 @@ +package handler + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "portfolio/database/ent" + "portfolio/database/ent/user" + "portfolio/database/query" + "portfolio/service/bcrypt" +) + +func Login(w http.ResponseWriter, r *http.Request) { + fmt.Println("test") + var u *ent.User + + isHtmx := r.Header.Get("HX-Request") + + if isHtmx == "true" { + u = &ent.User{ + Name: r.PostFormValue("name"), + Password: r.PostFormValue("password"), + Role: user.Role(r.PostFormValue("role")), + } + } else { + err := json.NewDecoder(r.Body).Decode(&u) + if err != nil { + InternalServerErrorHandler(w) + } + } + + User, err := query.GetLogin(context.Background(), u.Name) + if err != nil { + return + } + + if bcrypt.CheckPasswordHash(u.Password, User.Password) { + w.Header().Set("HX-Location", "/") + return + } else { + + } + + w.Header().Set("Content-Type", "application/json") + + err = json.NewEncoder(w).Encode(User) + if err != nil { + return + } +} diff --git a/api/handler/errorHandlers.go b/api/api/handler/errorHandlers.go similarity index 100% rename from api/handler/errorHandlers.go rename to api/api/handler/errorHandlers.go diff --git a/api/handler/mainHandler.go b/api/api/handler/mainHandler.go similarity index 100% rename from api/handler/mainHandler.go rename to api/api/handler/mainHandler.go diff --git a/api/handler/projectHandler.go b/api/api/handler/projectHandler.go similarity index 100% rename from api/handler/projectHandler.go rename to api/api/handler/projectHandler.go diff --git a/api/handler/userHandler.go b/api/api/handler/userHandler.go similarity index 100% rename from api/handler/userHandler.go rename to api/api/handler/userHandler.go diff --git a/api/webHandler/aboutpageHandler.go b/api/api/webHandler/aboutpageHandler.go similarity index 100% rename from api/webHandler/aboutpageHandler.go rename to api/api/webHandler/aboutpageHandler.go diff --git a/api/webHandler/homepageHandler.go b/api/api/webHandler/homepageHandler.go similarity index 100% rename from api/webHandler/homepageHandler.go rename to api/api/webHandler/homepageHandler.go diff --git a/api/api/webHandler/loginpageHandler.go b/api/api/webHandler/loginpageHandler.go new file mode 100644 index 0000000..00159e6 --- /dev/null +++ b/api/api/webHandler/loginpageHandler.go @@ -0,0 +1,24 @@ +package webHandler + +import ( + "html/template" + "net/http" +) + +func InitLoginpage(w http.ResponseWriter, r *http.Request) { + + tmpl := template.Must(template.ParseFiles( + "./templates/login/index.html", + "./templates/navbar/index.html", + "./templates/themeSelector/index.html", + )) + + err := tmpl.Execute(w, nil) + if err != nil { + _, err := w.Write([]byte("failed to load")) + if err != nil { + return + } + return + } +} diff --git a/api/webHandler/projectpageHandler.go b/api/api/webHandler/projectpageHandler.go similarity index 100% rename from api/webHandler/projectpageHandler.go rename to api/api/webHandler/projectpageHandler.go diff --git a/api/webRoutes.go b/api/api/webRoutes.go similarity index 90% rename from api/webRoutes.go rename to api/api/webRoutes.go index 17c47d9..51e52ee 100644 --- a/api/webRoutes.go +++ b/api/api/webRoutes.go @@ -11,6 +11,7 @@ func WebRoutes(mux **http.ServeMux) { m.HandleFunc("GET /{$}", webHandler.InitHomepage) m.HandleFunc("GET /projecten/{$}", webHandler.InitProjectpage) m.HandleFunc("GET /about/{$}", webHandler.InitAboutpage) + m.HandleFunc("GET /login/{$}", webHandler.InitLoginpage) m.HandleFunc("POST /theme", webHandler.UpdateTheme) diff --git a/database/database.go b/api/database/database.go similarity index 100% rename from database/database.go rename to api/database/database.go diff --git a/database/ent/client.go b/api/database/ent/client.go similarity index 100% rename from database/ent/client.go rename to api/database/ent/client.go diff --git a/database/ent/ent.go b/api/database/ent/ent.go similarity index 100% rename from database/ent/ent.go rename to api/database/ent/ent.go diff --git a/database/ent/enttest/enttest.go b/api/database/ent/enttest/enttest.go similarity index 100% rename from database/ent/enttest/enttest.go rename to api/database/ent/enttest/enttest.go diff --git a/database/ent/generate.go b/api/database/ent/generate.go similarity index 100% rename from database/ent/generate.go rename to api/database/ent/generate.go diff --git a/database/ent/hook/hook.go b/api/database/ent/hook/hook.go similarity index 100% rename from database/ent/hook/hook.go rename to api/database/ent/hook/hook.go diff --git a/database/ent/migrate/migrate.go b/api/database/ent/migrate/migrate.go similarity index 100% rename from database/ent/migrate/migrate.go rename to api/database/ent/migrate/migrate.go diff --git a/database/ent/migrate/schema.go b/api/database/ent/migrate/schema.go similarity index 96% rename from database/ent/migrate/schema.go rename to api/database/ent/migrate/schema.go index f0bd4f8..8f9d96f 100644 --- a/database/ent/migrate/schema.go +++ b/api/database/ent/migrate/schema.go @@ -42,7 +42,8 @@ var ( // UsersColumns holds the columns for the "users" table. UsersColumns = []*schema.Column{ {Name: "id", Type: field.TypeInt, Increment: true}, - {Name: "name", Type: field.TypeString, Default: "John doe"}, + {Name: "name", Type: field.TypeString, Unique: true}, + {Name: "password", Type: field.TypeString}, {Name: "role", Type: field.TypeEnum, Enums: []string{"admin", "user", "visitor"}}, } // UsersTable holds the schema information for the "users" table. diff --git a/database/ent/mutation.go b/api/database/ent/mutation.go similarity index 96% rename from database/ent/mutation.go rename to api/database/ent/mutation.go index 2bae4b4..52457af 100644 --- a/database/ent/mutation.go +++ b/api/database/ent/mutation.go @@ -932,6 +932,7 @@ type UserMutation struct { typ string id *int name *string + password *string role *user.Role clearedFields map[string]struct{} teams map[int]struct{} @@ -1076,6 +1077,42 @@ func (m *UserMutation) ResetName() { m.name = nil } +// SetPassword sets the "password" field. +func (m *UserMutation) SetPassword(s string) { + m.password = &s +} + +// Password returns the value of the "password" field in the mutation. +func (m *UserMutation) Password() (r string, exists bool) { + v := m.password + if v == nil { + return + } + return *v, true +} + +// OldPassword returns the old "password" field's value of the User entity. +// If the User object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *UserMutation) OldPassword(ctx context.Context) (v string, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldPassword is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldPassword requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldPassword: %w", err) + } + return oldValue.Password, nil +} + +// ResetPassword resets all changes to the "password" field. +func (m *UserMutation) ResetPassword() { + m.password = nil +} + // SetRole sets the "role" field. func (m *UserMutation) SetRole(u user.Role) { m.role = &u @@ -1200,10 +1237,13 @@ func (m *UserMutation) Type() string { // order to get all numeric fields that were incremented/decremented, call // AddedFields(). func (m *UserMutation) Fields() []string { - fields := make([]string, 0, 2) + fields := make([]string, 0, 3) if m.name != nil { fields = append(fields, user.FieldName) } + if m.password != nil { + fields = append(fields, user.FieldPassword) + } if m.role != nil { fields = append(fields, user.FieldRole) } @@ -1217,6 +1257,8 @@ func (m *UserMutation) Field(name string) (ent.Value, bool) { switch name { case user.FieldName: return m.Name() + case user.FieldPassword: + return m.Password() case user.FieldRole: return m.Role() } @@ -1230,6 +1272,8 @@ func (m *UserMutation) OldField(ctx context.Context, name string) (ent.Value, er switch name { case user.FieldName: return m.OldName(ctx) + case user.FieldPassword: + return m.OldPassword(ctx) case user.FieldRole: return m.OldRole(ctx) } @@ -1248,6 +1292,13 @@ func (m *UserMutation) SetField(name string, value ent.Value) error { } m.SetName(v) return nil + case user.FieldPassword: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetPassword(v) + return nil case user.FieldRole: v, ok := value.(user.Role) if !ok { @@ -1307,6 +1358,9 @@ func (m *UserMutation) ResetField(name string) error { case user.FieldName: m.ResetName() return nil + case user.FieldPassword: + m.ResetPassword() + return nil case user.FieldRole: m.ResetRole() return nil diff --git a/database/ent/predicate/predicate.go b/api/database/ent/predicate/predicate.go similarity index 100% rename from database/ent/predicate/predicate.go rename to api/database/ent/predicate/predicate.go diff --git a/database/ent/project.go b/api/database/ent/project.go similarity index 100% rename from database/ent/project.go rename to api/database/ent/project.go diff --git a/database/ent/project/project.go b/api/database/ent/project/project.go similarity index 100% rename from database/ent/project/project.go rename to api/database/ent/project/project.go diff --git a/database/ent/project/where.go b/api/database/ent/project/where.go similarity index 100% rename from database/ent/project/where.go rename to api/database/ent/project/where.go diff --git a/database/ent/project_create.go b/api/database/ent/project_create.go similarity index 100% rename from database/ent/project_create.go rename to api/database/ent/project_create.go diff --git a/database/ent/project_delete.go b/api/database/ent/project_delete.go similarity index 100% rename from database/ent/project_delete.go rename to api/database/ent/project_delete.go diff --git a/database/ent/project_query.go b/api/database/ent/project_query.go similarity index 100% rename from database/ent/project_query.go rename to api/database/ent/project_query.go diff --git a/database/ent/project_update.go b/api/database/ent/project_update.go similarity index 100% rename from database/ent/project_update.go rename to api/database/ent/project_update.go diff --git a/api/database/ent/runtime.go b/api/database/ent/runtime.go new file mode 100644 index 0000000..793d053 --- /dev/null +++ b/api/database/ent/runtime.go @@ -0,0 +1,9 @@ +// Code generated by ent, DO NOT EDIT. + +package ent + +// The init function reads all schema descriptors with runtime code +// (default values, validators, hooks and policies) and stitches it +// to their package variables. +func init() { +} diff --git a/database/ent/runtime/runtime.go b/api/database/ent/runtime/runtime.go similarity index 100% rename from database/ent/runtime/runtime.go rename to api/database/ent/runtime/runtime.go diff --git a/database/ent/schema/project.go b/api/database/ent/schema/project.go similarity index 100% rename from database/ent/schema/project.go rename to api/database/ent/schema/project.go diff --git a/database/ent/schema/team.go b/api/database/ent/schema/team.go similarity index 100% rename from database/ent/schema/team.go rename to api/database/ent/schema/team.go diff --git a/database/ent/schema/user.go b/api/database/ent/schema/user.go similarity index 92% rename from database/ent/schema/user.go rename to api/database/ent/schema/user.go index 59a6b4e..75f334d 100644 --- a/database/ent/schema/user.go +++ b/api/database/ent/schema/user.go @@ -15,7 +15,8 @@ type User struct { func (User) Fields() []ent.Field { return []ent.Field{ field.String("name"). - Default("John doe"), + Unique(), + field.String("password"), field.Enum("role"). Values("admin", "user", "visitor"), } diff --git a/database/ent/team.go b/api/database/ent/team.go similarity index 100% rename from database/ent/team.go rename to api/database/ent/team.go diff --git a/database/ent/team/team.go b/api/database/ent/team/team.go similarity index 100% rename from database/ent/team/team.go rename to api/database/ent/team/team.go diff --git a/database/ent/team/where.go b/api/database/ent/team/where.go similarity index 100% rename from database/ent/team/where.go rename to api/database/ent/team/where.go diff --git a/database/ent/team_create.go b/api/database/ent/team_create.go similarity index 100% rename from database/ent/team_create.go rename to api/database/ent/team_create.go diff --git a/database/ent/team_delete.go b/api/database/ent/team_delete.go similarity index 100% rename from database/ent/team_delete.go rename to api/database/ent/team_delete.go diff --git a/database/ent/team_query.go b/api/database/ent/team_query.go similarity index 100% rename from database/ent/team_query.go rename to api/database/ent/team_query.go diff --git a/database/ent/team_update.go b/api/database/ent/team_update.go similarity index 100% rename from database/ent/team_update.go rename to api/database/ent/team_update.go diff --git a/database/ent/tx.go b/api/database/ent/tx.go similarity index 100% rename from database/ent/tx.go rename to api/database/ent/tx.go diff --git a/database/ent/user.go b/api/database/ent/user.go similarity index 89% rename from database/ent/user.go rename to api/database/ent/user.go index 0d1158c..2e21fd0 100644 --- a/database/ent/user.go +++ b/api/database/ent/user.go @@ -18,6 +18,8 @@ type User struct { ID int `json:"id,omitempty"` // Name holds the value of the "name" field. Name string `json:"name,omitempty"` + // Password holds the value of the "password" field. + Password string `json:"password,omitempty"` // Role holds the value of the "role" field. Role user.Role `json:"role,omitempty"` // Edges holds the relations/edges for other nodes in the graph. @@ -51,7 +53,7 @@ func (*User) scanValues(columns []string) ([]any, error) { switch columns[i] { case user.FieldID: values[i] = new(sql.NullInt64) - case user.FieldName, user.FieldRole: + case user.FieldName, user.FieldPassword, user.FieldRole: values[i] = new(sql.NullString) default: values[i] = new(sql.UnknownType) @@ -80,6 +82,12 @@ func (u *User) assignValues(columns []string, values []any) error { } else if value.Valid { u.Name = value.String } + case user.FieldPassword: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field password", values[i]) + } else if value.Valid { + u.Password = value.String + } case user.FieldRole: if value, ok := values[i].(*sql.NullString); !ok { return fmt.Errorf("unexpected type %T for field role", values[i]) @@ -130,6 +138,9 @@ func (u *User) String() string { builder.WriteString("name=") builder.WriteString(u.Name) builder.WriteString(", ") + builder.WriteString("password=") + builder.WriteString(u.Password) + builder.WriteString(", ") builder.WriteString("role=") builder.WriteString(fmt.Sprintf("%v", u.Role)) builder.WriteByte(')') diff --git a/database/ent/user/user.go b/api/database/ent/user/user.go similarity index 91% rename from database/ent/user/user.go rename to api/database/ent/user/user.go index 4499533..6f03b7f 100644 --- a/database/ent/user/user.go +++ b/api/database/ent/user/user.go @@ -16,6 +16,8 @@ const ( FieldID = "id" // FieldName holds the string denoting the name field in the database. FieldName = "name" + // FieldPassword holds the string denoting the password field in the database. + FieldPassword = "password" // FieldRole holds the string denoting the role field in the database. FieldRole = "role" // EdgeTeams holds the string denoting the teams edge name in mutations. @@ -33,6 +35,7 @@ const ( var Columns = []string{ FieldID, FieldName, + FieldPassword, FieldRole, } @@ -52,11 +55,6 @@ func ValidColumn(column string) bool { return false } -var ( - // DefaultName holds the default value on creation for the "name" field. - DefaultName string -) - // Role defines the type for the "role" enum field. type Role string @@ -94,6 +92,11 @@ func ByName(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldName, opts...).ToFunc() } +// ByPassword orders the results by the password field. +func ByPassword(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldPassword, opts...).ToFunc() +} + // ByRole orders the results by the role field. func ByRole(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldRole, opts...).ToFunc() diff --git a/database/ent/user/where.go b/api/database/ent/user/where.go similarity index 68% rename from database/ent/user/where.go rename to api/database/ent/user/where.go index e15d0c2..ce3e0ac 100644 --- a/database/ent/user/where.go +++ b/api/database/ent/user/where.go @@ -59,6 +59,11 @@ func Name(v string) predicate.User { return predicate.User(sql.FieldEQ(FieldName, v)) } +// Password applies equality check predicate on the "password" field. It's identical to PasswordEQ. +func Password(v string) predicate.User { + return predicate.User(sql.FieldEQ(FieldPassword, v)) +} + // NameEQ applies the EQ predicate on the "name" field. func NameEQ(v string) predicate.User { return predicate.User(sql.FieldEQ(FieldName, v)) @@ -124,6 +129,71 @@ func NameContainsFold(v string) predicate.User { return predicate.User(sql.FieldContainsFold(FieldName, v)) } +// PasswordEQ applies the EQ predicate on the "password" field. +func PasswordEQ(v string) predicate.User { + return predicate.User(sql.FieldEQ(FieldPassword, v)) +} + +// PasswordNEQ applies the NEQ predicate on the "password" field. +func PasswordNEQ(v string) predicate.User { + return predicate.User(sql.FieldNEQ(FieldPassword, v)) +} + +// PasswordIn applies the In predicate on the "password" field. +func PasswordIn(vs ...string) predicate.User { + return predicate.User(sql.FieldIn(FieldPassword, vs...)) +} + +// PasswordNotIn applies the NotIn predicate on the "password" field. +func PasswordNotIn(vs ...string) predicate.User { + return predicate.User(sql.FieldNotIn(FieldPassword, vs...)) +} + +// PasswordGT applies the GT predicate on the "password" field. +func PasswordGT(v string) predicate.User { + return predicate.User(sql.FieldGT(FieldPassword, v)) +} + +// PasswordGTE applies the GTE predicate on the "password" field. +func PasswordGTE(v string) predicate.User { + return predicate.User(sql.FieldGTE(FieldPassword, v)) +} + +// PasswordLT applies the LT predicate on the "password" field. +func PasswordLT(v string) predicate.User { + return predicate.User(sql.FieldLT(FieldPassword, v)) +} + +// PasswordLTE applies the LTE predicate on the "password" field. +func PasswordLTE(v string) predicate.User { + return predicate.User(sql.FieldLTE(FieldPassword, v)) +} + +// PasswordContains applies the Contains predicate on the "password" field. +func PasswordContains(v string) predicate.User { + return predicate.User(sql.FieldContains(FieldPassword, v)) +} + +// PasswordHasPrefix applies the HasPrefix predicate on the "password" field. +func PasswordHasPrefix(v string) predicate.User { + return predicate.User(sql.FieldHasPrefix(FieldPassword, v)) +} + +// PasswordHasSuffix applies the HasSuffix predicate on the "password" field. +func PasswordHasSuffix(v string) predicate.User { + return predicate.User(sql.FieldHasSuffix(FieldPassword, v)) +} + +// PasswordEqualFold applies the EqualFold predicate on the "password" field. +func PasswordEqualFold(v string) predicate.User { + return predicate.User(sql.FieldEqualFold(FieldPassword, v)) +} + +// PasswordContainsFold applies the ContainsFold predicate on the "password" field. +func PasswordContainsFold(v string) predicate.User { + return predicate.User(sql.FieldContainsFold(FieldPassword, v)) +} + // RoleEQ applies the EQ predicate on the "role" field. func RoleEQ(v Role) predicate.User { return predicate.User(sql.FieldEQ(FieldRole, v)) diff --git a/database/ent/user_create.go b/api/database/ent/user_create.go similarity index 93% rename from database/ent/user_create.go rename to api/database/ent/user_create.go index 3a80d55..9fd5373 100644 --- a/database/ent/user_create.go +++ b/api/database/ent/user_create.go @@ -26,11 +26,9 @@ func (uc *UserCreate) SetName(s string) *UserCreate { return uc } -// SetNillableName sets the "name" field if the given value is not nil. -func (uc *UserCreate) SetNillableName(s *string) *UserCreate { - if s != nil { - uc.SetName(*s) - } +// SetPassword sets the "password" field. +func (uc *UserCreate) SetPassword(s string) *UserCreate { + uc.mutation.SetPassword(s) return uc } @@ -62,7 +60,6 @@ func (uc *UserCreate) Mutation() *UserMutation { // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { - uc.defaults() return withHooks(ctx, uc.sqlSave, uc.mutation, uc.hooks) } @@ -88,19 +85,14 @@ func (uc *UserCreate) ExecX(ctx context.Context) { } } -// defaults sets the default values of the builder before save. -func (uc *UserCreate) defaults() { - if _, ok := uc.mutation.Name(); !ok { - v := user.DefaultName - uc.mutation.SetName(v) - } -} - // check runs all checks and user-defined validators on the builder. func (uc *UserCreate) check() error { if _, ok := uc.mutation.Name(); !ok { return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "User.name"`)} } + if _, ok := uc.mutation.Password(); !ok { + return &ValidationError{Name: "password", err: errors.New(`ent: missing required field "User.password"`)} + } if _, ok := uc.mutation.Role(); !ok { return &ValidationError{Name: "role", err: errors.New(`ent: missing required field "User.role"`)} } @@ -139,6 +131,10 @@ func (uc *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) { _spec.SetField(user.FieldName, field.TypeString, value) _node.Name = value } + if value, ok := uc.mutation.Password(); ok { + _spec.SetField(user.FieldPassword, field.TypeString, value) + _node.Password = value + } if value, ok := uc.mutation.Role(); ok { _spec.SetField(user.FieldRole, field.TypeEnum, value) _node.Role = value @@ -180,7 +176,6 @@ func (ucb *UserCreateBulk) Save(ctx context.Context) ([]*User, error) { for i := range ucb.builders { func(i int, root context.Context) { builder := ucb.builders[i] - builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*UserMutation) if !ok { diff --git a/database/ent/user_delete.go b/api/database/ent/user_delete.go similarity index 100% rename from database/ent/user_delete.go rename to api/database/ent/user_delete.go diff --git a/database/ent/user_query.go b/api/database/ent/user_query.go similarity index 100% rename from database/ent/user_query.go rename to api/database/ent/user_query.go diff --git a/database/ent/user_update.go b/api/database/ent/user_update.go similarity index 92% rename from database/ent/user_update.go rename to api/database/ent/user_update.go index 87453ae..102082f 100644 --- a/database/ent/user_update.go +++ b/api/database/ent/user_update.go @@ -42,6 +42,20 @@ func (uu *UserUpdate) SetNillableName(s *string) *UserUpdate { return uu } +// SetPassword sets the "password" field. +func (uu *UserUpdate) SetPassword(s string) *UserUpdate { + uu.mutation.SetPassword(s) + return uu +} + +// SetNillablePassword sets the "password" field if the given value is not nil. +func (uu *UserUpdate) SetNillablePassword(s *string) *UserUpdate { + if s != nil { + uu.SetPassword(*s) + } + return uu +} + // SetRole sets the "role" field. func (uu *UserUpdate) SetRole(u user.Role) *UserUpdate { uu.mutation.SetRole(u) @@ -149,6 +163,9 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { if value, ok := uu.mutation.Name(); ok { _spec.SetField(user.FieldName, field.TypeString, value) } + if value, ok := uu.mutation.Password(); ok { + _spec.SetField(user.FieldPassword, field.TypeString, value) + } if value, ok := uu.mutation.Role(); ok { _spec.SetField(user.FieldRole, field.TypeEnum, value) } @@ -231,6 +248,20 @@ func (uuo *UserUpdateOne) SetNillableName(s *string) *UserUpdateOne { return uuo } +// SetPassword sets the "password" field. +func (uuo *UserUpdateOne) SetPassword(s string) *UserUpdateOne { + uuo.mutation.SetPassword(s) + return uuo +} + +// SetNillablePassword sets the "password" field if the given value is not nil. +func (uuo *UserUpdateOne) SetNillablePassword(s *string) *UserUpdateOne { + if s != nil { + uuo.SetPassword(*s) + } + return uuo +} + // SetRole sets the "role" field. func (uuo *UserUpdateOne) SetRole(u user.Role) *UserUpdateOne { uuo.mutation.SetRole(u) @@ -368,6 +399,9 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) if value, ok := uuo.mutation.Name(); ok { _spec.SetField(user.FieldName, field.TypeString, value) } + if value, ok := uuo.mutation.Password(); ok { + _spec.SetField(user.FieldPassword, field.TypeString, value) + } if value, ok := uuo.mutation.Role(); ok { _spec.SetField(user.FieldRole, field.TypeEnum, value) } diff --git a/api/database/query/authQuery.go b/api/database/query/authQuery.go new file mode 100644 index 0000000..d6b5b55 --- /dev/null +++ b/api/database/query/authQuery.go @@ -0,0 +1,23 @@ +package query + +import ( + "context" + "fmt" + "log" + "portfolio/database" + "portfolio/database/ent" + "portfolio/database/ent/user" +) + +func GetLogin(ctx context.Context, name string) (*ent.User, error) { + + u, err := database.Client.User. + Query(). + Where(user.Name(name)). + Only(ctx) + if err != nil { + return nil, fmt.Errorf("failed querying user: %w", err) + } + log.Println("user returned: ", u) + return u, nil +} diff --git a/database/query/userQuery.go b/api/database/query/userQuery.go similarity index 100% rename from database/query/userQuery.go rename to api/database/query/userQuery.go diff --git a/go.mod b/api/go.mod similarity index 94% rename from go.mod rename to api/go.mod index 287c1f0..0ebe29f 100644 --- a/go.mod +++ b/api/go.mod @@ -19,6 +19,7 @@ require ( github.com/hashicorp/hcl/v2 v2.19.1 // indirect github.com/mitchellh/go-wordwrap v1.0.1 // indirect github.com/zclconf/go-cty v1.14.2 // indirect + golang.org/x/crypto v0.21.0 // indirect golang.org/x/mod v0.15.0 // indirect golang.org/x/text v0.14.0 // indirect ) diff --git a/go.sum b/api/go.sum similarity index 97% rename from go.sum rename to api/go.sum index 02b43de..8ea9ed0 100644 --- a/go.sum +++ b/api/go.sum @@ -58,6 +58,8 @@ github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/zclconf/go-cty v1.14.2 h1:kTG7lqmBou0Zkx35r6HJHUQTvaRPr5bIAf3AoHS0izI= github.com/zclconf/go-cty v1.14.2/go.mod h1:VvMs5i0vgZdhYawQNq5kePSpLAoz8u1xvZgrPIxfnZE= +golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= +golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8= golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= diff --git a/main.go b/api/main.go similarity index 97% rename from main.go rename to api/main.go index 69d04fd..b20fa96 100644 --- a/main.go +++ b/api/main.go @@ -15,7 +15,7 @@ func main() { log.Fatalf(".env not found: %v", err) return } - + print("test") // Create a new request multiplexer // Take incoming requests and dispatch them to the matching webHandler mux := http.NewServeMux() diff --git a/api/service/bcrypt/password.go b/api/service/bcrypt/password.go new file mode 100644 index 0000000..8d7289a --- /dev/null +++ b/api/service/bcrypt/password.go @@ -0,0 +1,15 @@ +package bcrypt + +import "golang.org/x/crypto/bcrypt" + +func HashPassword(password string) (string, error) { + //generate hash from password + bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14) + return string(bytes), err +} + +func CheckPasswordHash(password, hash string) bool { + //check password with hash + err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) + return err == nil +} diff --git a/service/validate/validateUser.go b/api/service/validate/validateUser.go similarity index 100% rename from service/validate/validateUser.go rename to api/service/validate/validateUser.go diff --git a/templates/about/index.html b/api/templates/about/index.html similarity index 100% rename from templates/about/index.html rename to api/templates/about/index.html diff --git a/templates/assets/brand/Darius_pasfoto_2023_Large.png b/api/templates/assets/brand/Darius_pasfoto_2023_Large.png similarity index 100% rename from templates/assets/brand/Darius_pasfoto_2023_Large.png rename to api/templates/assets/brand/Darius_pasfoto_2023_Large.png diff --git a/templates/assets/brand/bootstrap-logo-white.svg b/api/templates/assets/brand/bootstrap-logo-white.svg similarity index 100% rename from templates/assets/brand/bootstrap-logo-white.svg rename to api/templates/assets/brand/bootstrap-logo-white.svg diff --git a/templates/assets/brand/bootstrap-logo.svg b/api/templates/assets/brand/bootstrap-logo.svg similarity index 100% rename from templates/assets/brand/bootstrap-logo.svg rename to api/templates/assets/brand/bootstrap-logo.svg diff --git a/templates/assets/dist/css/bootstrap.min.css b/api/templates/assets/dist/css/bootstrap.min.css similarity index 100% rename from templates/assets/dist/css/bootstrap.min.css rename to api/templates/assets/dist/css/bootstrap.min.css diff --git a/templates/assets/dist/css/bootstrap.min.css.map b/api/templates/assets/dist/css/bootstrap.min.css.map similarity index 100% rename from templates/assets/dist/css/bootstrap.min.css.map rename to api/templates/assets/dist/css/bootstrap.min.css.map diff --git a/templates/assets/dist/css/bootstrap.rtl.min.css b/api/templates/assets/dist/css/bootstrap.rtl.min.css similarity index 100% rename from templates/assets/dist/css/bootstrap.rtl.min.css rename to api/templates/assets/dist/css/bootstrap.rtl.min.css diff --git a/templates/assets/dist/css/bootstrap.rtl.min.css.map b/api/templates/assets/dist/css/bootstrap.rtl.min.css.map similarity index 100% rename from templates/assets/dist/css/bootstrap.rtl.min.css.map rename to api/templates/assets/dist/css/bootstrap.rtl.min.css.map diff --git a/templates/assets/dist/js/bootstrap.bundle.min.js b/api/templates/assets/dist/js/bootstrap.bundle.min.js similarity index 100% rename from templates/assets/dist/js/bootstrap.bundle.min.js rename to api/templates/assets/dist/js/bootstrap.bundle.min.js diff --git a/templates/assets/dist/js/bootstrap.bundle.min.js.map b/api/templates/assets/dist/js/bootstrap.bundle.min.js.map similarity index 100% rename from templates/assets/dist/js/bootstrap.bundle.min.js.map rename to api/templates/assets/dist/js/bootstrap.bundle.min.js.map diff --git a/templates/assets/js/color-modes.js b/api/templates/assets/js/color-modes.js similarity index 100% rename from templates/assets/js/color-modes.js rename to api/templates/assets/js/color-modes.js diff --git a/templates/homepage/index.html b/api/templates/homepage/index.html similarity index 100% rename from templates/homepage/index.html rename to api/templates/homepage/index.html diff --git a/templates/index.html b/api/templates/index.html similarity index 100% rename from templates/index.html rename to api/templates/index.html diff --git a/templates/jumbotron/index.html b/api/templates/jumbotron/index.html similarity index 100% rename from templates/jumbotron/index.html rename to api/templates/jumbotron/index.html diff --git a/api/templates/login/index.html b/api/templates/login/index.html new file mode 100644 index 0000000..0d36968 --- /dev/null +++ b/api/templates/login/index.html @@ -0,0 +1,157 @@ + + + + + + + + + + Signin Template · Bootstrap v5.3 + + + + + + + + + + + + + + + + {{template "themeSelector"}} + +
+
+ +

Please sign in

+ +
+ + +
+
+ + +
+ +
+ + +
+ +

© 2017–2024

+
+
+ + + + diff --git a/templates/navbar/index.html b/api/templates/navbar/index.html similarity index 82% rename from templates/navbar/index.html rename to api/templates/navbar/index.html index 938a46a..f5e8936 100644 --- a/templates/navbar/index.html +++ b/api/templates/navbar/index.html @@ -20,10 +20,7 @@ Servers - + Login diff --git a/templates/sign-in/index.html b/api/templates/sign-in/index.html similarity index 100% rename from templates/sign-in/index.html rename to api/templates/sign-in/index.html diff --git a/templates/sign-in/sign-in.css b/api/templates/sign-in/sign-in.css similarity index 100% rename from templates/sign-in/sign-in.css rename to api/templates/sign-in/sign-in.css diff --git a/templates/themeSelector/index.html b/api/templates/themeSelector/index.html similarity index 100% rename from templates/themeSelector/index.html rename to api/templates/themeSelector/index.html diff --git a/types/userTypes.go b/api/types/userTypes.go similarity index 100% rename from types/userTypes.go rename to api/types/userTypes.go diff --git a/database/ent/runtime.go b/database/ent/runtime.go deleted file mode 100644 index 3d56e0b..0000000 --- a/database/ent/runtime.go +++ /dev/null @@ -1,20 +0,0 @@ -// Code generated by ent, DO NOT EDIT. - -package ent - -import ( - "portfolio/database/ent/schema" - "portfolio/database/ent/user" -) - -// The init function reads all schema descriptors with runtime code -// (default values, validators, hooks and policies) and stitches it -// to their package variables. -func init() { - userFields := schema.User{}.Fields() - _ = userFields - // userDescName is the schema descriptor for name field. - userDescName := userFields[0].Descriptor() - // user.DefaultName holds the default value on creation for the name field. - user.DefaultName = userDescName.Default.(string) -} diff --git a/docker-compose.yml b/docker-compose.yml index 27b2298..240c979 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,7 +2,7 @@ version: '3.8' services: api: - build: . + build: ./api ports: - "4002:4002" restart: unless-stopped \ No newline at end of file