Payment of ₹ requested by HONEY VIG. Click the link to pay securely via Razorpay: Make 5000 payment Payment of ₹ requested by HONEY VIG. Click the link to pay securely via Razorpay: Make 5000 payment ⭐ If you would like to buy me a coffee, well thank you very much that is mega kind! : https://www.buymeacoffee.com/honeyvig
Hire a web Developer and Designer to upgrade and boost your online presence with cutting edge Technologies

Sunday, April 30, 2023

Creating REST API with Golang

 

REST (Representational State Transfer) is the most used interface to populate the dynamic data and perform operations such as add, update and delete in web applications. The REST APIs are used in all kinds applications to make GET, POST, PUT or DELETE HTTP requests to perform CRUD operations on data.

In this tutorial, we are going to explain to create simple REST API with Golang to perform GET, POST, DELETE and PUT on dynamic data. We will mainly focus on the basic concepts of this, so we will perform actions on employee JSON data instead of database. So you can use this by connection with database as per your application requirement. 

 

1. Create Server to Handle API Requests

We need to create server to handle HTTP requests to the API. So we will create a function apiRequests() in main.go file and called within func main(). The function apiRequests() will handle all requests to the root URL.

package main

import (
    "fmt"
    "log"
    "net/http"
)

func homePage(w http.ResponseWriter, r *http.Request){
    fmt.Fprintf(w, "Welcome to the API Home page!")    
}

func apiRequests() {
    http.HandleFunc("/", homePage)
    log.Fatal(http.ListenAndServe(":3000", nil))
}

func main() {
    apiRequests()
}

When we run the above code, the API will start on post 3000. The URL http://localhost:3000/ will be loaded on browser and see the API home page welcome message and it means we have created the base of our REST API.

2. Create Employee Dummy Data for REST API

In this tutorial we will create simple REST API to perform CRUD operations GET, POST, DELETE and PUT employee data. We will create some dummy employee data in func main(). We will perform

func main() {
	Employees = []Employee{
		Employee{Id: "1", Name: "Jhon Smith", Address: "New Jersy USA", Salary: "20000"},
		Employee{Id: "2", Name: "William", Address: "Wellington Newziland", Salary: "12000"},
		Employee{Id: "3", Name: "Adam", Address: "London England", Salary: "15000"},
	}
}

3. Get All Employee

Now we will start implementing our REST API methods. We will implement function getAllEmployees() to get all employee data.

func getAllEmployees(w http.ResponseWriter, r *http.Request) {    
    json.NewEncoder(w).Encode(Employees)
}

We will also handle routing and call function getAllEmployees() on action employees to get all employee in JSON format. We will use gorilla/mux based HTTP router in place of the standard library and use in this tutorial example.

 

func apiRequests() {
    route := mux.NewRouter().StrictSlash(true)    
    route.HandleFunc("/employees", getAllEmployees)   
    log.Fatal(route.ListenAndServe(":3000", route))
}

When we load URL http://localhost:3000/employees on method GET then it will display all employee in JSON format.

{"Id":"1","Name":"Jhon Smith","Address":"New Jersy USA","Salary":"20000"},
{"Id":"2","Name":"William","Address":"Wellington Newziland","Salary":"12000"},
{"Id":"3","Name":"Adam","Address":"London England","Salary":"15000"}

We can also get the specific employee record by passing employee id. For this we will create function getEmployee() to get employee data.

func getEmployee(w http.ResponseWriter, r *http.Request) {
    vars := mux.Vars(r)
    key := vars["id"]
    for _, employee := range Employees {
        if employee.Id == key {
            json.NewEncoder(w).Encode(employee)
        }
    }
}

we will call function getEmployee() on route /employee/{id} to get the employee by id.

func apiRequests() {
    route := mux.NewRouter().StrictSlash(true)
    route.HandleFunc("/employee/{id}", getEmployee)
    log.Fatal(http.ListenAndServe(":3000", route))
}

When we load URL http://localhost:3000/employee/3 then it will display employee by that id in JSON format.

{"Id":"3","Name":"Adam","Address":"London England","Salary":"15000"}

4. Create Employee Record

We will implement functionality to create a new employee record. We will create function createEmployee() to add a new employee record.

