serving-hugo.go
· 2.6 KiB · Go
Raw
package main
import (
"errors"
"fmt"
"log"
"github.com/foolin/goview"
"github.com/foolin/goview/supports/ginview"
"github.com/gin-gonic/gin"
flag "github.com/spf13/pflag"
)
// WebsiteConfig contains the config to run the server with
type WebsiteConfig struct {
port uint
disableCache bool
certFile string
keyFile string
tls bool
publicDir string
}
func parseWebsiteConfig() (WebsiteConfig, error) {
var config WebsiteConfig
flag.BoolVar(&config.disableCache, "disable-cache", false, "Disable cache (for testing)")
flag.UintVar(&config.port, "port", 8080, "port to listen and serve on")
flag.StringVar(&config.certFile, "cert-file", "", "location of certificate file (TLS)")
flag.StringVar(&config.keyFile, "key-file", "", "location of the key file (TLS)")
flag.StringVar(&config.publicDir, "public-dir", "./public", "location of the directory to be served")
var proxiesvar []string
flag.StringArrayVarP(&proxiesvar, "proxy", "", []string{}, "define a hostname reverse proxy")
flag.Parse()
if config.certFile != "" {
if config.keyFile == "" {
return config, errors.New("TLS: Certificate file provided without key file")
}
config.tls = true
} else if config.keyFile != "" {
return config, errors.New("TLS: Key file provided without certificate file")
}
if config.port == 0 || config.port > 65535 {
log.Fatal("Invalid port number: ", config.port)
return config, errors.New("Invalid port number:" + fmt.Sprintf("%d", config.port))
}
return config, nil
}
func setupRouter(config WebsiteConfig) (*gin.Engine, error) {
fmt.Println("Using port ", config.port)
fmt.Println("Disable cache: ", config.disableCache)
fmt.Println("Using TLS: ", config.tls)
fmt.Println("Public directory: ", config.publicDir)
router := gin.Default()
ginviewConfig := goview.DefaultConfig
if config.disableCache {
fmt.Println("Disabling cache...")
ginviewConfig.DisableCache = true
}
router.HTMLRender = ginview.New(ginviewConfig)
router.Static("/", config.publicDir)
router.SetTrustedProxies(nil)
return router, nil
}
func main() {
config, err := parseWebsiteConfig()
if err != nil {
log.Fatal("Error: ", err)
}
r, err := setupRouter(config)
if err != nil {
log.Fatal("Error: ", err)
}
// Listen and Server on specified port
if config.tls {
err = r.RunTLS(":"+fmt.Sprintf("%d", config.port), config.certFile, config.keyFile)
if err != nil {
log.Fatal("Error running the server with TLS: ", err)
}
} else {
err = r.Run(":" + fmt.Sprintf("%d", config.port))
if err != nil {
log.Fatal("Error running the server (http): ", err)
}
}
}
1 | package main |
2 | |
3 | import ( |
4 | "errors" |
5 | "fmt" |
6 | "log" |
7 | |
8 | "github.com/foolin/goview" |
9 | "github.com/foolin/goview/supports/ginview" |
10 | "github.com/gin-gonic/gin" |
11 | flag "github.com/spf13/pflag" |
12 | ) |
13 | |
14 | // WebsiteConfig contains the config to run the server with |
15 | type WebsiteConfig struct { |
16 | port uint |
17 | disableCache bool |
18 | certFile string |
19 | keyFile string |
20 | tls bool |
21 | publicDir string |
22 | } |
23 | |
24 | func parseWebsiteConfig() (WebsiteConfig, error) { |
25 | var config WebsiteConfig |
26 | |
27 | flag.BoolVar(&config.disableCache, "disable-cache", false, "Disable cache (for testing)") |
28 | flag.UintVar(&config.port, "port", 8080, "port to listen and serve on") |
29 | flag.StringVar(&config.certFile, "cert-file", "", "location of certificate file (TLS)") |
30 | flag.StringVar(&config.keyFile, "key-file", "", "location of the key file (TLS)") |
31 | flag.StringVar(&config.publicDir, "public-dir", "./public", "location of the directory to be served") |
32 | |
33 | var proxiesvar []string |
34 | flag.StringArrayVarP(&proxiesvar, "proxy", "", []string{}, "define a hostname reverse proxy") |
35 | |
36 | flag.Parse() |
37 | |
38 | if config.certFile != "" { |
39 | if config.keyFile == "" { |
40 | return config, errors.New("TLS: Certificate file provided without key file") |
41 | } |
42 | config.tls = true |
43 | } else if config.keyFile != "" { |
44 | return config, errors.New("TLS: Key file provided without certificate file") |
45 | } |
46 | |
47 | if config.port == 0 || config.port > 65535 { |
48 | log.Fatal("Invalid port number: ", config.port) |
49 | |
50 | return config, errors.New("Invalid port number:" + fmt.Sprintf("%d", config.port)) |
51 | } |
52 | |
53 | return config, nil |
54 | } |
55 | |
56 | func setupRouter(config WebsiteConfig) (*gin.Engine, error) { |
57 | |
58 | fmt.Println("Using port ", config.port) |
59 | fmt.Println("Disable cache: ", config.disableCache) |
60 | fmt.Println("Using TLS: ", config.tls) |
61 | fmt.Println("Public directory: ", config.publicDir) |
62 | router := gin.Default() |
63 | |
64 | ginviewConfig := goview.DefaultConfig |
65 | if config.disableCache { |
66 | fmt.Println("Disabling cache...") |
67 | ginviewConfig.DisableCache = true |
68 | } |
69 | router.HTMLRender = ginview.New(ginviewConfig) |
70 | router.Static("/", config.publicDir) |
71 | router.SetTrustedProxies(nil) |
72 | return router, nil |
73 | } |
74 | |
75 | func main() { |
76 | config, err := parseWebsiteConfig() |
77 | |
78 | if err != nil { |
79 | log.Fatal("Error: ", err) |
80 | } |
81 | |
82 | r, err := setupRouter(config) |
83 | |
84 | if err != nil { |
85 | log.Fatal("Error: ", err) |
86 | } |
87 | |
88 | // Listen and Server on specified port |
89 | if config.tls { |
90 | err = r.RunTLS(":"+fmt.Sprintf("%d", config.port), config.certFile, config.keyFile) |
91 | if err != nil { |
92 | log.Fatal("Error running the server with TLS: ", err) |
93 | } |
94 | } else { |
95 | err = r.Run(":" + fmt.Sprintf("%d", config.port)) |
96 | if err != nil { |
97 | log.Fatal("Error running the server (http): ", err) |
98 | } |
99 | } |
100 | } |
101 |