wireguard-ui/main.go

72 lines
2.3 KiB
Go
Raw Normal View History

2020-04-18 11:17:49 +02:00
package main
import (
2020-04-23 13:01:40 +02:00
"fmt"
rice "github.com/GeertJohan/go.rice"
"github.com/labstack/echo/v4"
2020-04-18 11:17:49 +02:00
"github.com/ngoduykhanh/wireguard-ui/handler"
"github.com/ngoduykhanh/wireguard-ui/router"
2020-04-23 13:01:40 +02:00
"github.com/ngoduykhanh/wireguard-ui/util"
"net/http"
2020-06-01 08:03:10 +02:00
"time"
2020-04-18 11:17:49 +02:00
)
2020-06-01 08:03:10 +02:00
var appVersion = "development"
var gitCommit = "N/A"
var gitRef = "N/A"
var buildTime = fmt.Sprintf(time.Now().UTC().Format("01-02-2006 15:04:05"))
2020-04-18 11:17:49 +02:00
func main() {
2020-06-01 08:03:10 +02:00
// print app information
fmt.Println("Wireguard UI")
fmt.Println("App Version\t:", appVersion)
fmt.Println("Git Commit\t:", gitCommit)
fmt.Println("Git Ref\t\t:", gitRef)
fmt.Println("Build Time\t:", buildTime)
fmt.Println("Git Repo\t:", "https://github.com/ngoduykhanh/wireguard-ui")
2020-06-01 10:24:11 +02:00
// set app extra data
extraData := make(map[string]string)
extraData["appVersion"] = appVersion
2020-04-23 13:01:40 +02:00
// initialize DB
err := util.InitDB()
if err != nil {
fmt.Print("Cannot init database: ", err)
}
// create rice box for embedded template
tmplBox := rice.MustFindBox("templates")
// rice file server for assets. "assets" is the folder where the files come from.
assetHandler := http.FileServer(rice.MustFindBox("assets").HTTPBox())
2020-04-23 13:01:40 +02:00
// register routes
2020-06-01 10:24:11 +02:00
app := router.New(tmplBox, extraData)
2020-04-18 11:17:49 +02:00
app.GET("/", handler.WireGuardClients())
2020-04-24 06:22:50 +02:00
app.GET("/login", handler.LoginPage())
2020-04-24 13:14:54 +02:00
app.POST("/login", handler.Login())
app.GET("/logout", handler.Logout())
2020-04-18 11:17:49 +02:00
app.POST("/new-client", handler.NewClient())
app.POST("/update-client", handler.UpdateClient())
app.POST("/client/set-status", handler.SetClientStatus())
2020-04-18 11:17:49 +02:00
app.POST("/remove-client", handler.RemoveClient())
2020-04-25 11:58:14 +02:00
app.GET("/download", handler.DownloadClient())
app.GET("/wg-server", handler.WireGuardServer())
app.POST("wg-server/interfaces", handler.WireGuardServerInterfaces())
app.POST("wg-server/keypair", handler.WireGuardServerKeyPair())
2020-04-20 04:54:41 +02:00
app.GET("/global-settings", handler.GlobalSettings())
app.POST("/global-settings", handler.GlobalSettingSubmit())
app.GET("/api/clients", handler.GetClients())
app.GET("/api/client/:id", handler.GetClient())
app.GET("/api/machine-ips", handler.MachineIPAddresses())
app.GET("/api/suggest-client-ips", handler.SuggestIPAllocation())
app.GET("/api/apply-wg-config", handler.ApplyServerConfig(tmplBox))
// servers other static files
app.GET("/static/*", echo.WrapHandler(http.StripPrefix("/static/", assetHandler)))
2020-04-24 19:33:22 +02:00
app.Logger.Fatal(app.Start("0.0.0.0:5000"))
2020-04-18 11:17:49 +02:00
}