Development utils for GO
Tiny but useful code snippets to link into your GO project.
Import / Fork / Cut-n-Paste .... up to you.
package main
import (
"fmt"
"github.com/steveoc64/godev/config"
"github.com/steveoc64/godev/mail"
)
func main() {
...
Use a config.json file to store your apps operational params, and load them at runtime.
func main() {
cfg := config.LoadConfig()
...
Loads the following file
{
"Debug": false,
"DataSourceName": "user=postgres password=xxxxxx dbname=dbdbdb sslmode=disable",
"WebPort": 8000,
"MailServer": "mail.mysite.com",
"MailUser": "mailaccount",
"MailPasswd": "xxxxxx",
"MailPort": 465
}
Extend gopkg.in/gomail.v2 by adding an internal mailserver that uses channels. Runtime config requires the config package.
package main
import (
"github.com/steveoc64/godev/config"
"github.com/steveoc64/godev/mail"
)
func main() {
cfg := config.LoadConfig()
MailChannel := mail.InitMailer()
m := mail.NewMail()
m.SetHeader("To", "jack@sprat.com")
m.SetHeader("Subject", "Loan Application")
m.SetBody("text/html", "Awww Snap ! Your Loan Application has been denied :(")
MailChannel <- m
So inside your app, you create emails using the regular gomail functions, and then just add them to the MailChannel queue. A goroutine in the background works through the MailChannel queue, and does all the messy delivery stuff for you.
We have CPU cores ... lets use em !!!
package main
import (
"fmt"
"github.com/steveoc64/godev/smt"
)
func main() {
cpus := smt.Init()
fmt.Printf("Yo Ho Ho, here we Go on %d CPU cores\n", cpus)
By default, this code uses numCores := runtime.NumCPU(), but your policy may vary.
Clone this microlib if you want to maitain consitent CPU hogging policies across all your GO services. (and we are going to produce a tonne of them this year - true ?)
Code snippet to add Cors support to your Echo webserver
package main
import (
"github.com/labstack/echo"
"github.com/steveoc64/godev/echocors"
)
func main() {
e := echo.New()
if cfg.Debug {
e.SetDebug(true)
}
echocors.Init(e, cfg.Debug)
Basic setup for using DAT / Postgres in your app, using the config lib for runtime paramaters.
Does a few basic tuning things, and then tests the connection by doing a "select now()"
package main
import (
"github.com/steveoc64/godev/config"
"github.com/steveoc64/godev/db"
)
func main() {
cfg := config.LoadConfig()
DB := db.Init(cfg.DataSourceName)