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 / start-devcontainer.sh
Created September 23, 2019 16:29
Go RESTful Series
#!/bin/bash
# Exit immediately on failure.
set -e
# Derive local workspace's absolute path from script's path.
LOCAL_WORKDIR=$(cd $(dirname $(dirname "${BASH_SOURCE[0]}")) >/dev/null 2>&1 && pwd)
main() {
# Variables.
@phuctm97
phuctm97 / .devcontainer.json
Created September 23, 2019 16:31
Go RESTful Series
{
"dockerFile": "Dockerfile",
// Configure port mapping between localhost and development container,
// should be updated respectively to exposed service ports of referencing container.
"appPort": [
"8000:8000"
],
// Configure to mount local workspace directory to an appropriate workspace directory
// within development container.
"workspaceMount": "src=${localWorkspaceFolder},dst=/root/project,type=bind",
@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
@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 / 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 / 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 / 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 / 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
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 / 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