{"id":18474190,"url":"https://github.com/usmanhalalit/hutplate","last_synced_at":"2025-06-17T22:35:40.833Z","repository":{"id":57548912,"uuid":"93928470","full_name":"usmanhalalit/hutplate","owner":"usmanhalalit","description":"A Go library over standard net/http library with auth, session, err handling and more.","archived":false,"fork":false,"pushed_at":"2020-04-25T12:13:54.000Z","size":21,"stargazers_count":28,"open_issues_count":0,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-08T12:47:06.330Z","etag":null,"topics":["auth","error-handling","flash-messages","go","session"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/usmanhalalit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"usmanhalalit"}},"created_at":"2017-06-10T09:16:46.000Z","updated_at":"2023-09-28T12:56:28.000Z","dependencies_parsed_at":"2022-08-28T11:31:32.698Z","dependency_job_id":null,"html_url":"https://github.com/usmanhalalit/hutplate","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/usmanhalalit/hutplate","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/usmanhalalit%2Fhutplate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/usmanhalalit%2Fhutplate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/usmanhalalit%2Fhutplate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/usmanhalalit%2Fhutplate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/usmanhalalit","download_url":"https://codeload.github.com/usmanhalalit/hutplate/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/usmanhalalit%2Fhutplate/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260451271,"owners_count":23011238,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["auth","error-handling","flash-messages","go","session"],"created_at":"2024-11-06T10:28:25.716Z","updated_at":"2025-06-17T22:35:35.806Z","avatar_url":"https://github.com/usmanhalalit.png","language":"Go","funding_links":["https://github.com/sponsors/usmanhalalit"],"categories":[],"sub_categories":[],"readme":"# HutPlate\n[![Build Status](https://travis-ci.org/usmanhalalit/hutplate.svg?branch=master)](https://travis-ci.org/usmanhalalit/hutplate)\n\nHutPlate is a Library over your standard net/http library which makes your life easier.\n \n It handles Authentication, Session, HTTP Error Handling, Flash Messages and Redirection. All these \n things are a breeze to do with HutPlate which usually takes a lot of your time.\n \n Some examples:\n```go\n// Use any router, also using hutplate.Handler is optional\nrouter.Handle(\"/\", hutplate.Handler(CreatePost))\n\nfunc CreatePost(hp hutplate.Http) interface{} {\n\tif ! hp.Auth.Check() {\n\t    // Will redirect and set flash message in session\n\t    return hp.Response.Redirect(\"/login\").With(\"notice\", \"You have to login first!\")\n\t}\n\t\n\t// hutplate.Http extends http.Request, so everything is still there\n\tif err := createPost(hp.FormValue(\"content\")); err =! nil {\n\t    // Will show a generic error message to user with 500 status.\n\t    //  You can also customize the behaviour\n\t    return err\n\t}\n\t\n\t// The http.ResponseWriter is also there\n\thp.Response.Write([]byte(\"Success!\"))\n}\n```\n\n## Table of Contents\n\n  - [Setup](#setup)\n      - [Configuration](#configuration)\n      - [Creating a HutPlate Instance](#hutplate-instance)\n  - [Authentication](#authentication)\n      - [Login](#login)\n      - [Check if Logged In](#check-if-logged-in)\n      - [Get Logged in User ID](#get-logged-in-user-id)\n      - [Get the Logged in User](#get-logged-in-user)\n      - [Logout](#logout)\n  - [HutPlate HTTP Handler](#http-handler)\n  - [Redirect](#redirect)\n  - [Session](#session)\n      - [Flash Messages](#flash-messages)\n      - [Session Config](#session-config)\n          - [Storage Path](#session-storage-path)\n          - [Secret Key](#session-secret-key)\n      - [Configure Session Store](#session-store)\n      - [Clear Context](#clear-context)\n  - [List of Config](#list-of-config)\n  \n\u003ca name=\"setup\"\u003e\u003c/a\u003e\n## Setup\n\nJust go get:\n```\ngo get github.com/usmanhalalit/hutplate\n```\n\nand import the package.\n\n\n\u003ca name=\"configuration\"\u003e\u003c/a\u003e\n### Configuration\n\nThe only mandatory configuration for HutPlate is that you let it know where to find your user \n(any database/datastore is fine). \nIt will give you email or whatever you log your user in with, \nyou just return that user's id and hashed password. \nFor example, a GORM example would look like this:\n```go\nhutplate.Config.GetUserWithCred = func(credential interface{}) (interface{}, string) {\n    // credential will be email, username or whatever you log in with\n    user := models.User{}\n    db.Orm.Find(\u0026user, \"email='\" + credential.(string) + \"'\")\n    \n    return user.ID, user.Password\n}\n```\n\n**Another highly recommended configuration** is that you set a session secret key:\n```go\nhutplate.Config.SessionSecretKey = \"a_random_secret_key\"\n```\n\n\u003ca name=\"hutplate-instance\"\u003e\u003c/a\u003e\n### Creating a HutPlate Instance\n\nHutPlate comes with an HTTP handler, which gives you some extra power. \nBut it's optional to use. If you want to keep using the default handler \nthen you just call `hutplate.NewHttp` and give it your `Request` and `ResponseWriter`. \nHere's how:\n```go\nfunc MyHandler(w http.ResponseWriter, r *http.Request,) {\n\thp := hutplate.NewHttp(w, r)\n}\n```\n\nIf you use the HutPlate handler, then it is even easier:\n```go\nrouter.Handle(\"/\", hutplate.Handler(CreatePost))\n\nfunc CreatePost(hp hutplate.Http) interface{} {\n\t// hutplate.Http extends http.Request, so everything is still there\n\t// like hp.FormValue(\"content\")\n\n\t// The http.ResponseWriter is also there\n\thp.Response.Write([]byte(\"Success!\"))\n}\n```\n\n\u003ca name=\"authentication\"\u003e\u003c/a\u003e\n## Authentication\n\n\u003ca name=\"login\"\u003e\u003c/a\u003e\n### Login\n```go\nsuccess, _ := hp.Auth.Login(email, password)\n```\n\nIt will let you know if user logging in succeeded or not by returning true or false\n and if there is an error (in rare case) it'll be in the second return value.\n \nLogin requires you to store bcrypt hashed password when you register/save your user.\nHere is an example of how you hash you password using bcrypt\n\n```go\nhashedPassword, _ := bcrypt.GenerateFromPassword([]byte(password), 14)\n```\n\n\u003ca name=\"check-if-logged-in\"\u003e\u003c/a\u003e\n### Check if Logged In\n```go\nhp.Auth.Check()\n```\nWill return true or false.\n\n\u003ca name=\"get-logged-in-user-id\"\u003e\u003c/a\u003e\n### Get Logged in User ID\n```go\nhp.Auth.UserId()\n```\n\n\u003ca name=\"get-logged-in-user\"\u003e\u003c/a\u003e\n### Get the Logged in User\n\nIt gives you the whole user object, not just id. To use this feature,\n you need to set a configuration to let HutPlate know how to \n get the user from your database/datastore.\n```go\nuser, err = hp.Auth.User()\n```\n\n**Configuration**\n```go\nhutplate.Config.GetUserWithId = func(userId interface{}) interface {} {\n    // You will receive the userId just return the user with corresponding id  \n}\n```\n\nA GORM example:\n```go\nhutplate.Config.GetUserWithId = func(userId interface{}) interface {} {\n    user := models.User{}\n    if userId == nil {\n        return user\n    }\n    db.Orm.Find(\u0026user, userId)\n    return user\n}\n```\n\n\u003ca name=\"logout\"\u003e\u003c/a\u003e\n### Logout\n```go\nhp.Auth.Logout()\n```\n\n\u003ca name=\"http-handler\"\u003e\u003c/a\u003e\n## HutPlate HTTP Handler\n\nHutPlate also comes with an HTTP handler, using it is optional but you get so much\npower by using it. \n\n - It automatically creates an HutPlate instance for you.\n - You can return an error from the handler, so you do much less `if err != nil`.\n - You can configure how to handle all errors or group of errors. There is a sensible default.\n - You can return a `Redirect` or a plain string response (text, HTML, etc.).\n\n```go\n// Use any router\nrouter.Handle(\"/\", hutplate.Handler(CreatePost))\n\nfunc CreatePost(hp hutplate.Http) interface{} {\n\tif ! hp.Auth.Check() {\n\t    // Will redirect and set flash message in session\n\t    return hp.Response.Redirect(\"/login\").With(\"notice\", \"You have to login first!\")\n\t}\n\t\n\t// hutplate.Http extends http.Request, so everything is still there\n\tif err := createPost(hp.FormValue(\"content\")); err =! nil {\n\t    // Will show a generic error message to user with 500 status.\n\t    //  You can also customize the behaviour\n\t    return err\n\t}\n\t\n\t// The http.ResponseWriter is also there\n\thp.Response.Write([]byte(\"Success!\"))\n}\n```\n\nConfigure the error handler\n\n```go\nhutplate.Config.ErrorHandler = func(err error, hut hutplate.Http) {\n        // TODO log the error\n\t\thttp.Error(hut.Response, err.Error(), 404)\n}\n```\n\n\u003ca name=\"redirect\"\u003e\u003c/a\u003e\n## Redirect\n\n```go\n// Plain redirect\nhp.Response.Redirect(\"/admin\")\n\n// Redirect with a flash message\nhp.Response.Redirect(\"/login\").With(\"error\", \"Please do login!\")\n\n// Redirect with a different status code\nhp.Response.Redirect(\"/login\", 301)\n```\n \n\u003ca name=\"session\"\u003e\u003c/a\u003e\n## Session\n\nSet a session value and it will persist.\n```go\nerr := hp.Session.Set(\"test_key\", \"test_value\")\n```\n\nGet the session value\n```go\nerr := hp.Session.Get(\"test_key\")\n```\n\n\u003ca name=\"flash-messages\"\u003e\u003c/a\u003e\n### Flash Messages\n```go\nerr = hut.Session.SetFlash(\"test_key\", \"value\")\n```\n\nGetting a flash message is same as getting normal session value:\n```go\nerr := hp.Session.Get(\"test_key\")\n```\n\n[As mentioned above](#Redirect) you can also easily set a flash message while you redirect.  \n\n\u003ca name=\"session-config\"\u003e\u003c/a\u003e\n### Session Config\n\n\u003ca name=\"session-storage-path\"\u003e\u003c/a\u003e\n#### Storage Path\n\nBy default Session uses file system storage. You can optionally \nconfigure the storage directory by setting:\n```go\nhutplate.Config.SessionDirectory = \"path/to/dir\"\n```\nBy default it uses operating system's temp directory.\n\n\u003ca name=\"session-secret-key\"\u003e\u003c/a\u003e\n#### Secret Key\nIt is recommended that you set a session secret key by setting:\n```go\nhutplate.Config.SessionSecretKey = \"your_key\"\n````\n\n\u003ca name=\"session-store\"\u003e\u003c/a\u003e\n### Configure Session Store\n\nAs noted above, by default Session uses file system storage. But\nyou can use an entire different session storage system. HutPlate\nuses [Gorilla Sessions](https://github.com/gorilla/sessions).\nSo you can use any session storage supported by Gorilla Sessions, \ni.e. the store that implements Gorilla's `sessions.Store` interface.\n\n[Here is a list](https://github.com/gorilla/sessions#store-implementations)\n of session stores supported by Gorilla Sessions.\n\nTo use your own session store, use the SessionStore config,\nfor example:\n```\nimport \"github.com/gorilla/sessions\"\nhutplate.Config.SessionStore = sessions.NewCookieStore(...)\n```\n\n\u003ca name=\"clear-context\"\u003e\u003c/a\u003e\n### Clear Context\n\nImportant Note: If you aren't using gorilla/mux router, you need to wrap \nyour handlers with context.ClearHandler as or else you will leak memory! \nAn easy way to do this is to wrap the top-level mux when calling \nhttp.ListenAndServe:\n```go\n\thttp.ListenAndServe(\":8080\", context.ClearHandler(http.DefaultServeMux))\n```\nThe ClearHandler function is provided by the gorilla/context package.\n\nMore examples are available \n[on the Gorilla website](http://www.gorillatoolkit.org/pkg/sessions).\n\n\u003ca name=\"list-of-config\"\u003e\u003c/a\u003e\n## List of Config\n\n```go\nhutplate.Config.GetUserWithId = func(userId interface{}) interface {} {\n\n}\n\nhutplate.Config.GetUserWithCred = func(credential interface{}) (interface{}, string) {\n    \n}\n\nhutplate.Config.ErrorHandler = func(err error, hp hutplate.Http) {\n\n}\n\nhutplate.Config.SessionSecretKey = \"a_secret_key\"\nhutplate.Config.SessionDirectory = \"path/to/dir\"\n```\n\n\n___\n\u0026copy; 2020 [Muhammad Usman](https://usman.it/). Licensed under MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fusmanhalalit%2Fhutplate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fusmanhalalit%2Fhutplate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fusmanhalalit%2Fhutplate/lists"}