func createEmployee(w http.ResponseWriter, r *http.Request) {        
    reqBody, _ := ioutil.ReadAll(r.Body)
    var employee Employee 
    json.Unmarshal(reqBody, &employee)    
    Employees = append(Employees, employee)
    json.NewEncoder(w).Encode(employee)
}

We will call function createEmployee() on method POST and route employee to post employee details to add a new record.

func apiRequests() {    
    route.HandleFunc("/employee", createEmployee).Methods("POST")   
    log.Fatal(http.ListenAndServe(":3000", route))
}

When we run our API and POST with following post body then it will create a new employee record.

{
    "Id": "4", 
    "Name": "Peter", 
    "Address": "London, UK", 
    "Salary": "40000" 
}

When we access URL with employee id 4 like http://localhost:3000/employee/4 then it will display employee by that id in JSON format.

{"Id":"4","Name":"Peter","Address":"London, UK","Salary":"40000"}

5. Delete Employee Record

We will implement employee delete functionality by creating function deleteEmployee() to delete employee by id.

func deleteEmployee(w http.ResponseWriter, r *http.Request) {
    vars := mux.Vars(r)
    id := vars["id"]
    for index, employee := range Employees {
        if employee.Id == id {
            Employees = append(Employees[:index], Employees[index+1:]...)
        }
    }
}

We will call function deleteEmployee() on route /employee/{id} to delete employee.

func apiRequests() {
    route := mux.NewRouter().StrictSlash(true)    
    route.HandleFunc("/employee/{id}", deleteEmployee).Methods("DELETE")  
    log.Fatal(http.ListenAndServe(":3000", route))
}

When we will make HTTP request with method DELETE to URL http://localhost:3000/employee/3 with employee id. It will delete that employee.

6. Complete REST API Code

Here is the complete running code for this example.

package main

import (
    "encoding/json"
    "fmt"
    "log"
    "io/ioutil"
    "net/http"

    "github.com/gorilla/mux"
)

type Employee struct {
    Id      string    `json:"Id"`
    Name    string `json:"Name"`
    Address string `json:"Address"`
    Salary  string `json:"Salary"`
}

var Employees []Employee

func homePage(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Welcome to the API Home page!")    
}

func getAllEmployees(w http.ResponseWriter, r *http.Request) {    
    json.NewEncoder(w).Encode(Employees)
}

func getEmployee(w http.ResponseWriter, r *http.Request) {
    vars := mux.Vars(r)
    key := vars["id"]
    for _, employee := range Employees {
        if employee.Id == key {
            json.NewEncoder(w).Encode(employee)
        }
    }
}


func createEmployee(w http.ResponseWriter, r *http.Request) {        
    reqBody, _ := ioutil.ReadAll(r.Body)
    var employee Employee 
    json.Unmarshal(reqBody, &employee)    
    Employees = append(Employees, employee)
    json.NewEncoder(w).Encode(employee)
}

func deleteEmployee(w http.ResponseWriter, r *http.Request) {
    vars := mux.Vars(r)
    id := vars["id"]
    for index, employee := range Employees {
        if employee.Id == id {
            Employees = append(Employees[:index], Employees[index+1:]...)
        }
    }
}

func apiRequests() {
    route := mux.NewRouter().StrictSlash(true)
    route.HandleFunc("/", homePage)
    route.HandleFunc("/employees", getAllEmployees)
    route.HandleFunc("/employee", createEmployee).Methods("POST")
    route.HandleFunc("/employee/{id}", deleteEmployee).Methods("DELETE")
    route.HandleFunc("/employee/{id}", getEmployee)
    log.Fatal(http.ListenAndServe(":3000", route))
}

func main() {
    Employees = []Employee{
        Employee{Id: "1", Name: "Jhon Smith", Address: "New Jersy USA", Salary: "20000"},
        Employee{Id: "2", Name: "William", Address: "Wellington Newziland", Salary: "12000"},
		Employee{Id: "3", Name: "Adam", Address: "London England", Salary: "15000"},
    }
    apiRequests()
}

Conclusion

Here in this tutorial, we have implemented HTTP methods GET, POST and DELETE to read, add and delete employee data. We have not implemented PUT method to update employee data. You can try this at your end and share your suggestion if any regarding this.

