Fadila gist felülvizsgálása . Revízióhoz ugrás
1 file changed, 100 insertions
serving-hugo.go(fájl létrehozva)
@@ -0,0 +1,100 @@ | |||
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 | + | } |
Újabb
Régebbi