Skip to content

Instantly share code, notes, and snippets.

View phuctm97's full-sized avatar
πŸ‘¨β€πŸ’»
Coding

Minh-Phuc Tran phuctm97

πŸ‘¨β€πŸ’»
Coding
View GitHub Profile
@phuctm97
phuctm97 / interfaces.go
Created September 30, 2019 16:31
Go RESTful Series
package users
// UserRepository abstracts accesses to users-related data.
type UserRepository interface {
ExistsByUsername(string) (bool, error)
ExistsByEmail(string) (bool, error)
}
@phuctm97
phuctm97 / entities.go
Created September 27, 2019 16:27
Go RESTful Series
// Errors.
var (
ErrUsernameIsTooShort = errors.New("username must be at least 1-character length")
ErrUsernameIsTooLong = errors.New("username must be at most 32-character length")
ErrUsernameContainsInvalidChar = errors.New("username must contain only digits, underscores, dashes, dots and alphabetical letters")
ErrUsernameBeginsWithInvalidChar = errors.New("username must begin with either underscore or an alphabetical letter")
ErrEmailIsInvalid = errors.New("email is invalid")
ErrEmailIsTooLong = errors.New("email must be at most 128-character length")
ErrFullNameIsTooShort = errors.New("full name must be at least 1-character length")
ErrFullNameIsTooLong = errors.New("full name must be at most 128-character length")
@phuctm97
phuctm97 / entities_test.go
Created September 27, 2019 16:22
Go RESTful Series
package users
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestValidateUser(t *testing.T) {
tests := []struct {
user User
@phuctm97
phuctm97 / entities.go
Last active September 24, 2019 16:28
Go RESTful Series
package users
import (
"errors"
"regexp"
"github.com/the-evengers/go-restful/common"
)
// ... other stuff ...
@phuctm97
phuctm97 / errors.go
Created September 24, 2019 16:23
Go RESTful Series
package common
import "fmt"
// Errors is a collection of errors.
type Errors []error
func (errs Errors) Error() string {
msgs := make([]string, len(errs))
for i, err := range errs {
@phuctm97
phuctm97 / entities.go
Created September 24, 2019 16:09
Go RESTful Series
package users
import (
"errors"
"regexp"
)
// Regular expressions.
var (
regexContainsOnlyDigitsUnderscoreDashesDotsAlpha = regexp.MustCompile(`^[0-9a-zA-Z_\.\-]*$`)
@phuctm97
phuctm97 / entities.go
Created September 24, 2019 16:03
Go RESTful Series
package users
// User holds a user's data.
type User struct {
Username string
Email string
FullName string
Bio string
}
@phuctm97
phuctm97 / project-directory-structure.md
Created September 24, 2019 16:02
Go RESTful Series
β”œβ”€β”€ common    # common package holding common, utility components.
└── users     # users package holding users-related components.
@phuctm97
phuctm97 / Dockerfile
Created September 23, 2019 16:37
Go RESTful Series
FROM golang:1
# Configure to avoid build warnings and errors as described in official VSCode Remote-Containers extension documentation.
# See https://code.visualstudio.com/docs/remote/containers-advanced#_reducing-dockerfile-build-warnings.
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update \
&& apt-get -y install --no-install-recommends apt-utils 2>&1
# Verify git, process tools, lsb-release (common in install instructions for CLIs) installed.
RUN apt-get -y install git iproute2 procps lsb-release
@phuctm97
phuctm97 / go.mod
Created September 23, 2019 16:35
Go RESTful Series
module github.com/the-evengers/go-restful
go 1.13
require github.com/gorilla/mux v1.7.3