Skip to content

primekobie/shelf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

shelf

shelf is a small Go package that provides helper functions to simplify common HTTP request and response handling tasks.

Installation

go get github.com/primekobie/shelf

Features

  • JSON helper functions

Example

package main

import (
    "net/http"
    "github.com/primekobie/shelf"
)

type User struct {
    ID   int    `json:"id"`
    Name string `json:"name"`
}

func userHandler(w http.ResponseWriter, r *http.Request) {
    user := User{ID: 1, Name: "John Doe"}
    headers := http.Header{}
    headers.Set("X-Custom-Header", "value")

    err := shelf.WriteJSON(w, http.StatusOK, user, headers)
    if err != nil {
        // Handle error
    }
}

func updateHandler(w http.ResponseWriter, r *http.Request) {
    var payload struct {  Status string `json:"status"` }
    
    err := shelf.ReadJSON(r, &payload) // Read JSON
    if err != nil {
        // Handle error, for example:
        http.Error(w, err.Error(), http.StatusBadRequest)
        return
    }

    // Process the payload...
    shelf.WriteJSON(w, http.StatusOK, map[string]string{"status": "updated"}, nil)
}

About

extention functions for net/http

Resources

License

Stars

Watchers

Forks

Contributors

Languages