-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
64 lines (51 loc) · 1.11 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
package main
import (
"fmt"
"net/http"
"os"
"path/filepath"
"shareburn/server/controllers"
"shareburn/server/models"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
func port() string {
port := os.Getenv("SERVE_PORT")
if len(port) == 0 {
port = "8092"
}
return ":" + port
}
func gethost() string {
hostenv := os.Getenv("HOSTNAME")
if len(hostenv) == 0 {
hostname := "http://localhost"
return hostname + port()
} else {
return "https://" + hostenv + port()
}
}
func main() {
enverr := godotenv.Load()
if enverr != nil {
fmt.Println("Failed to load env")
}
mydir, err := os.Getwd()
if err != nil {
fmt.Println(err)
}
r := gin.Default()
r.LoadHTMLFiles(filepath.Join(mydir, "frontend", "index.tmpl"))
r.Static("/static", filepath.Join(mydir, "frontend", "styles"))
models.ConnectDatabase()
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"title": "This string will self immolate",
"host": gethost(),
})
})
r.GET("/s", controllers.GetStrings)
r.GET("/s/:key", controllers.GetString)
r.POST("/s", controllers.PutString)
r.Run(port())
}