Skip to content

Commit

Permalink
Init project
Browse files Browse the repository at this point in the history
  • Loading branch information
giangnh13579 committed Aug 6, 2024
0 parents commit fc73853
Show file tree
Hide file tree
Showing 12 changed files with 771 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
SERVER_PORT=8080
SERVER_AUTH_MODE=basic
SERVER_AUTH_BASIC_USERNAME=admin
SERVER_AUTH_BASIC_PASSWORD=public

ASYNQ_MON_ROOT_PATH="/"
ASYNQ_READ_ONLY=false
ASYNQ_REDIS_DSN=redis://127.0.0.1:6379/0
ASYNQ_REDIS_INSECURE_TLS=false
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.git
.vscode

build/

.env
25 changes: 25 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Build stage
FROM golang:1.21.1-alpine as build

ARG CGO_ENABLED=0

WORKDIR /app

COPY go.* ./
COPY cmd cmd
COPY internal internal

RUN go mod tidy

RUN go build -v -o ./asynqmon_auth cmd/httpserver/main.go

## Runtime stage
FROM alpine:3.16.2

WORKDIR /app

COPY --from=build /app/asynqmon_auth /app/asynqmon_auth

EXPOSE 8080

CMD ["/app/asynqmon_auth", "8080"]
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
DOCKER_TAG = simpsons310/asynqmon-auth
ENV_FILE = .env
DOCKER_APP_PORT = 8080

.PHONY: run
run:
go run cmd/httpserver/main.go

.PHONY: build
build:
go build -v -o ./build/asynqmon_auth cmd/httpserver/main.go

.PHONY: docker-build
docker-build:
docker build -t ${DOCKER_TAG} .

.PHONY: docker-run
docker-run:
docker run -p ${DOCKER_APP_PORT}:8080 --env-file=${ENV_FILE} ${DOCKER_TAG}
87 changes: 87 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Asynqmon Auth

A webserver with basic authentication middleware for package [Asynqmon](https://github.com/hibiken/asynqmon)

## Configuration

- Environment variables: checking `.env.example` or table below

```bash
# Server port
SERVER_PORT=8080
# Authentication mode for web-server. Accept modes:
# - `none` - disable authentication
# - `basic` - basic authentication
# - `http` - authentication via http (TODO)
SERVER_AUTH_MODE=basic
# Username & password for basic authentication
SERVER_AUTH_BASIC_USERNAME=
SERVER_AUTH_BASIC_PASSWORD=

# Asynqmon environment variables
ASYNQ_MON_ROOT_PATH=/
ASYNQ_READ_ONLY=false
ASYNQ_REDIS_DSN=redis://127.0.0.1:6379/0
ASYNQ_REDIS_INSECURE_TLS=false
```

## Running package

### Running in source code

``` bash
# Running with make
make run
```

### Running with binary

``` bash
# build binary
make build

# running with binary
./build/asynqmon_auth
```

- Binary accept first argument as server port. This argument will override environment variable `SERVER_PORT`

```bash
./build/asynqmon_auth 8080
```

### Running with docker

- Build/pull image

```bash
# Build docker image in local
make build

# Pull docker image from registry
docker pull simpsons310/asynqmon-auth
```

- Run image

```bash
# Docker command
docker run --rm \
--name asynqmon-auth \
--env-file .env
-p 8080:8080
simpsons310/asynqmon-auth

# With source code
make docker-run
```

## NOTE

This package is created just for practice purpose. I'm newbie to golang :smile:. I've referenced from [syahidfrd/asynqmon-handler](https://github.com/syahidfrd/asynqmon-handler)

## TODO

- Push image to docker registry
- Versioning
- Authentication via HTTP
39 changes: 39 additions & 0 deletions cmd/httpserver/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"context"
"os"
"os/signal"
asynqmonauth "simpsons310/asynqmon-auth/internal"
"strconv"
"syscall"
)

func main() {
ctx, done := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer done()

cfg, err := asynqmonauth.LoadEnv()
if err != nil {
panic(err)
}

// If a port number is provided as a command-line argument, use that.
// In docker environment, the port is fixed (8080), reading from env can be override when running the container,
// thus, providing a way to override the port number via command-line argument.
if len(os.Args) == 2 {
port, err := strconv.Atoi(os.Args[1])
if err == nil && port > 0 {
cfg.Server.Port = port
}
}

app, err := asynqmonauth.NewApplication(cfg, nil)
if err != nil {
panic(err)
}

if err := app.StartServer(ctx); err != nil {
panic(err)
}
}
23 changes: 23 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module simpsons310/asynqmon-auth

go 1.21.1

require (
github.com/hibiken/asynq v0.24.1
github.com/hibiken/asynqmon v0.7.2
)

require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/redis/go-redis/v9 v9.0.4 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/spf13/cast v1.5.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
)
Loading

0 comments on commit fc73853

Please sign in to comment.