basic webserver

This commit is contained in:
Mike Bloy 2022-05-08 19:29:58 -05:00
parent d3f40ee087
commit f93dd223d6
5 changed files with 69 additions and 3 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
"git.bloy.org/mike/hasshelper/web"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.uber.org/zap" "go.uber.org/zap"
@ -16,18 +17,22 @@ var rootCmd = &cobra.Command{
var logger *zap.Logger var logger *zap.Logger
var err error var err error
if viper.GetString("deployment") == "prod" { if viper.GetString("deployment") == "prod" {
logger, err = zap.NewProduction() logger, err = zap.NewProduction(zap.WithCaller(false))
} else { } else {
logger, err = zap.NewDevelopment() logger, err = zap.NewDevelopment(zap.WithCaller(false))
} }
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
os.Exit(1) os.Exit(1)
} }
logger.Sugar().With("version", viper.GetString("version")).Info("HASSHelper startup") logger.Sugar().With("version", viper.GetString("version")).Info("HASSHelper startup")
exitchan := make(chan bool)
web.Run(logger, exitchan)
<-exitchan // run the main command until one of the goroutines is done
}, },
} }
// Execute will kick off cobra's processing of the root command
func Execute() { func Execute() {
viper.SetDefault("Version", "1.2.3") viper.SetDefault("Version", "1.2.3")
if err := rootCmd.Execute(); err != nil { if err := rootCmd.Execute(); err != nil {
@ -45,4 +50,17 @@ func init() {
func initConfig() { func initConfig() {
viper.AutomaticEnv() viper.AutomaticEnv()
expected_config := []string{
"deployment",
"image_dir",
"version",
"webserver_port",
}
for _, key := range expected_config {
if !viper.IsSet(key) {
fmt.Fprintf(os.Stderr, "Missing configuration value: %s\n", key)
os.Exit(1)
}
}
fmt.Println(viper.AllSettings())
} }

View File

@ -45,6 +45,9 @@ func main() {
fmt.Sprintf("package %s", pkgName), fmt.Sprintf("package %s", pkgName),
"// Code generated automatically. DO NOT EDIT", "// Code generated automatically. DO NOT EDIT",
"", "",
fmt.Sprintf(
"// %s is the generated version based on the current git revision",
varName),
fmt.Sprintf("const %s = \"%s\"", varName, versionStr), fmt.Sprintf("const %s = \"%s\"", varName, versionStr),
} }
file, err := os.Create(fileName) file, err := os.Create(fileName)

View File

@ -1,3 +1,4 @@
// Package main for the hasshelper application command
package main package main
import ( import (
@ -6,6 +7,8 @@ import (
) )
//go:generate go run ./gen_version.go -p cmd -f cmd/git_version.go //go:generate go run ./gen_version.go -p cmd -f cmd/git_version.go
// main
func main() { func main() {
cmd.Execute() cmd.Execute()
} }

View File

@ -1,10 +1,14 @@
{
daemon +sigterm: godoc -index -play
}
.git/**/* gen_version.go { .git/**/* gen_version.go {
prep: go mod tidy prep: go mod tidy
prep: go generate prep: go generate
} }
**/*.go { **/*.go .env {
prep: go mod tidy prep: go mod tidy
prep: go test @dirmods prep: go test @dirmods
prep: go build prep: go build
daemon +sigterm: ./hasshelper
} }

38
web/server.go Normal file
View File

@ -0,0 +1,38 @@
package web
import (
"fmt"
"net/http"
"os"
"github.com/spf13/viper"
"go.uber.org/zap"
)
type configObj struct {
logger *zap.Logger
port int
imageDir string
}
var config = configObj{}
// Run starts up the webserver part of hasshelper. It writes to exitch if
// the webserver ends unexpectedly. Config values will be read from viper.
func Run(rootLogger *zap.Logger, exitch chan bool) {
config.logger = rootLogger.Named("web")
config.port = viper.GetInt("webserver_port")
config.imageDir = viper.GetString("image_dir")
addr := fmt.Sprintf(":%d", config.port)
http.Handle("/", http.FileServer(http.FS(os.DirFS("."))))
var logger = config.logger.Sugar()
logger.Infow("Webserver startup", "port", config.port)
if err := http.ListenAndServe(addr, nil); err != nil {
logger.Errorf("Webserver fatal error: %s", err)
} else {
logger.Info("webserver shutting down")
}
exitch <- true
}