惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
SecWiki News
SecWiki News
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
小众软件
小众软件
C
CERT Recently Published Vulnerability Notes
Jina AI
Jina AI
N
Netflix TechBlog - Medium
GbyAI
GbyAI
IT之家
IT之家
Apple Machine Learning Research
Apple Machine Learning Research
AWS News Blog
AWS News Blog
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
C
Cybersecurity and Infrastructure Security Agency CISA
I
Intezer
T
Tor Project blog
P
Palo Alto Networks Blog
P
Privacy & Cybersecurity Law Blog
P
Privacy International News Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Proofpoint News Feed
T
Tailwind CSS Blog
C
Check Point Blog
Cloudbric
Cloudbric
Y
Y Combinator Blog
The Last Watchdog
The Last Watchdog
Forbes - Security
Forbes - Security
Last Week in AI
Last Week in AI
S
Security Affairs
博客园 - Franky
F
Fortinet All Blogs
量子位
M
MIT News - Artificial intelligence
C
Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
Stack Overflow Blog
Stack Overflow Blog
S
Secure Thoughts
V
Visual Studio Blog
AI
AI
美团技术团队
B
Blog RSS Feed
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 三生石上(FineUI控件)
阮一峰的网络日志
阮一峰的网络日志
Engineering at Meta
Engineering at Meta
人人都是产品经理
人人都是产品经理
Microsoft Security Blog
Microsoft Security Blog
T
Threatpost
Cyberwarzone
Cyberwarzone

Martin Heinz's Blog

A Guide to Python's Weak References Using weakref Module Recent Docker BuildKit Features You're Missing Out On Modern Git Commands and Features You Should Be Using Everything You Can Do with Python's textwrap Module Monitoring Indoor Air Quality with Prometheus, Grafana and a CO2 Sensor Everything You Can Do with Python's bisect Module You Don't Need a Dedicated Cache Service - PostgreSQL as a Cache A Collection of Docker Images To Solve All Your Debugging Needs Weird Python "Features" That Might Catch You By Surprise Lessons Learned From Writing 100 Articles Debugging Crashes and Deadlocks in Python using PyStack Goodbye etcd, Hello PostgreSQL: Running Kubernetes with an SQL Database Remote Interactive Debugging of Python Applications Running in Kubernetes The Right Way to Run Shell Commands From Python Real Multithreading is Coming to Python - Learn How You Can Use It Now Python's Missing Batteries: Essential Libraries You're Missing Out On Kubernetes-Native Synthetic Monitoring with Kuberhealthy Make Your CLI Demos a Breeze with Zero Stress and Zero Mistakes Reduce - The Power of a Single Python Function Why I Will Never Use Alpine Linux Ever Again Cgroups - Deep Dive into Resource Management in Kubernetes Dictionary Dispatch Pattern in Python Boost Your Python Application Performance using Continuous Profiling Lazy Evaluation Using Recursive Python Generators Python Magic Methods You Haven't Heard About Getting Started with Mastodon API in Python Backup-and-Restore of Containers with Kubernetes Checkpointing API Getting Started with Google APIs in Python Python CLI Tricks That Don't Require Any Code Whatsoever All The Ways To Introspect Python Objects at Runtime What is Python's "self" Argument, Anyway? Python List Comprehensions Are More Powerful Than You Might Think You Should Be Using Python's Walrus Operator - Here's Why Recipes and Tricks for Effective Structural Pattern Matching in Python It's Time to Say Goodbye to These Obsolete Python Libraries Advanced Features of Kubernetes' Horizontal Pod Autoscaler Data and System Visualization Tools That Will Boost Your Productivity Stop Messing with Kubernetes Finalizers Automate All the Boring Kubernetes Operations with Python End-to-End Monitoring with Grafana Cloud with Minimal Effort Bitly | bit.ly/3JLmSgA Bitly | bit.ly/3uETfbi Bitly | bit.ly/3MI4Iz0 Bitly | bit.ly/3M30D82 Bitly | bit.ly/3oMJ6qR Bitly | bit.ly/3IRD7IK Bitly | bit.ly/3A3B69t Bitly | bit.ly/31lKCYA Bitly | bit.ly/30uviIM Bitly | bit.ly/3E1X2mw Bitly | bit.ly/3Dv7JxP Bitly | bit.ly/3GG1BEz Bitly | bit.ly/3lLavs4 Bitly | bit.ly/39TqP3m Bitly | bit.ly/3A5Mpx8 Bitly | bit.ly/3kGwPl4 Bitly | bit.ly/3iHtulU Bitly | bit.ly/3xGjtKS Bitly | bit.ly/3h8DZg0 Bitly | bit.ly/2RQn1dG Bitly | bit.ly/3p2B5wW The Easiest Way to Debug Kubernetes Workloads Bitly | bit.ly/2PHVudx Cloud Native CI/CD with Tekton - Building Custom Tasks Bitly | bit.ly/3dg3QR9 Bitly | bit.ly/3qHtSkZ Deep Dive into Docker Internals - Union Filesystem Bitly | bit.ly/3qlRAUN Bitly | bit.ly/3pCUJ26 Bitly | bit.ly/3ifZxYr Bitly | bit.ly/34ZhIMt Bitly | bit.ly/3qSO7h0 Bitly | bit.ly/3muGLOk Bitly | bit.ly/35xN79v Bitly | bit.ly/3mLGshK Bitly | bit.ly/2IvkGQl Bitly | bit.ly/2Sk1KFK Bitly | bit.ly/3iCNIL6 Bitly | bit.ly/3beQPpy Saving Your Linux Machine from Certain Death New Features in Python 3.9 You Should Know About Deploy Any Python Project to Kubernetes Analyzing Docker Image Security Recursive SQL Queries with PostgreSQL Automating Every Aspect of Your Python Project Tour of Python Itertools Implementing 2D Physics in Javascript Ultimate Setup for Your Next Python Project Making Python Programs Blazingly Fast Security and Cryptography Mistakes You Are Probably Doing All The Time Going Serverless with OpenFaaS and Golang - Building Optimized Templates Going Serverless with OpenFaaS and Golang - The Ultimate Setup and Workflow Setting Up Swagger Docs for Golang API Building RESTful APIs in Golang Pytest Features, That You Need in Your (Testing) Life Setting up GitHub Package Registry with Docker and Golang Python Tips and Trick, You Haven't Already Seen, Part 2. Tricks for Postgres and Docker that will make your life easier Getting The Most Out of Reading Books - Reading The "Professional Way" Python Tips and Trick, You Haven't Already Seen
Ultimate Setup for Your Next Golang Project
Martin · 2019-09-28 · via Martin Heinz's Blog

