Script for New Golang Web Projects

Article Date

2020 Jun 10

Lately several people have been going over how they have their GO projects laid out, so I thought I’d join the group. I even went as far as created a powershell script to create the basic structure and initial files for a service/daemon based golang website.

It ends up with the root folder where you store your shared objects, logging, and config, and then subfolders for different aspects of your project. Maybe you have a Data folder that handles your connections to a backend, or whatever module-like organization you might have. The cmd folder has your actual executables, but its starts with only one: Service. I tend to always just run the ctr.ps1 (Compile-Test-Run) whenever I’m working on things. I could probably do an entire post on the compile.ps1 script as it has lots of stuff in there

Its a big script, here’s the full download: New-GoWebProject.ps1

GOLANG Dependencies

Other Dependencies

  • SASS compilation using dart-sass
  • Minimization (optional) using twdewolff/minify
  • GOHTML template compilation using StaticGen
  • On the javascript side of the house it is using vanilla javascript modules

You will need to modify the compile.ps1 file to point to the correct binaries for dart-sass, minify, and StaticGen.

Semi-Simplified Script

[CmdletBinding()]
param(
	[Parameter(Mandatory=$true)][String]$ProjectName,
    [int]$Port = 8042,
    [String]$BaseFolder = "E:\code"
)

$serviceName = $ProjectName + "Service"
$serviceNameShort = $ProjectName.Replace(' ','')


#Make project folders
$projFolder = Join-Path $BaseFolder $ProjectName
New-Item -Path $projFolder -Type Directory -Force | Out-Null
New-Item -Path (Join-Path $projFolder "cmd\Service") -Type Directory -Force | Out-Null
New-Item -Path (Join-Path $projFolder "scripts") -Type Directory -Force | Out-Null
$staticFolder = New-Item -Path (Join-Path $projFolder "Server\static") -Type Directory -Force
New-Item -Path (Join-Path $projFolder "Server\static\js") -Type Directory -Force | Out-Null
New-Item -Path (Join-Path $projFolder "Server\static\img") -Type Directory -Force | Out-Null
New-Item -Path (Join-Path $projFolder "Server\sass") -Type Directory -Force | Out-Null
New-Item -Path (Join-Path $projFolder "Server\templates") -Type Directory -Force | Out-Null

$file_config      = New-Item -Path (Join-Path $projFolder "config.go") -Type file -Force
$file_logging     = New-Item -Path (Join-Path $projFolder "logging.go") -Type file -Force
$file_gomod       = New-Item -Path (Join-Path $projFolder "go.mod") -Type file -Force
$file_config_json = New-Item -Path (Join-Path $projFolder "cmd\Service\config.json") -Type file -Force
$file_install     = New-Item -Path (Join-Path $projFolder "cmd\Service\install.go") -Type file -Force
$file_main        = New-Item -Path (Join-Path $projFolder "cmd\Service\main.go") -Type file -Force
$file_root        = New-Item -Path (Join-Path $projFolder "cmd\Service\root.go") -Type file -Force
$file_run         = New-Item -Path (Join-Path $projFolder "cmd\Service\run.go") -Type file -Force
$file_service     = New-Item -Path (Join-Path $projFolder "cmd\Service\service.go") -Type file -Force
$file_uninstall   = New-Item -Path (Join-Path $projFolder "cmd\Service\uninstall.go") -Type file -Force
$file_version     = New-Item -Path (Join-Path $projFolder "cmd\Service\version.go") -Type file -Force
$file_server      = New-Item -Path (Join-Path $projFolder "Server\webserver.go") -Type file -Force
$file_logger      = New-Item -Path (Join-Path $projFolder "Server\logger.go") -Type file -Force
$file_compile_ps1 = New-Item -Path (Join-Path $projFolder "scripts\compile.ps1") -Type file -Force
$file_test_ps1    = New-Item -Path (Join-Path $projFolder "scripts\test.ps1") -Type file -Force
$file_run_ps1     = New-Item -Path (Join-Path $projFolder "scripts\run.ps1") -Type file -Force
$file_ctr_ps1     = New-Item -Path (Join-Path $projFolder "scripts\ctr.ps1") -Type file -Force
$file_buildd      = New-Item -Path (Join-Path $projFolder "scripts\builddata.json") -Type file -Force
$file_main_sass   = New-Item -Path (Join-Path $projFolder "Server\sass\main.sass") -Type file -Force
$file_common_mjs  = New-Item -Path (Join-Path $projFolder "Server\static\js\common.mjs") -Type file -Force
$file_index_mjs   = New-Item -Path (Join-Path $projFolder "Server\static\js\index.mjs") -Type file -Force
$file__doc        = New-Item -Path (Join-Path $projFolder "Server\templates\_document.gohtml") -Type file -Force
$file_index       = New-Item -Path (Join-Path $projFolder "Server\templates\index.gohtml") -Type file -Force

#########################################################################
### REDACTED FILE CONTENTS FOR BREVITY, see download for full details ###
#########################################################################

## Now initialize the project
pushd $projFolder

# setup git
git init | Out-Null

# initial commit
git add * | Out-Null
git commit -m "initial commit" | Out-Null

#do the first build
. .\scripts\compile.ps1

popd