forked from naughtygopher/goapp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[patch] removed
platform
and introduced pkg
[patch] added info about SQL code generation [patch] added info about SQL migrations [patch] introduced Open telemetry usage (only in README, todo: update code)
- Loading branch information
1 parent
4a3e0b6
commit cf7161d
Showing
21 changed files
with
595 additions
and
446 deletions.
There are no files selected for viewing
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
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
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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package http | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"html/template" | ||
"net/http" | ||
"runtime/debug" | ||
|
||
"github.com/bnkamalesh/errors" | ||
"github.com/bnkamalesh/goapp/internal/api" | ||
"github.com/bnkamalesh/webgo/v6" | ||
) | ||
|
||
// Handlers struct has all the dependencies required for HTTP handlers | ||
type Handlers struct { | ||
api *api.API | ||
home *template.Template | ||
} | ||
|
||
func (h *Handlers) routes() []*webgo.Route { | ||
return []*webgo.Route{ | ||
{ | ||
Name: "helloworld", | ||
Pattern: "", | ||
Method: http.MethodGet, | ||
Handlers: []http.HandlerFunc{errWrapper(h.HelloWorld)}, | ||
TrailingSlash: true, | ||
}, | ||
{ | ||
Name: "health", | ||
Pattern: "/-/health", | ||
Method: http.MethodGet, | ||
Handlers: []http.HandlerFunc{errWrapper(h.Health)}, | ||
TrailingSlash: true, | ||
}, | ||
{ | ||
Name: "create-user", | ||
Pattern: "/users", | ||
Method: http.MethodPost, | ||
Handlers: []http.HandlerFunc{errWrapper(h.CreateUser)}, | ||
TrailingSlash: true, | ||
}, | ||
{ | ||
Name: "read-user-byemail", | ||
Pattern: "/users/:email", | ||
Method: http.MethodGet, | ||
Handlers: []http.HandlerFunc{errWrapper(h.ReadUserByEmail)}, | ||
TrailingSlash: true, | ||
}, | ||
} | ||
} | ||
|
||
// Health is the HTTP handler to return the status of the app including the version, and other details | ||
// This handler uses webgo to respond to the http request | ||
func (h *Handlers) Health(w http.ResponseWriter, r *http.Request) error { | ||
out, err := h.api.Health() | ||
if err != nil { | ||
return err | ||
} | ||
webgo.R200(w, out) | ||
return nil | ||
} | ||
|
||
// HelloWorld is a helloworld HTTP handler | ||
func (h *Handlers) HelloWorld(w http.ResponseWriter, r *http.Request) error { | ||
contentType := r.Header.Get("Content-Type") | ||
switch contentType { | ||
case "application/json": | ||
{ | ||
webgo.SendResponse(w, "hello world", http.StatusOK) | ||
} | ||
default: | ||
{ | ||
buff := bytes.NewBufferString("") | ||
err := h.home.Execute( | ||
buff, | ||
struct { | ||
Message string | ||
}{ | ||
Message: "welcome to the home page!", | ||
}, | ||
) | ||
if err != nil { | ||
return errors.InternalErr(err, "Inter server error") | ||
} | ||
|
||
w.Header().Set("Content-Type", "text/html; charset=UTF-8") | ||
_, err = w.Write(buff.Bytes()) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func errWrapper(h func(w http.ResponseWriter, r *http.Request) error) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
err := h(w, r) | ||
if err == nil { | ||
return | ||
} | ||
|
||
status, msg, _ := errors.HTTPStatusCodeMessage(err) | ||
webgo.SendError(w, msg, status) | ||
} | ||
} | ||
|
||
func panicRecoverer(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { | ||
defer func() { | ||
p := recover() | ||
if p == nil { | ||
return | ||
} | ||
fmt.Println(string(debug.Stack())) | ||
webgo.R500(w, errors.DefaultMessage) | ||
}() | ||
|
||
next(w, r) | ||
} | ||
|
||
func loadHomeTemplate(basePath string) (*template.Template, error) { | ||
t := template.New("index.html") | ||
home, err := t.ParseFiles( | ||
fmt.Sprintf("%s/index.html", basePath), | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return home, nil | ||
} |
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
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
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
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
Oops, something went wrong.