For me, the biggest struggle when starting new project has always been trying to set the project up "perfectly". I always try to use the best directory structure so everything is easy to find and imports work nicely, setup all commands so that I'm always one click/command away from desired action, find the best linter, formatter, testing framework for the language/library that I'm using...

The list goes on and it never gets to the point that I'm actually satisfied with the setup... except for this ultimate and best (IMHO) setup for Golang!

Note: This setup works so well partly because it is based on existing projects which can be found here and here.

TL;DR: Here is my repository - https://github.com/MartinHeinz/go-project-blueprint

Directory Structure

First of all, let's go over directory structure of our project. There are few top-level files as well as 4 directories:

  • pkg - Let's start simple - pkg is a Go package that contains only global version string. This is substituted for actual version computed from commit hash during build.
  • config - Next, there is configuration directory which holds files with all necessary environment variables. Any file type can be used, but I recommend YAML files, as they are more readable.
  • build - This directory contains all shell scripts needed to build and test your application as well as generate reports for code analysis tools.
  • cmd - Actual source code! By convention, the source directory is named cmd, inside there is another one with name of the project - in this case blueprint. Next, inside this directory is a main.go that runs the whole application, along with it, there are all other source files divided into modules (more on that later).

Other than directories, there are also quite a few files and we will talk about those in following sections.

Go Modules for Perfect Dependency Management

Go projects use wide variety of dependency management strategies. However, since version 1.11 Go has official dependency management solution called Go modules. All our dependencies are listed in go.mod file, which can be found in root directory. This is how it might look:


module github.com/MartinHeinz/go-project-blueprint

go 1.12

require (
	github.com/spf13/viper v1.4.0
	github.com/stretchr/testify v1.4.0
)

You may ask "How is the file populated with dependencies?". Well, it's pretty simple, all you need is one command:


go mod vendor

This command resets the main module's vendor directory to include all packages needed to build and test all of the module's packages based on the state of the go.mod files and Go source code.

Actual Source Code and Configuration

Now we're finally getting to source code. As mentioned above, the source code is divided into modules. Each module is a directory in source root. In each module there are source files along with their tests files, e.g.:


./cmd/
└── blueprint
    ├── apis <- Module
    │   ├── apis_test.go
    │   ├── user.go
    │   └── user_test.go
    ├── daos <- Module
    │   ├── user.go
    │   └── user_test.go
    ├── services <- Module
    │   ├── user.go
    │   └── user_test.go
    ├── config <- Module
    │       └── config.go
    └── main.go

