Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/pioz/microbo

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/pioz/microbo

  • v0.1.0
  • Source
  • Go
  • Socket score

Version published
Created
Source

Microbo

Microbo is a micro framework to create micro API webservers in go. A webserver that use Microbo require a very minimal configuration (just create a .env file) and support a DB connection, CORS, authentication with JWT and HTTP2 out of the box.

Usage

First of all you have to create .env file like this:

CERT_FILE="./cert/localhost+1.pem"
CERT_KEY="./cert/localhost+1-key.pem"
DB_CONNECTION=":memory:"
JWT_KEY="1234"
ROOT_PATH="/var/www/public"
ROOT_PATH_ENDPOINT="/public/"
SERVER_ADDR="127.0.0.1:3000"

You can generate the certificate using mkcert.

Then create your main.go file

package main

import (
  "encoding/json"
  "fmt"
  "net/http"
  "os"

  "github.com/pioz/microbo"
  "gorm.io/driver/sqlite"
  "gorm.io/gorm"
)

type Server struct {
  *microbo.Server
}

func NewServer(db *gorm.DB) *Server {
  server := &Server{Server: microbo.NewServer(db)}
  server.HandleFunc("GET", "/ping", pingHandler)
  server.HandleFuncWithAuth("GET", "/secure-ping", server.securePingHandler)
  return server
}

func pingHandler(w http.ResponseWriter, r *http.Request) {
  w.Header().Add("Content-Type", "application/json")
  encoder := json.NewEncoder(w)
  encoder.Encode("pong")
}

func (server *Server) securePingHandler(w http.ResponseWriter, r *http.Request) {
  w.Header().Add("Content-Type", "application/json")
  userId := r.Context().Value("user_id").(uint)
  user := microbo.DefaultUser{} // or your user model struct
  server.DB.Where("id = ?", userId).Find(&user)
  encoder := json.NewEncoder(w)
  encoder.Encode(fmt.Sprintf("pong %s", user.Email))
}

func main() {
  os.Setenv("SERVER_ADDR", "127.0.0.1:3001") // override env variable
  db, _ := gorm.Open(sqlite.Open(os.Getenv("DB_CONNECTION")), nil)
  NewServer(db).Run()
}

And yeah, your microservice is ready!

You can use your database with server.DB that is a *gorm.DB pointer. You can find all info about "The fantastic ORM library for Golang" here.

You can use the router with server.Router that is a *mux.Router pointer. You can find all info about "Gorilla web toolkit" here.

Also the following authentication endpoints are available:

  • POST /auth/register
  • POST /auth/login
  • POST /auth/refresh

Here the godoc to know all about microbo.

Questions or problems?

If you have any issues please add an issue on GitHub or fork the project and send a pull request.

Copyright (c) 2020 Enrico Pilotto (@pioz). See LICENSE for details.

FAQs

Package last updated on 20 Oct 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc