Created
October 5, 2016 08:09
-
-
Save afrikaan-official/2644ff1e290b20363eafc7f5ad352f11 to your computer and use it in GitHub Desktop.
sample http health checker with http package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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