pkg

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2019 License: Apache-2.0 Imports: 0 Imported by: 0

README

pkg

This repository contains common golang packages

Using this package

If you are using Go Modules you can just reference the package directly in your code and and run go mod download or tidy, this would update your go.mod file

Http package

This package contains common methods helpful when implementing http web servers using Go

Starting the server

http package uses gorilla mux router which is a widely used HTTP router in the go community. You can simply setup your route collection and instantiate a new instance of the server and call Start method to start the server. See a simple example below

package main

import (
    "net/http"
     ws "github.com/appsbyram/pkg/http"

)

func main() {
    routes := ws.Routes{
        ws.Route{
            "Home",
            "GET",
            "/".
            homeHandler,
        },
    }

    srv := ws.NewServer("8080", false, "", "", routes)
    srv.Start()
}

func homeHandler() http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request){
        //Handle 
    }
}
Reading from Request and Response Payloads Writing to Response

If you end up writing microservices in Go you typically find yourself having to read some data primarily JSON, Yaml and XML from request and Unmarshall into a struct, another use case here is you might call another service and that service sends back from response which you need to read and Unmarshall into a Go struct. Http package contains an interface called Payload that allows you to do just that without having to write the same code over and over again in various services.

Reading from Request

Here is a simple example that demonstrates how to read data from request and unmarshall to a Go Struct. Lets imagine we have a handler function called postHandler for one of the routes that our server supports.

type SomeModel struct {
    //fields
}

func postHandler() http.HandlerFunc {
    return func (w http.ResponseWriter, r *http.Request) {
        var model SomeModel 

        p := ws.NewPayload()
        err := p.ReadRequest(ws.ContentTypeJSON, &model, r)

        //Handle error if required
    }
}
Reading from Response

Here is a simple example that demonstrates how to read data from response and unmarshall to Go struct. Lets imagine we have a handler function call for one of the routes that our server supports which invokes another service and receives a response and we need to Unmarshall the response payload into a Go struct

type SomeModel struct {
    //fields
}

func postHandler() http.HandlerFunc {
    return func (w http.ResponseWriter, r *http.Request) {
        var model SomeModel 

        //setup request

        //setup http client

        //make the call
        resp, err := client.Do(req)
        
        //Handle error if required

        //read response
        p := ws.NewPayload()
        err := p.ReadResponse(ws.ContentTypeJSON, &model, resp)
        //Handle error if required
    }
}

Documentation

Overview

========================================================================= Copyright © 2019 AppsByRam authors

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. =========================================================================

Directories

Path Synopsis
========================================================================= Copyright © 2019 AppsByRam authors.
========================================================================= Copyright © 2019 AppsByRam authors.
========================================================================= Copyright © 2019 AppsByRam authors.
========================================================================= Copyright © 2019 AppsByRam authors.

Jump to

Keyboard shortcuts

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