This structure help with readability and maintainability, as it divides code into reasonable chunks, which are easier to traverse. As for the configuration, in this setup I use Viper, which is Go configuration library, that can handle various formats, commandline flags, environment variables, etc.

So how do we use it (Viper) here? Let's have a look at config package:



var Config appConfig

type appConfig struct {
	// Example Variable, which is loaded in LoadConfig function
	ConfigVar string
}

// LoadConfig loads config from files
func LoadConfig(configPaths ...string) error {
	v := viper.New()
	v.SetConfigName("example")  // <- name of config file
	v.SetConfigType("yaml")
	v.SetEnvPrefix("blueprint")
	v.AutomaticEnv()
	for _, path := range configPaths {
		v.AddConfigPath(path)  // <- // path to look for the config file in
	}
	if err := v.ReadInConfig(); err != nil {
		return fmt.Errorf("failed to read the configuration file: %s", err)
	}
	return v.Unmarshal(&Config)
}

This package consist of a single file. It declares one struct that holds all config variables and has one function LoadConfig which, well, loads config. It takes path to config files, in our case, we would use path to config directory which resides in project root and contains our YAML files (mentioned above). And how do we use it? We run it as first thing in main.go:


if err := config.LoadConfig("./config"); err != nil {
    panic(fmt.Errorf("invalid application configuration: %s", err))
}

Simple and Fast Testing

The second most important thing after code itself? Quality tests. To be willing to write lots of good tests, you need a setup that will make it easy for you to do so. To achieve that we will use Makefile target called test, which collects and runs all tests in cmd subdirectories (all files with _test.go suffix). These tests are also cached, so they are ran only if there were some changes to relevant code. This is crucial as if the tests are too slow you will (most likely) eventually stop running and maintaining them. Besides unit testing, the make test also helps you maintain general code quality, as it also runs gofmt and go vet with every test run. go fmt forces you to format your code properly and go vet finds any suspicious code constructs using heuristics. Example output:


foo@bar:~$ make test
Running tests:
ok  	github.com/MartinHeinz/go-project-blueprint/cmd/blueprint	(cached)
?   	github.com/MartinHeinz/go-project-blueprint/cmd/blueprint/config	[no test files]
?   	github.com/MartinHeinz/go-project-blueprint/pkg	[no test files]

Checking gofmt: FAIL - the following files need to be gofmt'ed:
    cmd/blueprint/main.go

Checking go vet: FAIL
# github.com/MartinHeinz/go-project-blueprint/cmd/blueprint
cmd/blueprint/main.go:19:7: assignment copies lock value to l: sync.Mutex

Makefile:157: recipe for target 'test' failed
make: *** [test] Error 1

Always Running in Docker

People often say "It works on my machine (and not in cloud)...", to avoid this we have simple solution - always run in docker container. And when I say always I really mean it - build in container, run in container, test in container. Actually I didn't mention it in previous section, but the make test really is "just" docker run.

So, how does it work here? Let's start with Dockerfiles we have in root of the project - we have two of them one for testing (test.Dockerfile) and one running the application (in.Dockerfile):

  • test.Dockerfile - In ideal world, we would have just one Dockerfile for both running and testing the application. However, there might be need for little adjustments in environment for when the tests are ran. That's why we have this image here - to allow us to install additional tools and libraries, in case our tests require it. As an example, let's assume that we have database that we are connecting to. We don't want to spin up whole PostgreSQL server with every test run or be dependent some database running on host machine. So instead, we can use SQLite in-memory database for our test runs. But, guess what? SQLite binary requires CGO. So, what do we do? We just install gcc and g++, flip the CGO_ENABLED flag and we are good to go.
  • in.Dockerfile - If you look at this Dockerfile in the repository, it's just bunch of arguments and copying of config into image - so, what's going on in there? in.Dockerfile is used only from Makefile, where the arguments are populated, when we run make container. Now, it's time to look at the Makefile itself, which does all the docker stuff for us. 👇

Tying it all together with Makefile

