forked from kjelly/resource-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
72 lines (60 loc) · 2.47 KB
/
main.go
File metadata and controls
72 lines (60 loc) · 2.47 KB
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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"os/signal"
_ "runtime"
_ "time"
"github.com/gorilla/mux"
"github.com/kjelly/resource-queue/httpHandler"
"github.com/kjelly/resource-queue/worker"
"github.com/onrik/logrus/filename"
log "github.com/sirupsen/logrus"
ini "gopkg.in/ini.v1"
)
func setRouter(router *mux.Router, handler httpHandler.Handler) {
router.HandleFunc("/"+handler.Version()+"/"+handler.Kind()+"/{request_id}", handler.UpdateProperty).Methods("POST")
router.HandleFunc("/"+handler.Version()+"/"+handler.Kind()+"/{request_id}", handler.GetJobs).Methods("GET").Queries("owner_id", "owner_id").Queries("status", "status")
router.HandleFunc("/"+handler.Version()+"/"+handler.Kind()+"/{request_id}", handler.GetJobs).Methods("GET").Queries("owner_id", "owner_id")
router.HandleFunc("/"+handler.Version()+"/"+handler.Kind()+"/{request_id}", handler.GetJob).Methods("GET")
router.HandleFunc("/"+handler.Version()+"/"+handler.Kind()+"/{request_id}", handler.DeleteJob).Methods("DELETE")
router.HandleFunc("/"+handler.Version()+"/"+handler.Kind()+"/", handler.AddJob).Methods("POST")
router.HandleFunc("/"+handler.Version()+"/"+handler.Kind()+"/", handler.GetJobs).Methods("GET").Queries("owner_id", "owner_id")
router.HandleFunc("/"+handler.Version()+"/"+handler.Kind()+"/", handler.GetJobs).Methods("GET")
router.HandleFunc("/"+handler.Version()+"/"+handler.Kind()+"/{request_id}/test", handler.Test).Methods("GET")
}
func main() {
log.SetLevel(log.DebugLevel)
filenameHook := filename.NewHook()
filenameHook.Field = "line" // Customize source field name
log.AddHook(filenameHook)
var INIPath string
flag.StringVar(&INIPath, "path", "queue.ini", "config path")
flag.Parse()
cfg, err := ini.Load(INIPath)
if err != nil {
log.Warnf("Failed to read config. (%s)", err)
log.Warn("Use the default value to run.")
cfg = ini.Empty()
}
log.Debug("Start")
databaseType := cfg.Section("database").Key("type").MustString("sqlite3")
databaseURI := cfg.Section("database").Key("uri").MustString("test.db")
vHandler := httpHandler.InitVMHandler(databaseType, databaseURI)
VMWorker := worker.InitVMWorker(vHandler.GetQueue())
//go VMWorker.Run()
router := mux.NewRouter()
setRouter(router, vHandler)
srv := &http.Server{Addr: ":8080", Handler: router}
go srv.ListenAndServe()
log.Debug("Listenering")
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
s := <-c
signal.Reset(os.Interrupt)
fmt.Println("Got signal:", s)
VMWorker.Stop()
srv.Shutdown(nil)
}