Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test/dependency review #5

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# References:
# - https://docs.docker.com/engine/reference/builder/#dockerignore-file
# - https://docs.docker.com/build/building/context/#dockerignore-files

# git
.git/

# github
.github/

# visual studio code
.vscode/

# documentation
docs/

# docker
Dockerfile
docker-compose.yml
Expand All @@ -7,7 +23,10 @@ docker-compose.yml
.DS_Store
README.md

# jetbrains
/.idea

# project
/data
/build
/examples
20 changes: 15 additions & 5 deletions .github/workflows/dependency_review.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name: 'Dependency Review'

on: [ push ]
on: [ pull_request ]

permissions:
contents: read

jobs:
dependency-review:
dependency_review:
runs-on: ubuntu-latest

steps:
Expand All @@ -16,7 +16,17 @@ jobs:
# Configuration Options: https://github.com/actions/dependency-review-action/blob/main/README.md#configuration-options
# Examples: https://github.com/actions/dependency-review-action/blob/main/docs/examples.md
- name: Dependency Review
id: dependency_review
uses: actions/dependency-review-action@v3
with:
base_ref: main
head_ref: main

- name: Get Dependency Review
env:
GH_TOKEN: ${{ github.token }}
run: |
BASE_REF="${{ steps.dependency_review.with.base_ref || github.base_ref }}"
HEAD_REF="${{ steps.dependency_review.with.head_ref || github.head_ref }}"
REPO="${{ github.repository }}"
gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/repos/$REPO/dependency-graph/compare/$BASE_REF...$HEAD_REF"
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,16 @@ fabric.properties
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

# jetbrains
.idea
/.idea

# Go Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work

# snyk output and cache files
snyk_*.json
.dccache
11 changes: 6 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
FROM golang:1.19.3-alpine
RUN mkdir /app
ADD . /app
FROM golang:1.21
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
RUN go build -o main .
CMD ["/app/main"]
COPY . ./
RUN CGO_ENABLED=0 GOOS=linux go build -o /go-module-template
EXPOSE 8080
CMD ["/go-module-template"]
22 changes: 13 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
current_dir := $(notdir $(patsubst %/,%,$(dir $(mkfile_path))))
envfile := .env.json # TODO: env file is now yaml. Change to handle yaml files
makefile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
current_dir := $(notdir $(patsubst %/,%,$(dir $(makefile_path))))
envfile := .env

.PHONY: help build rebuild cert start stop run logs local config coverage webhook graph_deps
.PHONY: help build build-container rebuild-container start-container stop-container run logs test_coverage

# help target adapted from:
# https://gist.github.com/prwhite/8168133#gistcomment-2278355

TARGET_MAX_CHAR_NUM=20

## Start the services
start:
start-container:
@echo "Pulling images from Docker Hub"
docker-compose pull
@echo "Building Application Image"
Expand All @@ -19,19 +19,23 @@ start:
docker-compose up --detach
./build/post-start.sh

build:
build-container:
@echo "building application image"
docker-compose build app
@echo "build completed. exit=$?"

rebuild:
rebuild-container:
@echo "building application image (--no-cache)"
docker-compose build --no-cache app
@echo "build completed. exit=$?"

coverage:
test_coverage:
go test -coverprofile data/coverage "$1?" && go tool cover -html=data/coverage

## build the app
build:
go build -i -v -ldflags="-X main.version=$(git describe --always --long --dirty)"

## Start the services locally
run:
go run main.go
Expand Down Expand Up @@ -59,7 +63,7 @@ logs:

## Generate certificate private and public key
## Stop services
stop:
stop-container:
docker-compose down
docker volume rm $(current_dir)_{app,server}_node_modules 2>/dev/null || true

Expand Down
35 changes: 0 additions & 35 deletions cmd/main.go

This file was deleted.

72 changes: 72 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package cmd

import (
"errors"
"fmt"
"log"
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var cfgFile string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "go-module-template",
Short: "go-module-template",
Long: `Go module template is a demo application microservice.`,
// Uncomment the following line if your bare application has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) {
// cobra.CheckErr(cmd.Help())
// },
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}

func init() {
cobra.OnInitialize(initConfig)

// persistent: [--config, -t]
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "configuration file (default is $HOME/.test-service.yaml)")
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// use config file from the flag
viper.SetConfigFile(cfgFile)
} else {
// find the user home directory.
home, err := os.UserHomeDir()
cobra.CheckErr(err)

// search config in home directory with name ".test-service" (without extension).
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigName(".test-service")
}

// read in environment variables that match
viper.AutomaticEnv()

// if a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
if _, err := fmt.Fprintln(os.Stderr, "using config file:", viper.ConfigFileUsed()); err != nil {
cobra.CheckErr(err)
}
} else if errors.As(err, &viper.ConfigFileNotFoundError{}) {
// Config file not found; ignore error if desired
log.Println(err)
} else {
// cli error
cobra.CheckErr(err)
}
}
13 changes: 0 additions & 13 deletions cmd/server/server.go

This file was deleted.

36 changes: 36 additions & 0 deletions cmd/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cmd

import (
"context"

"github.com/cloether/go-module-template/pkg/server"
"github.com/spf13/cobra"
)

const defaultAddr = "127.0.0.1"
const defaultPort = 8080

type options struct {
addr string
port uint16
}

var arguments options

// startCmd represents the `receive` command
var startCmd = &cobra.Command{
Use: "start",
Short: "start server",
Long: `start server`,
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()
s := server.New(ctx, server.WithAddr(arguments.addr))
s.Run(ctx, arguments.addr)
},
}

func init() {
startCmd.Flags().StringVarP(&arguments.addr, "addr", "a", defaultAddr, "ip address to server on")
startCmd.Flags().Uint16VarP(&arguments.port, "port", "p", defaultPort, "port to listen on")
rootCmd.AddCommand(startCmd)
}
4 changes: 1 addition & 3 deletions doc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Package go_module_template contains a template go module
// intended for re-use within other modules.
//
// Created by @cloether
package go_module_template
package main
29 changes: 24 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
module github.com/cloether/go-module-template

go 1.19
go 1.21

require (
github.com/spf13/cobra latest
github.com/gorilla/mux v1.7.4
go.uber.org/zap v1.15.0
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.17.0
go.uber.org/zap v1.21.0
)

require (
go.uber.org/atomic v1.6.0 // indirect
go.uber.org/multierr v1.5.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading
Loading