Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
abaldeweg committed Oct 16, 2024
1 parent f60dcd0 commit 4a27ab6
Show file tree
Hide file tree
Showing 48 changed files with 2,227 additions and 1 deletion.
23 changes: 23 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "Go",
"image": "mcr.microsoft.com/devcontainers/go:1-1.23-bullseye",
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
"features": {
"ghcr.io/guiyomh/features/goreleaser:0": {},
"ghcr.io/abaldeweg/devcontainer_features/bash:latest": {}
},
"forwardPorts": [
5984
],
"customizations": {
"vscode": {
"extensions": [
"EditorConfig.EditorConfig",
"golang.go",
"DavidAnson.vscode-markdownlint",
"redhat.vscode-yaml",
"42Crunch.vscode-openapi"
]
}
}
}
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# editorconfig.org
root = true

[*]
indent_style = space
indent_size = 4
charset = utf-8
trim_trailing_whitespace = true
end_of_line = lf
insert_final_newline = true
62 changes: 62 additions & 0 deletions .github/workflows/unit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: Unit-Tests

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
test-admincli:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: "1.23"
- name: Run admincli tests
working-directory: ./admincli
run: |
go mod tidy
go test ./...
test-blog:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: "1.23"
- name: Run blog tests
working-directory: ./blog
run: |
go mod tidy
go test ./...
test-framework:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: "1.23"
- name: Run framework tests
working-directory: ./framework
run: |
go mod tidy
go test ./...
test-gateway:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: "1.23"
- name: Run gateway tests
working-directory: ./gateway
run: |
go mod tidy
go test ./...
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2022 - 2024 André Baldeweg <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
87 changes: 86 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,86 @@
# warehouse-server
# monorepo-go

monorepo-go is a database to manage your warehouse.

## Getting Started

## Blog

Mount auth volume to `/usr/src/app/data/auth/` and data volume to `/usr/src/app/data/content/`.

The routes needs the API-Key to contain the `articles` permission.

## Framework

```go
package main

import (
"log"

"github.com/abaldeweg/warehouse-server/blog/router"
"github.com/abaldeweg/warehouse-server/framework/config"
)

func main() {
config.LoadAppConfig()

r := router.Routes()
log.Fatal(r.Run(":8080"))
}
```

### Config

```go
import "github.com/abaldeweg/warehouse-server/framework/config"

config.LoadAppConfig(config.WithName("myconfig"), config.WithFormat("json"), config.WithPaths("./config", "."))

viper.SetDefault("CORS_ALLOW_ORIGIN", "http://127.0.0.1")
```

### ApiKey

```go
import "github.com/abaldeweg/warehouse-server/framework/apikey"

apikey.IsValidAPIKey("key")
apikey.HasPermission("key", "permission")
```

### Cors

```go
import "github.com/abaldeweg/warehouse-server/framework/cors"

viper.SetDefault("CORS_ALLOW_ORIGIN", "*")

corsConfig := cors.NewCors()
corsConfig.AllowOrigins = []string{viper.GetString("CORS_ALLOW_ORIGIN"), "http://127.0.0.1"}
corsConfig.SetCorsHeaders()

r := gin.Default()
r.Use(corsConfig.SetCorsHeaders())
```

## Settings

|Var |Description |Used by
|-----------------------|-------------------------------------------|--------------------------------
|CORS_ALLOW_ORIGIN |Allowed origins |gateway, blog
|API_CORE |API endpoint for the core |gateway
|project_dir |Path to docker compose |admincli
|database |Database name to dump |admincli

admincli will read a config file from following paths:

- /etc/admincli/admincli.yaml

Example

```yaml
// admincli.yaml
project_dir: .
database: db-1
```
37 changes: 37 additions & 0 deletions admincli/.goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
before:
hooks:
- go mod tidy
- go generate ./...

release:
disable: true

builds:
- env:
- CGO_ENABLED=0
goos:
- linux
id: admincli
binary: admincli

