Skip to main content

Posts

Showing posts from February 10, 2019

GO API: Utill

GO API: Utill constant.go package utils const PORT string = "9090" const COCKROACHDB_USERNAME_ENV string = "username" const COCKROACHDB_DB_ENV string = "students" const COCKROACHDB_HOST_ENV string = "localhost" const COCKROACHDB_PORT_ENV string = "26257" rest_util.go package utils import ( "encoding/json" "net/http" ) func WriteHttpResponse(w http.ResponseWriter, v interface{}, err error) { if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } js, err := json.Marshal(v) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) w.Write(js) } func WriteEmptyHttpResponse(w htt...

GO API: Model

GO API: Model goapi/server/src/test.com/goapi/app/model/student.go package model type Student struct { // Roll Number of the student RollNumber int `json:"roll_number,omitempty"` // Name of the student Name string `json:"name,omitempty"` // Branch of the student Branch string `json:"branch,omitempty"` // Year of admission Batch string `json:"batch,omitempty"` } Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GO API: API Definition

GO API: API Definition student.go package apis import ( "github.com/gorilla/mux" . "test.com/goapi/app/utils" . "test.com/goapi/app/db" . "test.com/goapi/app/model" "net/http" "encoding/json" "strconv" ) func GetAllStudentInfo(w http.ResponseWriter, r *http.Request) { students := GetStudentsDB() WriteHttpResponse(w, students, nil) } func GetStudentInfo(w http.ResponseWriter, r *http.Request) { params := mux.Vars(r) rollNumber := params["student-id"] student := GetStudentDB(rollNumber) WriteHttpResponse(w, student, nil) } func CreateStudentInfo(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() decoder := json.NewDecoder(r.Body) var student Student err := decoder.Decode(&student) if err == nil { ...

GO API: Database Connection

GO API: Database Connection client_db.go package db import ( "database/sql" _ "github.com/lib/pq" . "test.com/goapi/app/utils" "sync" "log" ) var ( db *sql.DB ConnOnce sync.Once ) // create database connection func GetDBConnection() *sql.DB { var err error ConnOnce.Do(func() { db, err = sql.Open("postgres", "postgresql://"+COCKROACHDB_USERNAME_ENV+"@"+COCKROACHDB_HOST_ENV+":"+COCKROACHDB_PORT_ENV+ "/"+COCKROACHDB_DB_ENV+"?"+"sslmode=disable") }) if err != nil { log.Fatal("error connecting to the database: ", err) } return db } func ExecuteQuery(query string, args string) *sql.Rows{ db := GetDBConnection() if args == "" { rows, ...

GO API: MAIN FUNC

GO API: MAIN FUNC package main import ( sw "test.com/goapi/app" "log" "net/http" ) func main() { router := sw.NewRouter() log.Println("listen on:9090") log.Fatal(http.ListenAndServe(":"+"9090", router)) } Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GO API: Router

GO API: Router In this post, we will define router for our GOLANG API project. For now, copy below code and paste it in router.go file package app import ( "github.com/gorilla/mux" . "test.com/goapi/app/apis" "net/http" "fmt" ) type Route struct { Name string Method string Pattern string HandlerFunc http.HandlerFunc } type Routes []Route func NewRouter() *mux.Router { router := mux.NewRouter().StrictSlash(true) for _, route := range routes { var handler http.Handler handler = route.HandlerFunc router. Methods(route.Method). Path(route.Pattern). Name(route.Name). Handler(handler) } return router } func Index(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "...

GO API: Database

GO API: Database In this post, we will create database for our GOLANG API project. For this series for tutorial we are using cockroachDB CockroachDb If you have not installed cockroachDb yet. Please follow this post  Install CockroachDB . Create Database Start a CockroachDB node and access cockroachDB client shell. If you are not familiar with this you can follow this post  CockroachDB Client Command . Once you have access to cockroachdb client create database  Create a User Create database Create table Grant All access to User Create User Create cockroachDB user  CREATE USER USERNAME; Create Database CREATE DATABASE students; Create Table use students; CREATE TABLE students.student_tlb ( roll_number INT PRIMARY KEY, name STRING, batch STRING, branch STRING ); Grant Access GRANT ALL ON TABLE students.* TO USERNAME; Extra Let's insert some dummy data into student_tlb...