Skip to content

Commit

Permalink
v1.0.2: Cleanup and added GoDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
wneessen committed Aug 11, 2021
1 parent 32575b5 commit 8cc882f
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 13 deletions.
7 changes: 5 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -ldflag
## Create scratch image
FROM scratch
LABEL maintainer="[email protected]"
COPY ["build-files/passwd", "/etc/passwd"]
COPY ["build-files/group", "/etc/group"]
COPY ["docker-files/passwd", "/etc/passwd"]
COPY ["docker-files/group", "/etc/group"]
COPY ["", "/etc/group"]
COPY --from=builder ["/etc/ssl/certs/ca-certificates.crt", "/etc/ssl/cert.pem"]
COPY --chown=js-mailer ["LICENSE", "/js-mailer/LICENSE"]
COPY --chown=js-mailer ["README.md", "/js-mailer/README.md"]
COPY --chown=js-mailer ["etc/js-mailer", "/etc/js-mailer/"]
COPY --from=builder --chown=js-mailer ["/builddir/js-mailer", "/js-mailer/js-mailer"]
WORKDIR /js-mailer
Expand Down
3 changes: 3 additions & 0 deletions apirequest/apirequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import (
"net/http"
)

// ApiRequest reflects a new Api request object
type ApiRequest struct {
Cache *ttlcache.Cache
Config *config.Config
IsHttps bool
Scheme string
}

// RequestHandler handles an incoming HTTP request on the API routes and
// routes them accordingly to its request type
func (a *ApiRequest) RequestHandler(w http.ResponseWriter, r *http.Request) {
l := log.WithFields(log.Fields{
"action": "apiRequest.RequestHandler",
Expand Down
2 changes: 2 additions & 0 deletions apirequest/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/wneessen/js-mailer/form"
)

// GetForm gets a form.Form object either from the in-memory cache or if not cached
// yet, from the file system
func (a *ApiRequest) GetForm(i string) (form.Form, error) {
var formObj form.Form
cacheForm, err := a.Cache.Get(fmt.Sprintf("formObj_%s", i))
Expand Down
3 changes: 3 additions & 0 deletions apirequest/gettoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"
)

// TokenResponseJson reflects the JSON response struct for token request
type TokenResponseJson struct {
Token string `json:"token"`
CreateTime int64 `json:"create_time,omitempty"`
Expand All @@ -18,6 +19,8 @@ type TokenResponseJson struct {
Url string `json:"url"`
}

// GetToken handles the HTTP token requests and return a TokenResponseJson on success or
// an response.ErrorResponseJson on failure
func (a *ApiRequest) GetToken(w http.ResponseWriter, r *http.Request) {
l := log.WithFields(log.Fields{
"action": "apiRequest.GetToken",
Expand Down
1 change: 1 addition & 0 deletions apirequest/sendform.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"
)

// SendForm handles a send Api request
func (a *ApiRequest) SendForm(w http.ResponseWriter, r *http.Request) {
l := log.WithFields(log.Fields{
"action": "apiRequest.SendForm",
Expand Down
Empty file removed builds/.gitkeep
Empty file.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions form/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
)

// Form reflect the configuration struct for form configurations
type Form struct {
Id int `fig:"id" validate:"required"`
Secret string `fig:"secret" validate:"required"`
Expand All @@ -29,6 +30,8 @@ type Form struct {
}
}

// NewForm returns a new Form object to the caller. It fails with an error when
// the form is question wasn't found or does not fullfill the syntax requirements
func NewForm(c *config.Config, i string) (Form, error) {
l := log.WithFields(log.Fields{
"action": "form.NewForm",
Expand Down
4 changes: 3 additions & 1 deletion logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"strings"
)

func SetLogging() {
// InitLogging initializes the logging object and sets some sane defaults
func InitLogging() {
log.SetLevel(log.InfoLevel)
log.SetFormatter(&log.TextFormatter{
DisableLevelTruncation: true,
Expand All @@ -15,6 +16,7 @@ func SetLogging() {
})
}

// SetLogLevel sets the correspoding log level for the global logging object
func SetLogLevel(l string) {
if l == "" {
return
Expand Down
20 changes: 11 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,10 @@ import (
"time"
)

const VERSION = "0.1.1"

func main() {
// Initialize everything
logging.SetLogging()
confObj := config.NewConfig()
logging.SetLogLevel(confObj.Loglevel)
serve(&confObj)
}
// VERSION is the global version string contstant
const VERSION = "0.1.2"

// serve acts as main web service server router/handler for incoming HTTP requests
func serve(c *config.Config) {
l := log.WithFields(log.Fields{
"action": "main.serve",
Expand Down Expand Up @@ -59,3 +53,11 @@ func serve(c *config.Config) {
os.Exit(1)
}
}

// main is the main function
func main() {
logging.InitLogging()
confObj := config.NewConfig()
logging.SetLogLevel(confObj.Loglevel)
serve(&confObj)
}
7 changes: 7 additions & 0 deletions response/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@ import (
"net/http"
)

// ErrorResponseJson reflects the JSON response for a failed request
type ErrorResponseJson struct {
StatusCode int `json:"status_code"`
ErrorMessage string `json:"error_message"`
}

// MissingFieldsResponseJson reflects the JSON response for a failed request due to
// missing fields in the send request
type MissingFieldsResponseJson struct {
StatusCode int `json:"status_code"`
ErrorMessage string `json:"error_message"`
MissingFields []string `json:"missing_fields"`
}

// ErrorJson writes a ErrorResponseJson to the http.ResponseWriter in case
// an error response is needed as result to the HTTP request
func ErrorJson(w http.ResponseWriter, c int, m string) {
l := log.WithFields(log.Fields{
"action": "http_error.ErrorJson",
Expand All @@ -30,6 +35,8 @@ func ErrorJson(w http.ResponseWriter, c int, m string) {
}
}

// MissingFieldsJson writes a MissingFieldsResponseJson to the http.ResponseWriter when
// a send request is missing required fields
func MissingFieldsJson(w http.ResponseWriter, c int, m string, f []string) {
l := log.WithFields(log.Fields{
"action": "http_error.MissingFieldsJson",
Expand Down
4 changes: 3 additions & 1 deletion response/success.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"net/http"
)

// SuccessResponseJson reflects the HTTP response JSON for a successful request
type SuccessResponseJson struct {
StatusCode int `json:"status_code"`
SuccessMessage string `json:"success_message"`
FormId int
FormId int `json:"form_id"`
}

// SuccessJson writes a SuccessResponseJson struct to the http.ResponseWriter
func SuccessJson(w http.ResponseWriter, c int, f *form.Form) {
l := log.WithFields(log.Fields{
"action": "http_error.ErrorJson",
Expand Down

0 comments on commit 8cc882f

Please sign in to comment.