<commit_before>package main

import (
	"github.com/asdine/storm"
	"github.com/labstack/echo"
	"github.com/labstack/echo/engine/fasthttp"
	"github.com/labstack/echo/middleware"
	"fmt"
	"log"
	"net/http"
	"os"
)

type (
	Mood struct {
		Value int `json:"mood"`
	}

	Subscribers struct {
		Users []Subscriber `json:"users"`
	}

	Subscription struct {
		Email string `json:"email"`
	}
)

func main() {
	database := createDatabase()
	defer database.Close()

	createCronJob(database, triggerMail(database))

	server := initServer(database)
	logFile := setLogLocation(server)
	defer logFile.Close()

	bind := getBind()
	log.Println("Starting server on bind " + bind + ".")
	server.Run(fasthttp.New(bind))
}

func getBind() string {
	if os.Getenv("OPENSHIFT_GO_PORT") != "" {
		return fmt.Sprintf("%s:%s", os.Getenv("OPENSHIFT_GO_IP"), os.Getenv("OPENSHIFT_GO_PORT"))
	} else {
		return ":8081"
	}
}

func initServer(database *storm.DB) (server *echo.Echo) {
	server = echo.New()

	server.Use(middleware.Logger())
	server.Get("/subscribers", getSubscribers(database))
	server.Get("/subscribers/:uuid", getSubscribersByUuid(database))
	server.Post("/subscribers", postSubscriber(database))
	server.Get("/moods", getDailyMoods(database))
	server.Get("/moods/:key", getDailyMoodsForm())
	server.Post("/moods/:key", postDailyMoods(database))

	return server
}

func setLogLocation(server *echo.Echo) (logFile *os.File) {
	if os.Getenv("OPENSHIFT_DATA_DIR") != "" {
		logFile, fileError := os.OpenFile(os.Getenv("OPENSHIFT_DATA_DIR") + "mut.log", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666)

		if fileError != nil {
			log.Fatal(fileError)
		}

		log.SetOutput(logFile)
		server.SetLogOutput(logFile)
	}

	return logFile
}

func getDailyMoods(database *storm.DB) echo.HandlerFunc {
	return (func(context echo.Context) error {
		dailyMoods, databaseError := getAllDailyMoods(database)

		if databaseError != nil {
			return databaseError
		} else {
			return context.JSON(http.StatusOK, dailyMoods)
		}

		return nil<commit_msg>refactoring<commit_after>package main

import (
	"github.com/asdine/storm"
	"github.com/labstack/echo"
	"github.com/labstack/echo/engine/fasthttp"
	"github.com/labstack/echo/middleware"
	"fmt"
	"log"
	"net/http"
	"os"
)

type (
	Mood struct {
		Value int `json:"mood"`
	}

	Subscribers struct {
		Users []Subscriber `json:"users"`
	}

	Subscription struct {
		Email string `json:"email"`
	}
)

func main() {
	database := createDatabase()
	defer database.Close()

	createCronJob(database, triggerMail(database))

	server := initServer(database)
	appLogFile, requestLogFile := setLogLocation(server)
	defer appLogFile.Close()
	defer requestLogFile.Close()

	bind := getBind()
	log.Println("Starting server on bind " + bind + ".")
	server.Run(fasthttp.New(bind))
}

func getBind() string {
	if os.Getenv("OPENSHIFT_GO_PORT") != "" {
		return fmt.Sprintf("%s:%s", os.Getenv("OPENSHIFT_GO_IP"), os.Getenv("OPENSHIFT_GO_PORT"))
	} else {
		return ":8081"
	}
}

func initServer(database *storm.DB) (server *echo.Echo) {
	server = echo.New()

	server.Use(middleware.Logger())
	server.Get("/subscribers", getSubscribers(database))
	server.Get("/subscribers/:uuid", getSubscribersByUuid(database))
	server.Post("/subscribers", postSubscriber(database))
	server.Get("/moods", getDailyMoods(database))
	server.Get("/moods/:key", getDailyMoodsForm())
	server.Post("/moods/:key", postDailyMoods(database))

	return server
}

func setLogLocation(server *echo.Echo) (appLogFile *os.File, requestLogFile *os.File) {
	if os.Getenv("OPENSHIFT_DATA_DIR") != "" {
		appLogFile, fileError := os.OpenFile(os.Getenv("OPENSHIFT_DATA_DIR") + "mut-app.log", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666)
		requestLogFile, fileError := os.OpenFile(os.Getenv("OPENSHIFT_DATA_DIR") + "mut-request.log", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666)

		if fileError != nil {
			log.Fatal(fileError)
		}

		log.SetOutput(appLogFile)
		server.SetLogOutput(requestLogFile)
	}

	return appLogFile, requestLogFile
}

func getDailyMoods(database *storm.DB) echo.HandlerFunc {
	return (func(context echo.Context) error {
		dailyMoods, databaseError := getAllDailyMoods(database)

		if databaseError != nil {
			return databaseError
		} else {
			return context.JSON(http.StatusOK, dailyMoods)
		}

		return nil
{'fraction_non_alphanumeric': 0.1530921820303384, 'fraction_numerical': 0.004667444574095682, 'mean_word_length': 5.0366197183098596, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3338867', 'n_tokens_mistral': 1664, 'n_tokens_neox': 1571, 'n_words': 269}