-
Notifications
You must be signed in to change notification settings - Fork 1
/
router.go
90 lines (70 loc) · 1.6 KB
/
router.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
package main
import (
"net/http"
"os"
"github.com/gin-gonic/gin"
h "github.com/thbkrkr/go-apish/handlers"
m "github.com/thbkrkr/go-apish/middlewares"
)
var basicAuthUser = "zuperadmin"
func Router() *gin.Engine {
router := gin.Default()
router.Use(m.CORSMiddleware())
// Default routes
router.GET("/", index)
router.GET("/favicon.ico", favicon)
// Authentication
authorized := router.Group("/")
if *password != "" {
authorized = router.Group("/", m.AuthMiddleware(
*apiKey,
gin.Accounts{
basicAuthUser: *password,
},
))
}
// Version (commit and date)
authorized.GET("/version", version)
lsHandler := &h.LsHandler{ApiDir: apiDir}
execHandler := &h.ExecHandler{ApiDir: apiDir}
// List resources
authorized.GET("/ls", func(c *gin.Context) {
lsHandler.ListResources(c)
})
// API propulsed by shell scripts
authorized.GET("/api/*path", execHandler.ExecScript)
authorized.POST("/api/*path", execHandler.PostExecScript)
authorized.POST("/docker", h.DockerRun)
// Static files
authorized.Static("/s/", *apiDir+"/_static")
return router
}
/** Base routes */
func indexExists() bool {
if _, err := os.Stat(*apiDir + "/_static/index.html"); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
func index(c *gin.Context) {
if indexExists() {
c.Redirect(http.StatusMovedPermanently, "/s")
} else {
c.JSON(200, gin.H{
"ok": true,
"status": 200,
"name": "go-apish",
})
}
}
func favicon(c *gin.Context) {
c.JSON(200, nil)
}
func version(c *gin.Context) {
c.JSON(200, gin.H{
"git_commit": gitCommit,
"build_date": buildDate,
})
}