For a longest time, Makefiles seemed scary to me as I've only seen them used with C code, but they are not scary and can be used for so many things, including this project! Let's now explore targets we have in our Makefile:

  • make build - First in the workflow - application build - it builds binary executable in bin directory:
  • 
    @echo "making $(OUTBIN)"
      @docker run                                              \ # <- It's just a `docker run`
        -i                                                     \ #    command in disguise  
        --rm                                                   \ # <- Remove container when done
        -u $$(id -u):$$(id -g)                                 \ # <- Use current user
        -v $$(pwd):/src                                        \ # <- Mount source folder
        -w /src                                                \ # <- Set workdir
        -v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin               \ # <- Mount directories where
        -v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin/$(OS)_$(ARCH) \ #    binary will be outputted
        -v $$(pwd)/.go/cache:/.cache                           \
        --env HTTP_PROXY=$(HTTP_PROXY)                         \
        --env HTTPS_PROXY=$(HTTPS_PROXY)                       \
        $(BUILD_IMAGE)                                         \
        /bin/sh -c "                                           \ # <- Run build script
            ARCH=$(ARCH)                                       \ #    (Checks for presence
            OS=$(OS)                                           \ #    of arguments, sets
            VERSION=$(VERSION)                                 \ #    env vars and runs
            ./build/build.sh                                   \ #    `go install`)
        "
      @if ! cmp -s .go/$(OUTBIN) $(OUTBIN); then \ # <- If binaries have changed
          mv .go/$(OUTBIN) $(OUTBIN);            \ #    move them from `.go` to `bin`
          date >$@;                              \
      fi
    
  • make test - Next one is testing - it once again uses docker run which is nearly identical, with only difference being the test.sh script (only relevant parts):
  • 
    TARGETS=$(for d in "$@"; do echo ./$d/...; done)
    
    go test -installsuffix "static" ${TARGETS} 2>&1
    
    ERRS=$(find "$@" -type f -name \*.go | xargs gofmt -l 2>&1 || true)
    
    ERRS=$(go vet ${TARGETS} 2>&1 || true)
    
    The lines above are the important part of the file. First of them collects testing targets using path given as parameter. Second line runs the tests and prints output to std out. Remaining two lines run go fmt and go vet respectively, both collecting errors (if there are any) and printing them.
  • make container - Now, the most important part - creating container that can be deployed:
  • 
    .container-$(DOTFILE_IMAGE): bin/$(OS)_$(ARCH)/$(BIN) in.Dockerfile
    	@sed                                 \
    	    -e 's|{ARG_BIN}|$(BIN)|g'        \
    	    -e 's|{ARG_ARCH}|$(ARCH)|g'      \
    	    -e 's|{ARG_OS}|$(OS)|g'          \
    	    -e 's|{ARG_FROM}|$(BASEIMAGE)|g' \
    	    in.Dockerfile < .dockerfile-$(OS)_$(ARCH)
    	@docker build -t $(IMAGE):$(TAG) -t $(IMAGE):latest -f .dockerfile-$(OS)_$(ARCH) .
    	@docker images -q $(IMAGE):$(TAG) > $@
    
    Code for this target is pretty simple, it first substitutes variables in in.Dockerfile and then runs docker build to produce image with both "dirty" and "latest" tags. Finally it prints container name to standard output.
  • make push - Next, when we have image, we need to store it somewhere, right? So, all that make push does is push image to Docker registry.
  • make ci - Another good use for Makefile is to leverage it inside our CI/CD pipeline (next section). This target is very similar to make test - it also runs all the tests, but on top of that, it also generates coverage reports which are then used as an input to code analysis tools.
  • make clean - Lastly, if we want to clean-up our project, we can run make clean, which removes all files generated by previous targets.

I will omit the remaining ones as they are not needed for normal workflow or are just part of other targets.

CI/CD for Ultimate Coding Experience

Last, but definitely not the least - CI/CD. With such a nice setup (if I say so myself), it would be a shame to omit some fancy pipeline, that can do tons of stuff for us, right? I won't go into too much detail about what is in the pipeline, because you can check it out yourself here (I also included comments for pretty much every line, so everything is explained), but I want to point out few things: This Travis build uses Matrix Build with 4 parallel jobs to speed up whole process:

  • The 4 parts (jobs) here are:
    • Build and Test where we verify that application works as expected
    • SonarCloud where we generate coverage reports and send them to SonarCloud server
    • CodeClimate - here, again as in previous one - we generate reports and send them, this time to CodeClimate using their test reporter
    • Push to Registry - finally, we push our container to GitHub Registry (stay tuned for blog post on that!)

Conclusion

I hope this post will help you in your future Go coding adventures. If you want to see more details, go ahead and checkout the repository here. Also, if you have any feedback or ideas for improvements, don't hesitate and submit issue, fork the repo or just give a star, so I know it makes sense to work on it little more. 🙂

In the next part we will look at how you can extend this blueprint to easily build RESTful APIs, test with in-memory database and setup swagger documentation (you can have a sneak peek in rest-api branch in the repository).