-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·61 lines (47 loc) · 1.27 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
56
57
58
59
60
61
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/template/html"
"io/ioutil"
"log"
"os"
)
func main() {
port := flag.String("port", "8080", "port to use")
devMode := flag.Bool("dev", false, "enable dev mode")
flag.Parse()
templateEngine := html.New("./views", ".html")
templateEngine.Reload(*devMode)
app := fiber.New(fiber.Config{
Views: templateEngine,
})
app.Use(logger.New())
setupRouting(app)
log.Println(log.Ldate|log.Ltime|log.Lshortfile, fmt.Sprintf("Started service v1 on port: : %s. Dev mode is: %t", *port, *devMode))
log.Fatal(app.Listen(":" + *port))
}
func setupRouting(app *fiber.App) {
// => http://localhost:3000/static/js/script.js
// => http://localhost:3000/static/css/style.css
app.Static("/static", "./public")
app.Get("/", func(c *fiber.Ctx) error {
return c.Render("index", getContent("index"))
})
}
func getContent(name string) *fiber.Map {
jsonFile, err := os.Open(fmt.Sprintf("content/%s.json", name))
if err != nil {
fmt.Println(err)
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var result fiber.Map
if err := json.Unmarshal(byteValue, &result); err != nil {
fmt.Println(err)
}
return &result
}