aboutsummaryrefslogtreecommitdiff
path: root/crg2.go
blob: 41f7c13f55d7ec3a08c3629f7741ac4e8288310f (plain)
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
// 2019-03-04 16:34 this is better than the previous one, although very clunky. Need to make a new "type Item struct { }" where Item is the object to be changed when you want to have a new object. But the functions should be a minimal example to show how to deal with POST, GET. 
// Reference
//    docs https://godoc.org/github.com/dougblack/sleepy
//    https://github.com/dougblack/sleepy as gitlab.com/bgstack15/sleepy
//    https://gobyexample.com/json
package main

import (
	"net/url"
	"net/http"
	//"github.com/dougblack/sleepy"
	"gitlab.com/bgstack15/sleepy"
	"fmt"
	"encoding/json"
	"strconv"
	"os/exec"
	"io/ioutil"
)

type Item struct{}
type Harbor struct{}
type CertreqResponse struct{
	Csrfile string  `json:"csrfile"`
	Certificatefile string  `json:"certificatefile"`
	Keyfile string `json:"keyfile"`
	Rc int  `json:"rc"`
}

type CertreqContents struct{
	Csr string `json:"csr"`
	Cert string `json:"cert"`
	Key string `json:"key"`
	Rc int `json:"rc"`
	// add chain?
}

type CertreqInput struct{
	Ca string `json:"ca"`
	Pass string `json:"pass"`
	Subject string `json:"subject"`
	Template string `json:"template"`
	User string `json:"user"`
}

// what is passed in, name, and return values
func Certreq_Request(i CertreqInput) (int, CertreqResponse) {
	var o CertreqResponse
	// WORKHERE figure how to sent environment variables for the command.
	crexec := exec.Command("/home/bgstack15/dev/certreq/files/certreq.sh","-c","/home/bgstack15/dev/certreq/files/certreq.conf")
	//execIn, _ := crexec.StdinPipe()
	execOut, _ := crexec.StdoutPipe()
	crexec.Start()
	crexecBytes, _ := ioutil.ReadAll(execOut)
	crexec.Wait()
	fmt.Print(string(crexecBytes))

	o.Rc = 5
	o.Keyfile = "something"
	return 0, o
}

func (certreqweb CertreqInput) Get(values url.Values, headers http.Header) (int, interface{}, http.Header) {
	var i CertreqInput
	var e int = 200
	var r string
	for {
		if values.Get("subject") == "" {
			e = 400
			r = "subject is not defined"
			break
		} else { i.Subject = values.Get("subject") }
		if values.Get("template") == "" {
			fmt.Print("using default template...\n")
		} else { i.Template = values.Get("template") }
		if values.Get("ca") == "" {
			fmt.Print("using default ca...\n")
		} else { i.Ca = values.Get("ca") }
		break
	}

	// check for errors
	if ( e != 200 ) {
		return e, map[string][]string{"errorcode":[]string{strconv.Itoa(e)},"reason":[]string{r}}, http.Header{"Content-type:": {"application/json"}}
	}
	// after all the checks, we should be good to go

	data , _ := json.Marshal(&map[string]interface{}{"input": i})
	var out1, out2 map[string]interface{}
	json.Unmarshal(data,&out1)

	// call function and pass in i
	_ , o := Certreq_Request(i)
	data , _ = json.Marshal(&map[string]interface{}{"output": o})
	json.Unmarshal(data,&out2)

	// debugging output and return values
	fmt.Print("CertreqWeb: ",values,"\n")
	return e, out2, http.Header{"Content-type": {"application/json"}}
}

func (item Item) Post(values url.Values, headers http.Header) (int, interface{}, http.Header) {
	items := []string{"please replace", "with error"}
	data := map[string][]string{"items": items}
	return 200, data, http.Header{"Content-type": {"application/json"}}
}

func (harbor Harbor) Get(values url.Values, headers http.Header) (int, interface{}, http.Header) {
	items := []string{"item1", "HARBORMONSTER"}
	data := map[string][]string{"items": items}
	fmt.Print(values)
	return 200, data, http.Header{"Content-type": {"application/json"}}
}

func (harbor Harbor) Post(values url.Values, headers http.Header) (int, interface{}, http.Header) {
	items := []string{"item1", "posterized"}
	data := map[string][]string{"items": items}
	fmt.Print(values)
	return 200, data, http.Header{"Content-type": {"application/json"}}
}

func main() {
	//item := new(Item)
	//harbor := new(Harbor)

	api := sleepy.NewAPI()
	api.AddResource(new(Item), "/items")
	api.AddResource(new(Harbor), "/harborfreight")
	api.AddResource(new(CertreqInput), "/v1/certreq")
	//api.AddResourceWithWrapper(item, Special, "/item")
	api.StartTLS(8081,"/home/bgstack15/dev/c1/gocert.crt","/home/bgstack15/dev/c1/gocert.key")
}
bgstack15