Skip to content

Commit

Permalink
+repackage json, health, buildinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
lnashier committed Mar 15, 2024
1 parent 2cd167d commit b21d672
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 50 deletions.
4 changes: 2 additions & 2 deletions examples/httpservice/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func main() {
// Register health controller for shutdown signal
srv.Component(ctr)
// Custom endpoints
srv.Register("/alive", http.MethodGet, http.HandlerFunc(ctr.LiveHandler))
srv.Register("/ready", http.MethodGet, http.HandlerFunc(ctr.ReadyHandler))
srv.Register("/alive", http.MethodGet, http.HandlerFunc(ctr.Live))
srv.Register("/ready", http.MethodGet, http.HandlerFunc(ctr.Ready))
return nil
},
),
Expand Down
3 changes: 1 addition & 2 deletions examples/mockhttp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/lnashier/goarc/x/config"
"github.com/lnashier/goarc/x/env"
xhttp "github.com/lnashier/goarc/x/http"
"github.com/lnashier/goarc/x/zson"
"net/http"
"time"
)
Expand All @@ -28,7 +27,7 @@ func main() {
&CustomHandler{
"application/json; charset=UTF-8",
func(req *http.Request) (any, error) {
return zson.Marshal(map[string]string{
return json.Marshal(map[string]string{
"msgId": "mock-msg-id",
}), nil
},
Expand Down
10 changes: 5 additions & 5 deletions examples/multiapps/apps/echo/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (

func App(srv *goarchttp.Service) error {
srv.Register("/echo", http.MethodPost, &xhttp.JSONHandler{Route: func(req *http.Request) (any, error) {
echoReq := &EchoRequest{}
echoReq := &Request{}
err := xhttp.RequestParse(req, echoReq)
if err != nil {
return xhttp.BadRequestf(err, err.Error())
}

return EchoResponse{
return Response{
Message: echoReq.Message,
}, nil

Expand All @@ -24,17 +24,17 @@ func App(srv *goarchttp.Service) error {
return nil
}

type EchoRequest struct {
type Request struct {
Message string `json:"message"`
}

func (er EchoRequest) Validate(*http.Request) error {
func (er Request) Validate(*http.Request) error {
if len(er.Message) < 1 {
return errors.New("missing message")
}
return nil
}

type EchoResponse struct {
type Response struct {
Message string `json:"message"`
}
3 changes: 2 additions & 1 deletion x/buildinfo/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
)

// App configures the build information related endpoint /buildinfo for the given service.
// To customize the endpoints, get a New Client and register the endpoints with custom names.
// To customize the endpoints, get a New Handler and register the endpoints with custom names.
// To customize the report info, pass in a custom Reporter.
// See Reporter, Report and Key
func App(s *shttp.Service) error {
s.Register("/buildinfo", http.MethodGet, New())
Expand Down
18 changes: 9 additions & 9 deletions x/buildinfo/buildinfo.go → x/buildinfo/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package buildinfo

import (
"github.com/lnashier/goarc/x/env"
"github.com/lnashier/goarc/x/zson"
xjson "github.com/lnashier/goarc/x/json"
"net/http"
"time"
)
Expand Down Expand Up @@ -33,7 +33,7 @@ type Report map[Key]any
// Reporter provides build-info report
type Reporter func() Report

// Client provides Key build information about current service:
// Handler provides Key build information about current service:
//
// KeyHost
// KeyVersion
Expand All @@ -43,32 +43,32 @@ type Reporter func() Report
//
// Custom Reporter can override all the Report Key since it runs after inbuilt reporter,
// Custom Reporter can override the default reporting keys, it runs after the built-in reporter.
type Client struct {
type Handler struct {
r Reporter
host string
startTime time.Time
}

func New(r ...Reporter) *Client {
func New(r ...Reporter) *Handler {
var r1 Reporter
if len(r) > 0 {
r1 = r[0]
}
return &Client{
return &Handler{
r: r1,
host: env.Hostname(),
startTime: time.Now(),
}
}

// ServeHTTP makes Client http.Handler
func (c *Client) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
// ServeHTTP makes Handler http.Handler
func (c *Handler) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
w.Header().Add("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
w.Write(zson.Marshal(c.report()))
w.Write(xjson.Marshal(c.report()))
}

func (c *Client) report() Report {
func (c *Handler) report() Report {
report := Report{
KeyHost: c.host,
KeyVersion: Version,
Expand Down
6 changes: 3 additions & 3 deletions x/health/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
// App configures health-related endpoints /alive and /ready for the given service.
// To customize the endpoints, get a New Controller and register the endpoints with custom names.
//
// See Controller.Stop, Controller.LiveHandler and Controller.ReadyHandler
// See Controller.Stop, Controller.Live and Controller.Ready
func App(srv *shttp.Service) error {
ctr := New()
srv.Component(ctr)
srv.Register("/alive", http.MethodGet, http.HandlerFunc(ctr.LiveHandler))
srv.Register("/ready", http.MethodGet, http.HandlerFunc(ctr.ReadyHandler))
srv.Register("/alive", http.MethodGet, http.HandlerFunc(ctr.Live))
srv.Register("/ready", http.MethodGet, http.HandlerFunc(ctr.Ready))
return nil
}
8 changes: 4 additions & 4 deletions x/health/handler.go → x/health/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ func (hc *Controller) Stop() {
close(hc.done)
}

// LiveHandler handles the HTTP request for the live endpoint,
// Live handles the HTTP request for the live endpoint,
// indicating that the service is up and running.
// This should not be used for load-balancer purposes.
func (hc *Controller) LiveHandler(w http.ResponseWriter, _ *http.Request) {
func (hc *Controller) Live(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(http.StatusText(http.StatusOK)))
}

// ReadyHandler handles the HTTP request for the ready endpoint,
// Ready handles the HTTP request for the ready endpoint,
// indicating whether the service is ready to accept new connections.
// This should be used for load-balancer purposes.
func (hc *Controller) ReadyHandler(w http.ResponseWriter, _ *http.Request) {
func (hc *Controller) Ready(w http.ResponseWriter, _ *http.Request) {
select {
case <-hc.done:
w.WriteHeader(http.StatusNotFound)
Expand Down
3 changes: 2 additions & 1 deletion x/http/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package http

import (
"fmt"
xjson "github.com/lnashier/goarc/x/json"
"net/http"
)

Expand All @@ -19,7 +20,7 @@ func HandleError(w http.ResponseWriter, err error) {

w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(httpErr.Status)
w.Write(marshal(httpErr))
w.Write(xjson.Marshal(httpErr))
}

// Error represents an HTTP error with an underlying error cause
Expand Down
3 changes: 2 additions & 1 deletion x/http/jsonhandler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package http

import (
xjson "github.com/lnashier/goarc/x/json"
"net/http"
)

Expand All @@ -17,5 +18,5 @@ func (h *JSONHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
w.Write(marshal(result))
w.Write(xjson.Marshal(result))
}
9 changes: 0 additions & 9 deletions x/http/zson.go

This file was deleted.

2 changes: 1 addition & 1 deletion x/zson/json.go → x/json/json.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package zson
package json

import "encoding/json"

Expand Down
25 changes: 13 additions & 12 deletions x/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"github.com/lnashier/goarc/x/buildinfo"
"github.com/lnashier/goarc/x/env"
"github.com/lnashier/goarc/x/zson"
xjson "github.com/lnashier/goarc/x/json"
"log"
"os"
"time"
Expand Down Expand Up @@ -45,10 +45,10 @@ const (
// Logger can be used simultaneously from multiple goroutines.
// Its zero value (DefaultLogger) is a usable object that uses os.Stdout to output log lines.
type Logger struct {
AppName string
Hostname string
Hash string
Logger *log.Logger
ServiceName string
Hostname string
Hash string
Logger *log.Logger
// Verifier verifies if provided logging Level is enabled
// PanicLevel is always enabled
Verifier func(Level) bool
Expand All @@ -57,12 +57,13 @@ type Logger struct {
}

// DefaultLogger is the default Logger and is used by Debug, Info, Error and Panic.
// It is ready to use except that
var DefaultLogger = func() *Logger {
return &Logger{
Hostname: env.Hostname(),
AppName: "unknown",
Hash: buildinfo.Hash[:len(buildinfo.Hash)/2],
Logger: log.New(os.Stdout, "", 0),
Hostname: env.Hostname(),
ServiceName: "unknown",
Hash: buildinfo.Hash[:len(buildinfo.Hash)/2],
Logger: log.New(os.Stdout, "", 0),
Verifier: func(a Level) bool {
return true
},
Expand Down Expand Up @@ -118,21 +119,21 @@ func (l *Logger) log(level Level, logType Type, msg any) {
if logType == NetType || level == PanicLevel || l.Verifier(level) {
e := &Entry{
Hostname: l.Hostname,
App: l.AppName,
Service: l.ServiceName,
Hash: l.Hash,
Timestamp: time.Now().Format(time.RFC3339),
LogType: string(logType),
Level: string(level),
Message: msg,
}
l.Logger.Print(string(zson.Marshal(e)))
l.Logger.Print(string(xjson.Marshal(e)))
l.Publisher(e)
}
}

type Entry struct {
Hostname string `json:"hostname"`
App string `json:"app"`
Service string `json:"service"`
Hash string `json:"hash"`
Timestamp string `json:"timestamp"`
LogType string `json:"logType"`
Expand Down

0 comments on commit b21d672

Please sign in to comment.