~starkingdoms/starkingdoms

ref: e36f9087f62db5f554de869952d35abb18ebe3b9 starkingdoms/starkingdoms-api/cmd/starkingdoms-api/main.go -rw-r--r-- 1.1 KiB
e36f9087 — core new API base 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main

import (
	"flag"
	"fmt"
	"github.com/sirupsen/logrus"
	starkingdoms_api "gitlab.com/starkingdoms.tk/starkingdoms.tk/starkingdoms-api"
	"net/http"
	"os"
)

func main() {
	configPath := flag.String("config", "config.yml", "File to load configuration file from")
	printUsage := flag.Bool("help", false, "Show command line usage")

	flag.Parse()

	if *printUsage {
		flag.Usage()
		os.Exit(0)
	}

	c, err := starkingdoms_api.LoadConfig(*configPath)

	if err != nil {
		fmt.Printf("failed to load config: %s", err)
		os.Exit(1)
	}

	if c.Logging.Format == "json" {
		logrus.SetFormatter(&logrus.JSONFormatter{})
	}
	logrus.SetLevel(c.Logging.Level)

	logrus.WithFields(logrus.Fields{
		"host": c.Server.Host,
		"port": c.Server.Port,
	}).Info("starting http listener")

	http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
		logrus.WithFields(logrus.Fields{
			"remote": r.RemoteAddr,
		}).Info("GET /ping")

		_, err := fmt.Fprintf(w, "pong")
		if err != nil {
			logrus.Error(err)
		}
	})

	logrus.Error(http.ListenAndServe(c.Server.Host+":"+c.Server.Port, nil))
}