-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
71 lines (60 loc) · 1.12 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
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/ieee0824/getenv"
"io/ioutil"
"os"
"time"
)
var (
buffer []byte
killer = make(chan bool)
)
func open(ctx *gin.Context) {
}
func wq(ctx *gin.Context) {
if err := ioutil.WriteFile(os.Args[1], buffer, 0644); err != nil {
panic(err)
}
fmt.Fprint(ctx.Writer, string(buffer))
killer <- true
}
func root(ctx *gin.Context) {
fmt.Fprint(ctx.Writer, string(buffer))
}
func edit(ctx *gin.Context) {
body, err := ioutil.ReadAll(ctx.Request.Body)
if err != nil {
panic(err)
}
buffer = body
}
func main() {
if getenv.Bool("FAULT") {
os.Exit(1)
}
go func() {
<-killer
time.Sleep(500 * time.Millisecond)
os.Exit(0)
}()
var err error
if len(os.Args) != 2 {
panic(fmt.Errorf("file name not set"))
}
buffer, err = ioutil.ReadFile(os.Args[1])
if err != nil {
panic(err)
}
router := gin.Default()
router.GET("/", root)
router.POST("/", root)
router.POST("/o", open)
router.POST("/wq", wq)
router.GET("/wq", wq)
router.POST("/e", edit)
if err := router.Run(fmt.Sprintf(":%d", getenv.Int("EDITOR_PORT", 8080))); err != nil {
panic(err)
}
}