-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmain.go
140 lines (129 loc) · 3.9 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// (C) Copyright 2014 yum-nginx-api Contributors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"time"
"github.com/FINRAOS/yum-nginx-api/repojson"
routing "github.com/go-ozzo/ozzo-routing"
"github.com/go-ozzo/ozzo-routing/access"
"github.com/go-ozzo/ozzo-routing/content"
"github.com/go-ozzo/ozzo-routing/fault"
"github.com/go-ozzo/ozzo-routing/slash"
"github.com/h2non/filetype"
)
// crRoutine is a simple buffer to not overload the system
// by running too many createrepo system commands, uncompress,
// and sqlite connections at the same time
func crRoutine() {
for {
if crCtr > 0 {
for i := 0; i < maxRetries; i++ {
crExec := exec.Command(crBin, "--update", "--workers", createRepo, uploadDir)
_, err := crExec.Output()
if err != nil {
log.Println("Unable to execute createrepo ", err)
} else {
rJSON, err = repojson.RepoJSON(uploadDir)
if err != nil {
log.Println(err)
}
crCtr--
break
}
time.Sleep(1 * time.Second)
}
}
time.Sleep(3 * time.Second)
}
}
// uploadRoute function for handler /upload
func uploadRoute(c *routing.Context) error {
err := c.Request.ParseMultipartForm(maxLength)
if err != nil {
c.Response.WriteHeader(http.StatusInternalServerError)
_ = c.Write("Upload Failed " + err.Error())
return err
}
file, handler, err := c.Request.FormFile("file")
if err != nil {
c.Response.WriteHeader(http.StatusInternalServerError)
_ = c.Write("Upload Failed " + err.Error())
return err
}
defer file.Close()
filePath := uploadDir + handler.Filename
f, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
c.Response.WriteHeader(http.StatusInternalServerError)
_ = c.Write("Upload Failed " + err.Error())
return err
}
defer f.Close()
_, err = io.Copy(f, file)
if err != nil {
c.Response.WriteHeader(http.StatusInternalServerError)
_ = c.Write("Upload Failed " + err.Error())
return err
}
buf, _ := ioutil.ReadFile(filePath)
if kind, err := filetype.Match(buf); err != nil || kind.MIME.Value != "application/x-rpm" {
err := os.Remove(filePath)
if err != nil {
log.Println("Unable to delete " + filePath)
}
c.Response.WriteHeader(http.StatusUnsupportedMediaType)
return c.Write(handler.Filename + " not RPM")
}
// If not in development mode increment create-repo counter
// for command to be ran by go routine crRoutine
if !devMode {
crCtr++
}
c.Response.WriteHeader(http.StatusAccepted)
return c.Write("Uploaded")
}
// healthRoute function for handler /health
func healthRoute(c *routing.Context) error {
c.Response.Header().Add("Version", commitHash)
return c.Write("OK")
}
// repoRoute function for handler /repo
func repoRoute(c *routing.Context) error {
return c.Write(rJSON)
}
func main() {
if err := configValidate(); err != nil {
log.Fatalln(err.Error())
}
go crRoutine()
rtr := routing.New()
api := rtr.Group("/api",
fault.Recovery(log.Printf),
slash.Remover(http.StatusMovedPermanently),
content.TypeNegotiator(content.JSON))
// Disable logging on health endpoints
api.Get("/health", healthRoute)
api.Use(access.Logger(log.Printf))
api.Post("/upload", uploadRoute)
api.Get("/repo", repoRoute)
http.Handle("/", rtr)
log.Printf("yumapi built-on %s, version %s started on %s \n", builtOn, commitHash, port)
err := http.ListenAndServe(":"+port, nil)
if err != nil {
log.Panicln(err)
}
}