forked from jfberry/signpost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
55 lines (44 loc) · 1.13 KB
/
main.go
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
53
54
55
package main
import (
"fmt"
"io"
"net/http"
"os"
"text/template"
"time"
"github.com/gin-gonic/gin"
"github.com/pelletier/go-toml/v2"
)
var pokemonTemplate *template.Template
func main() {
tomlFile, err := os.Open("config.toml")
// if we os.Open returns an error then handle it
if err != nil {
panic(err)
}
// defer the closing of our tomlFile so that we can parse it later on
defer tomlFile.Close()
byteValue, _ := io.ReadAll(tomlFile)
err = toml.Unmarshal(byteValue, &config)
if err != nil {
panic(err)
}
config.TimestampFormat = "2006-01-02 15:04:05"
// templateStr := "https://maps.google.com/maps?q={{.lat}},{{.lon}}"
pokemonTemplate = template.New("pokemon")
for _, t := range config.Pokemon {
pokemonTemplate, err = pokemonTemplate.New(t.Name).Parse(t.Url)
if err != nil {
panic(err)
}
}
gin.SetMode(gin.ReleaseMode)
r := gin.New()
r.GET("/pokemon/:pokemon_id/:template", GetPokemon)
srv := &http.Server{
Addr: fmt.Sprintf(":%d", config.Port),
Handler: r,
}
fmt.Printf("%s [] Starting server on port %d\n", time.Now().Format(config.TimestampFormat), config.Port)
srv.ListenAndServe()
}