Thursday, April 27, 2023

Create Material Design Login and Register Form

 

Google’s Material Design is getting more popular on each passing day due its awesome design and features. Now more designers started thinking about to design website using Material Design.

The Material Design uses more grid-based layouts, responsive animations and transitions, padding, and depth effects such as lighting and shadows.

So if you’re thinking about designing your user login and register form using Material Design, then you’re here at right place. In this tutorial you will learn how to design user Login and Register Form using Material Design.

We will cover this tutorial in easy steps to design user login and register using Material Design with live demo. 

 

So let’s start,

Before begin, take a look of files structure used for this tutorial.

  • login.php
  • register.php

Include Material Design CSS and JavaScript Files
First of all we will need to include Material Design CSS and JavaScript files to create HTML using Material Design. It comes with both the minified and unminified CSS and JavaScript CDN files. We just need to include following files to start using Material Design.

 

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.1/css/materialize.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.1/js/materialize.min.js"></script>

Create Material Design Login Form
After including Material Design CSS and JavaScript files, we will start design login form. The Material Design provided great look and feel to Form inputs. We just need to use class .input-field div wrapping input and label. It will enable input border light up and clearly indicating which field the user is currently editing. Material Design also enable us to add an icon prefix to make the Form inputs label more clear. We just need to add class prefix with input related class to display icon prefix. Below complete login Form design using Material Design. In this login Form, we have used email, password and remember me input with relevant icons.

<h4>Material Design Login Form</h4>
<div id="user-login" class="row">
	<div class="col s12 z-depth-6 card-panel">
		<form class="login-form">
			<div class="row margin">
				<div class="input-field col s12">
					<i class="mdi-social-person-outline prefix"></i>
					<input class="validate" id="user_email" type="email">
					<label for="email" data-error="wrong" data-success="right" class="center-align">Email</label>
				</div>
			</div>
			<div class="row margin">
				<div class="input-field col s12">
					<i class="mdi-action-lock-outline prefix"></i>
					<input id="user_pass" type="password">
					<label for="password">Password</label>
				</div>
			</div>
			<div class="row">          
				<div class="input-field col s12 m12 l12  login-text">
					<input type="checkbox" id="remember-me" />
					<label for="remember-me">Remember me</label>
				</div>
			</div>
			<div class="row">
				<div class="input-field col s12">
					<a href="login.html" class="btn waves-effect waves-light col s12">Login</a>
				</div>
			</div>
			<div class="row">
				<div class="input-field col s6 m6 l6">
					<p class="margin medium-small"><a href="register.php">Register Now!</a></p>
				</div>               
			</div>
		</form>
	</div>
</div>

Create Material Design Register Form
Here is complete register Form HTML designed using Material Design. In this Register From, we have used username, email, password inputs with relevant icons.

<h4>Material Design Register Form</h4>	
<div id="register-page" class="row">
	<div class="col s12 z-depth-6 card-panel">
		<form class="register-form">        
			<div class="row margin">
				<div class="input-field col s12">
					<i class="mdi-social-person-outline prefix"></i>
					<input id="user_name" type="text" class="validate">
					<label for="user_name" class="center-align">Username</label>
				</div>
			</div>
			<div class="row margin">
				<div class="input-field col s12">
					<i class="mdi-communication-email prefix"></i>
					<input id="user_email" type="email" class="validate">
					<label for="user_email" class="center-align">Email</label>
				</div>
			</div>
			<div class="row margin">
				<div class="input-field col s12">
					<i class="mdi-action-lock-outline prefix"></i>
					<input id="user_passw" type="password" class="validate">
					<label for="user_passw">Password</label>
				</div>
			</div>
			<div class="row margin">
				<div class="input-field col s12">
					<i class="mdi-action-lock-outline prefix"></i>
					<input id="confirm_pass" type="password">
					<label for="confirm_pass">Re-type password</label>
				</div>
			</div>
			<div class="row">
				<div class="input-field col s12">
					<a href="register.html" class="btn waves-effect waves-light col s12">Register Now</a>
				</div>
				<div class="input-field col s12">
					<p class="margin center medium-small sign-up">Already have an account? <a href="login.php">Login</a></p>
				</div>
			</div>
		</form>
	</div>
</div>