2023-09-09 20:52:50 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
2023-09-09 20:52:50 +00:00
|
|
|
"github.com/joho/godotenv"
|
2023-09-09 20:52:50 +00:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/labstack/echo/v4/middleware"
|
2023-09-09 20:52:50 +00:00
|
|
|
"github.com/namsral/flag"
|
2023-09-09 20:52:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
e *echo.Echo
|
|
|
|
t *Template
|
|
|
|
COMMIT_LONG_SHA string
|
|
|
|
COMMIT_SHORT_SHA string
|
|
|
|
VERSION string
|
2023-09-09 20:52:50 +00:00
|
|
|
PORT int
|
2023-09-09 20:52:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func execCmd(name string, args ...string) string {
|
|
|
|
cmd := exec.Command(name, args...)
|
|
|
|
stdout, err := cmd.Output()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(string(stdout))
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2023-09-09 20:52:50 +00:00
|
|
|
err := godotenv.Load()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error loading .env file")
|
|
|
|
}
|
|
|
|
flag.IntVar(&PORT, "PORT", 4321, "Server port")
|
|
|
|
flag.Parse()
|
2023-09-09 20:52:50 +00:00
|
|
|
e = echo.New()
|
|
|
|
t = &Template{
|
|
|
|
templates: template.Must(template.ParseGlob("template/*.html")),
|
|
|
|
}
|
|
|
|
COMMIT_LONG_SHA = execCmd("git", "rev-parse", "HEAD")
|
|
|
|
COMMIT_SHORT_SHA = execCmd("git", "rev-parse", "--short", "HEAD")
|
|
|
|
VERSION = fmt.Sprintf("v0.0.0+%s", COMMIT_SHORT_SHA)
|
|
|
|
log.Printf("Running commit %s", COMMIT_SHORT_SHA)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
e.Static("/", "public")
|
|
|
|
e.Renderer = t
|
|
|
|
e.GET("/", index)
|
|
|
|
e.GET("/login", login)
|
2023-09-09 20:52:50 +00:00
|
|
|
e.GET("/api/login", verifyLogin)
|
|
|
|
e.GET("/api/session", checkSession)
|
|
|
|
e.POST("/logout", logout)
|
2023-09-09 20:52:50 +00:00
|
|
|
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
|
|
|
|
Format: "${time_custom} ${method} ${uri} ${status}\n",
|
|
|
|
CustomTimeFormat: "2006-01-02 15:04:05.00000-0700",
|
|
|
|
}))
|
2023-09-09 20:52:50 +00:00
|
|
|
e.Use(sessionHandler)
|
2023-09-09 20:52:50 +00:00
|
|
|
e.HTTPErrorHandler = httpErrorHandler
|
2023-09-09 20:52:50 +00:00
|
|
|
err := e.Start(fmt.Sprintf("%s:%d", "127.0.0.1", PORT))
|
2023-09-09 20:52:50 +00:00
|
|
|
if err != http.ErrServerClosed {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|