-
Notifications
You must be signed in to change notification settings - Fork 0
/
nses.go
110 lines (99 loc) · 2.87 KB
/
nses.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
package main
import (
"context"
"database/sql"
"fmt"
"net/http"
"os"
"time"
"github.com/ecyshor/nses/internal"
"github.com/golang/protobuf/jsonpb"
"github.com/gorilla/mux"
"github.com/mattes/migrate"
"github.com/mattes/migrate/database/postgres"
_ "github.com/mattes/migrate/source/file"
log "github.com/sirupsen/logrus"
"github.com/golang/protobuf/proto"
)
func main() {
log.SetLevel(log.DebugLevel)
/*if status, err := strconv.ParseBool(getEnv("DEBUG", "false")); err != nil && status {
}*/
nsesDb := getEnv("NSES_DB_DB", "nses")
d, err := sql.Open("postgres", fmt.Sprintf("dbname=%s user=%s password=%s host=%s sslmode=disable",
nsesDb, getEnv("NSES_DB_USER", "nses"), getEnv("NSES_DB_PASSWORD", "superpassword"),
getEnv("NSES_DB_HOST", "localhost")))
handleFailure(err)
internal.Db = d
driver, err := postgres.WithInstance(d, &postgres.Config{DatabaseName: nsesDb})
if err != nil {
log.Panic("Could not create driver instance", err)
}
m, err := migrate.NewWithDatabaseInstance(
"file://migrations",
nsesDb, driver)
if err != nil {
log.Fatal("Could not initialize migrations", err)
}
err = m.Up()
if err != nil && err != migrate.ErrNoChange {
log.Fatal("Could not run migrations ", err)
}
r := mux.NewRouter()
r.HandleFunc("/health", func(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(http.StatusOK)
})
r.HandleFunc("/templates", internal.TemplateHandler).Methods("PUT")
r.PathPrefix("/templates/{template}/jobs").Handler(http.HandlerFunc(internal.JobHandler)).Methods("POST")
http.Handle("/", r)
log.Info("Migrated nses, binding and starting.")
go internal.Start()
srv := &http.Server{
Handler: r,
Addr: ":8000",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}
// GRPC SERVER
type GrpcNsesServer struct {
}
var marshaller = &jsonpb.Marshaler{EnumsAsInts: true}
func (s *GrpcNsesServer) CreateTemplate(ctx context.Context, template *JobTemplate) (*JobTemplate, error) {
var message proto.Message
var jobType internal.JobType
if template.GetLambdaProperties() != nil {
message = template.GetLambdaProperties()
jobType = internal.AwsLambda
}
if template.GetLambdaProperties() != nil {
message = template.GetHttpProperties()
jobType = internal.Http
}
value, e := marshaller.MarshalToString(message)
if e != nil {
return nil, e
}
jobTemplate, err := internal.CreateTemplate(&internal.JobTemplate{Type: jobType, Props: []byte(value)})
if err != nil {
log.Error("could not create template", err)
return nil, err
}
template.Id = jobTemplate.Id.String()
return template, nil
}
func (s *GrpcNsesServer) CreateJob(context.Context, *Job) (*Job, error) {
return nil, nil
}
func handleFailure(e error) {
if e != nil {
panic(e)
}
}
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}