Skip to content

Commit

Permalink
dockerize app & make config path configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Hiltl committed Jul 25, 2024
1 parent 026e06e commit 7f7c143
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 31 deletions.
18 changes: 18 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# copy of .gitignore
tmp
node_modules

*.pem
.env
openchangelog.yml

go.work*
*.txt

test.db
.cache

# extras
.testdata
.air.toml
openchangelog.example.yml
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ node_modules

*.pem
.env
config.yaml
openchangelog.yml

go.work*
*.txt
Expand Down
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Non alpine because of CGO glibc dependency
FROM golang:1.22 AS builder

WORKDIR /build
ENV CGO_ENABLED=1
COPY go.mod .
COPY go.sum .
RUN go mod download

COPY . .
RUN go build -buildvcs=false -ldflags "-s -w -extldflags '-static'" -o ./openchangelog cmd/server.go

FROM alpine

WORKDIR /app
COPY --from=builder /build/openchangelog ./openchangelog

ENTRYPOINT ["/app/openchangelog"]
4 changes: 2 additions & 2 deletions MULTI_TENANCY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
Openchangelog supports multi tenancy by storing `workspaces`, `sources` & `changelogs` in SQLite.
**Note**: We do **not** store the changelog articles in SQLite, they still must be stored in a source like Github.

To configure sqlite and enable Multi Tenancy you need to specify the SQLite URL on the `config.yaml`.
To configure sqlite and enable Multi Tenancy you need to specify the SQLite URL on the `openchangelog.yml`.
```
# config.yaml
# openchangelog.yml
sqliteUrl:
```

Expand Down
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@ Openchangelog is an open source, self hostable Changelog Rendering Website.
Changelogs are written in Markdown and can be integrated from different sources, e.g `local` or `GitHub`.

## Configuration
You can configure your Changelog by adapting the `config.yaml` file.
You can configure your Changelog by adapting the `openchangelog.yml` file.
It is typically in `/etc/openchangelog.yml`.

### Look & Feel
**title**: The title is displayed above all Changelog articles.
```yaml
# config.yaml
# openchangelog.yml
page:
title:
```
**subtitle**: The subtitle is displayed below the title.
```yaml
# config.yaml
# openchangelog.yml
page:
subtitle:
```
**Logo**: Your logo is displayed in the header.
```yaml
# config.yaml
# openchangelog.yml
page:
logo:
src: # url to image
Expand All @@ -32,15 +33,15 @@ page:
### Local Data Source
You can specify a local file path to a directory containing your Changelog Markdown files.
```yaml
# config.yaml
# openchangelog.yml
local:
filesPath: .testdata
```
### Github Data Source
You can specify your repository and path to a directory inside the repo containing your Changelog Markdown files.
You can **authenticate** via a `Github App` or `Personal Access Token`.
```yaml
# config.yaml
# openchangelog.yml
github:
owner: # gh username
repo:
Expand All @@ -57,7 +58,7 @@ You can configure a cache to improve latency and avoid hitting rate limits from
Internally [httpcache](https://github.com/gregjones/httpcache) is used to cache the request to Github.
You can choose between a `memory`, `disk` and `s3`.
```yaml
# config.yaml
# openchangelog.yml
cache:
type: # disk, memory, s3
disk: # used when type is disk
Expand Down
47 changes: 29 additions & 18 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"errors"
"flag"
"fmt"
"log"
"net/http"
Expand All @@ -22,28 +23,22 @@ import (
)

func main() {
cfg, err := config.Load()
cfg, err := parseConfig()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read config: %v\n", err)
fmt.Fprintf(os.Stderr, "Failed to read config: %s\n", err)
os.Exit(1)
}

var st store.Store
if cfg.IsDBMode() {
log.Println("Starting Openchangelog backed by sqlite")
st, err = store.NewSQLiteStore(cfg.SqliteURL)
if err != nil {
panic(err)
}
} else {
log.Println("Starting Openchangelog in config mode")
st = store.NewConfigStore(cfg)
}

mux := http.NewServeMux()
cache, err := createCache(cfg)
if err != nil {
panic(err)
fmt.Fprintf(os.Stderr, "Failed to create cache: %s\n", err)
os.Exit(1)
}

st, err := createStore(cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create store: %s\n", err)
os.Exit(1)
}

rest.RegisterRestHandler(mux, rest.NewEnv(st))
Expand All @@ -54,6 +49,22 @@ func main() {
log.Fatal(http.ListenAndServe(addr, mux))
}

func parseConfig() (config.Config, error) {
configPath := flag.String("config", "", "config file path")
flag.Parse()
return config.Load(*configPath)
}

func createStore(cfg config.Config) (store.Store, error) {
if cfg.IsDBMode() {
log.Println("Starting Openchangelog backed by sqlite")
return store.NewSQLiteStore(cfg.SqliteURL)
} else {
log.Println("Starting Openchangelog in config mode")
return store.NewConfigStore(cfg), nil
}
}

func createCache(cfg config.Config) (httpcache.Cache, error) {
if cfg.Cache != nil {
switch cfg.Cache.Type {
Expand All @@ -62,7 +73,7 @@ func createCache(cfg config.Config) (httpcache.Cache, error) {
return httpcache.NewMemoryCache(), nil
case config.Disk:
if cfg.Cache.Disk == nil {
return nil, errors.New("missing 'cache.file' config")
return nil, errors.New("missing 'cache.file' config section")
}
log.Println("using disk cache")
return diskcache.NewWithDiskv(diskv.New(diskv.Options{
Expand All @@ -71,7 +82,7 @@ func createCache(cfg config.Config) (httpcache.Cache, error) {
})), nil
case config.S3:
if cfg.Cache.S3 == nil {
return nil, errors.New("missing 'cache.s3' config")
return nil, errors.New("missing 'cache.s3' config section")
}
log.Println("using s3 cache")
return s3cache.New(cfg.Cache.S3.Bucket), nil
Expand Down
9 changes: 9 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
openchangelog:
build: .
ports:
- "80:80"
volumes:
- type: bind
source: openchangelog.yml
target: /etc/openchangelog.yml
17 changes: 14 additions & 3 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,20 @@ func (c Config) IsDBMode() bool {
return c.SqliteURL != ""
}

func Load() (Config, error) {
viper.SetConfigFile("config.yaml")
viper.AddConfigPath(".")
// Loads the config file from configPath if specified.
//
// Otherwise searches the standard list of search paths. Returns an error if
// no configuration files could be found.
func Load(configFile string) (Config, error) {
viper.SetConfigType("yaml")
viper.SetConfigName("openchangelog") // extension needs to be in the name, otherwise openchangelog binary might be read

if configFile != "" {
viper.SetConfigFile(configFile)
} else {
viper.AddConfigPath("/etc/")
viper.AddConfigPath(".")
}
err := viper.ReadInConfig()
if err != nil {
return Config{}, err
Expand Down
File renamed without changes.

0 comments on commit 7f7c143

Please sign in to comment.