archives:
- format: tar.gz
name_template: >-
{{ .Binary }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
nfpms:
-
package_name: admincli
section: misc
description: "Offers tools for maintenance"
maintainer: "André Baldeweg"
formats:
- deb

checksum:
name_template: "checksums.txt"
2 changes: 2 additions & 0 deletions admincli/admincli.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
project_dir: .
database: db-1
58 changes: 58 additions & 0 deletions admincli/cmd/dump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cmd

import (
"fmt"
"os"
"os/exec"
"strings"
"time"

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

var dumpCmd = &cobra.Command{
Use: "dump",
Short: "Dump database",
Long: `Dumps the database to the file system`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Dumping database")

// command
c := exec.Command("docker", "exec", viper.GetString("database"), "sh", "-c", fmt.Sprintf("exec mysqldump %s -uroot -p\"%s\"", "$MYSQL_DATABASE", "$MYSQL_ROOT_PASSWORD"))

// create directory
dirPath := fmt.Sprintf("%s/dump/", viper.GetString("project_dir"))

if _, err := os.Stat(dirPath); os.IsNotExist(err) {
err := os.MkdirAll(dirPath, 0755)
if err != nil {
fmt.Println(err)
return
}
}

// create dump
weekday := strings.ToLower(time.Now().Weekday().String())

file, err := os.Create(fmt.Sprintf("%sdump_%s.sql", dirPath, weekday))
if err != nil {
fmt.Println(err)
return
}
defer file.Close()

// run command
c.Stdout = file
err = c.Run()
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Done")
}
},
}

func init() {
rootCmd.AddCommand(dumpCmd)
}
32 changes: 32 additions & 0 deletions admincli/cmd/pull.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cmd

import (
"fmt"
"log"

"github.com/abaldeweg/warehouse-server/admincli/command"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var pullCmd = &cobra.Command{
Use: "pull",
Short: "Refresh container images",
Long: `Fetch the latest images from the registry. After restarting the container the new image will be used.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Pulling new images...")

out, err := command.Command([]string{"/usr/bin/docker", "compose", "--project-directory", viper.GetString("project_dir"), "pull"})
if err != nil {
log.Fatal(err)
}

fmt.Println(string(out))
fmt.Println("Done")
},
}


func init() {
rootCmd.AddCommand(pullCmd)
}
28 changes: 28 additions & 0 deletions admincli/cmd/reboot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cmd

import (
"fmt"
"log"

"github.com/abaldeweg/warehouse-server/admincli/command"
"github.com/spf13/cobra"
)

var rebootCmd = &cobra.Command{
Use: "reboot",
Short: "Reboot the OS.",
Long: `Reboot the OS. The connection will be terminated.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Reboot system")
fmt.Println("The connection will be terminated")

_, err := command.Command([]string{"reboot"})
if err != nil {
log.Fatal(err)
}
},
}

func init() {
rootCmd.AddCommand(rebootCmd)
}
41 changes: 41 additions & 0 deletions admincli/cmd/refresh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cmd

import (
"fmt"
"log"

"github.com/abaldeweg/warehouse-server/admincli/command"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var refreshCmd = &cobra.Command{
Use: "refresh",
Short: "Reload container images",
Long: `Restart all containers.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Stopping all containers...")

stopContainers, err := command.Command([]string{"/usr/bin/docker", "compose", "--project-directory", viper.GetString("project_dir"), "down"})
if err != nil {
log.Fatal(err)
}

fmt.Println(string(stopContainers))
fmt.Println("Done")

fmt.Println("Starting all containers...")

startContainers, err := command.Command([]string{"/usr/bin/docker", "compose", "--project-directory", viper.GetString("project_dir"), "up", "-d"})
if err != nil {
log.Fatal(err)
}

fmt.Println(string(startContainers))
fmt.Println("Done")
},
}

func init() {
rootCmd.AddCommand(refreshCmd)
}
Loading

0 comments on commit 4a27ab6

Please sign in to comment.