shridarpatil / frappe-go

API layer for frappe in go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Frappe-go

Frappe go , Write apis for frappe framework in go

Features

  • Token based authetication
  • RPC

Tech

Frappe-go uses a number of open source projects to work properly:

  • Gorilla RPC - A golang foundation for RPC over HTTP services.
  • Sqlx - General purpose extensions to golang's database/sql.
  • koanf - Simple, lightweight, extensible, configuration management library for Go.
  • Logger - Simple logger for Go programs. Allows custom formats for messages.

And of course Frappe-go itself is open source with a public repository on GitHub.

Installation

Frappe-go requires Go to run.

Install the dependencies and devDependencies and start the server.

package main

import "github.com/shridarpatil/frappe"
import "net/http"
import "fmt"
import "log"



type HelloService struct{}


type HelloReply struct {
	Message string
}


type HelloArgs struct {
	Who string
}


type User struct {
	Name 	string
	Owner	string
}


func (h *HelloService) Say(r *http.Request, args *HelloArgs, reply *HelloReply) error {
	err := frappe.Authorize(r)

	if err != nil{
		return err
	}
	reply.Message = "Hello, " + args.Who + "!"
	log.Printf("args: %v\nreply: %v, \n %v", r, r.Header.Get("Authorization"), frappe.Frappe)

	fmt.Println(frappe.Frappe.Ping())
	var user = User{}
	frappe.Frappe.Db.Get(&user, `SELECT name, owner FROM "tabUser" limit 1 `)
	fmt.Printf("%#v\n", user)


	return nil
}

func main() {

	var config = &frappe.SiteConfig{
		Driver: "postgres",
		DSN: "host=172.17.0.1 port=5432 user=crm password=Gorv71YDDqYaW0kl dbname=crm sslmode=disable",
		EncryptionKey: "sEgBb3h1KKIlGayaGUem65aowNkGQp_3WgWqYnONMa4=",
		SetLogLevel: "INFO",
	}
	var app = frappe.New(
		config,
	)
	app.RegisterService(&HelloService{app}, "")
	http.Handle("/rpc", app.GetServer())
	http.ListenAndServe("localhost:10000", nil)

}

Save above code in main.go and run

go run main.go
localhost:10000/rpc
curl --location --request POST 'http://localhost:10000/rpc' \
--header 'Authorization: token <api_key>:<api_secret>' \
--header 'Content-Type: application/json' \
--data-raw '{
    "method":"HelloService.Say",
    "params":[{"Who":"Shri"}],
    "id":"1"
}'
{
    "result": {
        "Message": "Hello, Shri!"
    },
    "error": null,
    "id": "1"
}

Stats

Python based api took around 100ms on an average image

Golang based api took around less then 10ms on an average image

License

MIT

Free Software, Hell Yeah!

About

API layer for frappe in go

License:MIT License


Languages

Language:Go 96.2%Language:Dockerfile 3.8%