go-admin

command module
v0.0.0-...-9d51557 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2022 License: MIT Imports: 4 Imported by: 0

README

Go Admin App

A simple backend app/API that lets you create/manage users, products and their orders. Source code is based on this Udemy course. The server runs on port 3000 and depends on a MySQL database.

How to Run?

  • Directly
    export GO111MODULE=on
    go run main.go
    
  • With Realize
    export GO111MODULE=on
    realize start
    
  • In Docker (runs on port 8000 on the host)
    docker-compose up --build
    

Resources

Code Structure

controllers

This directory contains the codes that handle all operations on specific database models

authController
Function Description API Endpoint Method Body/Params
Register Registers a new user /api/register POST
    json
    {
        "first_name": "Harry",
        "last_name": "Potter",
        "email": "hjp@hogwarts.edu",
        "password": "patronus",
        "password_confirm": "patronus"
    }
    
Login Logs in a user
Set a JWT token in the cookie.
/api/login POST
    json
    {
        "email": "hjp@hogwarts.edu",
        "password": "patronus",
    }
    
User Get the logged-in user's info (except password) /api/user GET

    
Logout Logs out the logged-in user
Removes the JWT token cookie.
/api/logout POST

    
UpdateInfo Updates the logged-in user's info (except password) /api/users/info PUT
    json
    {
        "first_name": "Harry",
        "last_name": "Potter",
        "email": "hjp@hogwarts.edu",
    }
    
UpdatePassword Updates the password for the logged-in user /api/users/password PUT
    json
    {
        "password": "crucio",
        "password_confirm": "crucio"
    }
    
imageController
Function Description API Endpoint Method Body/Params
Upload Uploads an image to uploads directory /api/upload POST
    form-data
    {
        "image": "file_blob"
    }
    
orderController
Function Description API Endpoint Method Body/Params
AllOrders Gets all orders in the DB, and paginates the result /api/orders GET

    
Export Exports orders into a CSV file in csv/orders.csv /api/export POST

    
Chart Returns per-day orders /api/chart GET

    
permissionController
Function Description API Endpoint Method Body/Params
AllPermissions Get all permission types from DB /api/permissions GET

    
productController
Function Description API Endpoint Method Body/Params
AllProducts Get all the products from DB, and paginate result /api/products GET

    
CreateProduct Create a Product in DB. /api/products POST
    json
    {
        "title": "Wand",
        "description": "MX1000",
        "image": "not found",
        "price": 1000
    }
    
GetProduct Get the product with specified id /api/products/{id} GET

    
UpdateProduct Update the product details of a specific product /api/products/{id} PUT
    json
    {
        "title": "Wand",
        "description": "MX1000",
        "image": "not found",
        "price": 1100
    }
    
DeleteProduct Deletes the specified product /api/products/{id} DELETE

    
roleController
Function Description API Endpoint Method Body/Params
AllRoles Get all the roles from DB, and paginate result /api/roles GET

    
CreateRole Create a Role in DB. /api/roles POST
    json
    {
        "name": "Administrator",
        "permissions": [1, 2, 3, 7]
    }
    
GetRole Get the role with specified id /api/roles/{id} GET

    
UpdateRole Update the role details of a specific role /api/roles/{id} PUT
    json
    {
        "name": "Admin",
    }
    
DeleteRole Deletes the specified role /api/roles/{id} DELETE

    
userController
Function Description API Endpoint Method Body/Params
AllUsers Get all the users from DB, and paginate result /api/users GET

    
CreateUser Create a User in DB. /api/users POST
    json
    {
        "first_name": "Hermione",
        "last_name": "Granger"
        "email": "hjg@hogwarts.edu"
    }
    
GetUser Get the user with specified id /api/users/{id} GET

    
UpdateUser Update the user details of a specific user /api/users/{id} PUT
    json
    {
        "first_name": "Hermione",
        "last_name": "Granger"
        "email": "hjg@hogwarts.edu"
    }
    
DeleteUser Deletes the specified user /api/users/{id} DELETE

    

database

This directory contains a single file connect.go that is responsible for:

  • Connecting to the [MySQL] database
  • Creating the necessary tables during startup (if not already present).
  • Exports the database variable so that it can be imported in other packages.

middlewares

This directory contains codes for authentication and authorization to be used for access control across various routes.

authMiddleware
  • Parses the "jwt" cookie
  • If the cookie is present and valid, it allows privileged access to private routes.
  • If the cookie is not present or is invalid, trying to access private routes results in an UnauthorizedAccess error.
  • This is included in routes, placed strategically before the private routes.
permissionMiddleware
  • Checks if the jwt token cookie is present and valid.
  • For the given user_id (from cookie) and for the given page, checks if the page is accessible.
  • For GET requests to a page, the user must have either view_{page} or edit_{page} access.
  • For other requests, the user must have edit_{page} access.

models

This directory contains codes that define various database models (tables), and provides an interface for them.

ER Diagram:

entity

Creates a template/interface entity with two function definitions:

  • Count() that counts the number of entities in the database table.
  • Take() that gets the data for the entity from the database table and paginates them.
order
  • Defines two models, namely:

    • Order: a model to store information about who ordered and when.
    • OrderItems: a model to store actual ordered items, with OrderId to reference Order.
  • Defines its implementations of Count() and Take() for Order.

  • Defines two meta fields: Name and Total that are not stored in the database but are returned during the call to Take() to be used by the frontend later.

paginate

Defines functionality to paginate the list of entities to be used by controllers during GET requests.

permission

Defines the Permission model to handle permission types.

product

Defines the Product model and its implementation of Count() and Take()

role

Defines the Role model with a many-to-many relationship with the Permission model.

user
  • Defines the User model with a foreign key to Role
  • Implements the Count() and Take() functions
  • Implements methods to Check and Set password for the user.

routes

The directory contains a single file route.go that defines the various endpoints that this application supports, the associated methods as well as the functions that handle these endpoints.

util

This directory contains utility/helper functions. Currently, this only contains one file jwt.go that contains functions to:

  • Generate a JWT token for the logged-in user.
  • Parse the stored JWT token, determine whether it is valid, and return the issuer (userID) if it is.

.realize.yml

The configuration file for realize that can be used to automatically restart the server when any of the code files change.

main.go

The file where the main package is defined. It:

  • Connects to the database.
  • Defines the CORS policy.
  • Sets up routes that this application supports.
  • Starts the server at port 3000.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL