package middlewares

import (
	"net/http"
	"time"

	"github.com/grokking-engineering/grokking-blog/utils/logs"
)

var l = logs.New("recovery")

type Logger struct {
}

func NewLogger() func(http.Handler) http.Handler {
	logger := Logger{}
	return logger.factory
}

type responseWriter struct {
	http.ResponseWriter

	httpCode int
}

func (w *responseWriter) WriteHeader(status int) {
	w.httpCode = status
	w.ResponseWriter.WriteHeader(status)
}

func (this Logger) factory(next http.Handler) http.Handler {
Beispiel #2
0
package main

import (
	"flag"

	"github.com/grokking-engineering/grokking-blog/gserver"
	"github.com/grokking-engineering/grokking-blog/utils/load-config"
	"github.com/grokking-engineering/grokking-blog/utils/logs"
)

var (
	flConfigFile = flag.String("config-file", "config-default.json", "Load config from file")

	l = logs.New("grokking-server")
)

func main() {
	flag.Parse()

	var cfg gserver.Config
	err := loadConfig.FromFileAndEnv(&cfg, *flConfigFile)
	if err != nil {
		l.WithError(err).Fatal("Loading config")
	}

	l.Fatal(gserver.Start(cfg))
}
package handlers

import (
	"bytes"
	"fmt"
	"html/template"
	"net/http"
	"path/filepath"
	"strings"

	"github.com/grokking-engineering/grokking-blog/store"
	"github.com/grokking-engineering/grokking-blog/utils/logs"
)

var l = logs.New("handlers")

type MainHandler struct {
	Store *store.Instance
	IsDev bool
}

func (this *MainHandler) Init() {
	if this.Store == nil {
		panic("Required object is nil")
	}
}

func (this *MainHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	if this.IsDev {
		err := this.Store.ClearCacheAndReload()
		if err != nil {
package store

import (
	"html/template"
	"time"

	"github.com/grokking-engineering/grokking-blog/utils/logs"
)

var kTimeFormat = "02-01-2006"

var l = logs.New("store")

func ParseDate(s string) (time.Time, error) {
	return time.Parse(kTimeFormat, s)
}

type Instance struct {
	ContentDir string

	data *Data
}

func (this *Instance) Init() {
	if this.ContentDir == "" {
		panic("Empty ContentDir")
	}
	err := this.ClearCacheAndReload()
	if err != nil {
		l.Fatal(err)
	}
package loadConfig

import (
	"encoding/json"
	"io/ioutil"
	"os"
	"path/filepath"
	"reflect"
	"strings"

	"github.com/grokking-engineering/grokking-blog/utils/logs"
)

var (
	l = logs.New("load-config")
)

func FromFileAndEnv(cfg interface{}, configPath string) error {
	err := FromFile(cfg, configPath)
	if err != nil {
		return err
	}

	envMap := make(map[string]interface{})
	fromEnv(envMap, cfg, "json")
	l.WithFields(envMap).Info("Environment config")
	return nil
}

func FromFile(cfg interface{}, configPath string) error {
	absPath, err := filepath.Abs(configPath)
package gserver

import (
	"fmt"
	"net/http"
	"os"
	"time"

	"github.com/grokking-engineering/grokking-blog/handlers"
	"github.com/grokking-engineering/grokking-blog/middlewares"
	"github.com/grokking-engineering/grokking-blog/store"
	"github.com/grokking-engineering/grokking-blog/utils/logs"
)

var l = logs.New("gserver")

type Config struct {
	Server struct {
		ListenAddr    string `json:"LISTEN_ADDR"`
		ContentDir    string `json:"CONTENT_DIR"`
		StaticDir     string `json:"STATIC_DIR"`
		IsDevelopment string `json:"DEVELOPMENT"`
	} `json:"server"`
}

func Start(cfg Config) error {
	s := setup(cfg)
	listenAddr := cfg.Server.ListenAddr

	l.WithFields(logs.M{
		"addr": listenAddr,