-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
133 lines (117 loc) · 3.78 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2023-2024 Kirill Scherba <[email protected]>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// The teoproxy fortune-gui example web serve package.
//
// How to build this web application server:
//
// # Install fyne executible (if not installed)
// go install fyne.io/fyne/v2/cmd/fyne@latest
//
// # Go to serve folder inside fortune-gui folder
// cd cmd/websocket/fortune-gui/serve
//
// # Build web package
// fyne package -os wasm --appVersion=0.0.3 --sourceDir ../
//
// (or you can use go generate command to build and run this web server)
//
// How to run this web application server:
//
// After build the web server you can start it with next command:
//
// # Run web server (in development mode)
// go run .
//
// # Run web server (in production mode)
// go run -tags=prod .
//
// How to build executible of this web application server:
//
// # Build executible
// go build -tags=prod .
//
//go:generate fyne package -os wasm --appVersion=0.0.3 --sourceDir ../
package main
import (
"flag"
"fmt"
"log"
"net/http"
"github.com/NYTimes/gziphandler"
"github.com/teonet-go/teoproxy/teonet/server"
"golang.org/x/crypto/acme/autocert"
)
const (
appShort = "fortune-gui-serve"
appName = "Fortune-gui web server"
appVersion = "0.0.3"
)
var domain string
// main is the entry point of the program.
//
// It parses application parameters, defines a handler function for HTTP requests,
// registers the handler function to handle all requests, creates a file server
// to serve static files, registers a websocket server, starts an HTTPS server
// if a domain is set, or starts an HTTP server if a domain is not set.
func main() {
// Parse application parameters
var monitor, laddr string
var gzip bool
//
flag.StringVar(&domain, "domain", "", "domain name to process HTTP/s server")
flag.StringVar(&laddr, "laddr", "localhost:8081", "local address of http, used if domain doesn't set")
flag.StringVar(&monitor, "monitor", "", "teonet monitor address")
flag.BoolVar(&gzip, "gzip", false, "gzip http files")
flag.Parse()
// Define Hello handler function for the HTTP requests
handler := func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World!")
}
// Register the Hello handler function to handle all requests
http.HandleFunc("/hello", handler)
// Create a file server to serve static files from the "wasm" directory
var frontendFS http.Handler
if gzip {
frontendFS = gziphandler.GzipHandler(http.FileServer(http.FS(getFrontendAssets())))
} else {
frontendFS = http.FileServer(http.FS(getFrontendAssets()))
}
http.Handle("/", frontendFS)
// Register teonet proxy server handler
serve, err := server.New(appShort, &server.TeonetMonitor{
Addr: monitor,
AppName: appName,
AppShort: appShort,
AppVersion: appVersion,
})
if err != nil {
fmt.Println("Create teonet proxy server error:", err)
return
}
http.HandleFunc("/ws", serve.HandleWebSocket)
// Start HTTPS server if domain is set
if len(domain) > 0 {
// Redirect HTTP requests to HTTPS
go func() {
err := http.ListenAndServe(":80", http.HandlerFunc(redirectTLS))
if err != nil {
log.Fatalf("ListenAndServe error: %v", err)
}
}()
// Start HTTPS server and create certificate for domain
log.Println("Start https serve with domain:", domain)
log.Fatal(http.Serve(autocert.NewListener(domain), nil))
return
}
// Start HTTP server
log.Println("Start http serve at:", laddr)
log.Fatalln(http.ListenAndServe(laddr, nil))
}
// redirectTLS redirects the HTTP request to HTTPS.
//
// It takes in the http.ResponseWriter and http.Request as parameters.
func redirectTLS(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://"+domain+":443"+r.RequestURI,
http.StatusMovedPermanently)
}