Skip to content

Instantly share code, notes, and snippets.

@afrikaan-official
Created October 5, 2016 08:09
Show Gist options
  • Save afrikaan-official/2644ff1e290b20363eafc7f5ad352f11 to your computer and use it in GitHub Desktop.
Save afrikaan-official/2644ff1e290b20363eafc7f5ad352f11 to your computer and use it in GitHub Desktop.
sample http health checker with http package
package main
import (
"net/http"
_ "github.com/lib/pq"
"github.com/astaxie/beego/orm"
"encoding/json"
)
type response struct{
rws int64
statusCode int
info map[string]int `json:"info"`
}
func init(){
orm.RegisterDriver("postgres", orm.DRPostgres)
orm.RegisterDataBase("default", "postgres", "postgresql://myUser:123@192.168.99.100:5432/test?sslmode=disable")
}
func main(){
http.HandleFunc("/",health())
http.ListenAndServe(":8080",nil)
}
func health() func(w http.ResponseWriter, r *http.Request){
return func(w http.ResponseWriter,r *http.Request){
var response=&response{}
o := orm.NewOrm()
o.Using("default")
res,_:=o.Raw("select * from users").Exec()
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
response.rws,_=res.RowsAffected()
response.statusCode=200
response.info=map[string]int{"statusCode":response.statusCode,"rows":int(response.rws)}
j,_:=json.Marshal(response.info)
w.Write(j)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment