api works
This commit is contained in:
parent
54eefca3e5
commit
ee988f3157
18
Caddyfile
18
Caddyfile
|
|
@ -1,5 +1,21 @@
|
||||||
|
|
||||||
www.j.de {
|
www.jpmatz.de {
|
||||||
|
proxy link1:80 link/1
|
||||||
|
tls dfasfasfsafdsafsd
|
||||||
|
}
|
||||||
|
www.jpmatz.de {
|
||||||
|
proxy link1:80 link/1
|
||||||
|
tls dfasfasfsafdsafsd
|
||||||
|
}
|
||||||
|
www.jpmatz.de {
|
||||||
|
proxy link1:80 link/1
|
||||||
|
tls dfasfasfsafdsafsd
|
||||||
|
}
|
||||||
|
www.jpmatz.de {
|
||||||
|
proxy link1:80 link/1
|
||||||
|
tls dfasfasfsafdsafsd
|
||||||
|
}
|
||||||
|
www.jpmatz.de {
|
||||||
proxy link1:80 link/1
|
proxy link1:80 link/1
|
||||||
tls dfasfasfsafdsafsd
|
tls dfasfasfsafdsafsd
|
||||||
}
|
}
|
||||||
29
main.go
29
main.go
|
|
@ -3,7 +3,8 @@ package main
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"exec"
|
"os/exec"
|
||||||
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
|
@ -54,10 +55,10 @@ func load() {
|
||||||
func save() {
|
func save() {
|
||||||
jsonString, _ := json.Marshal(sites)
|
jsonString, _ := json.Marshal(sites)
|
||||||
ioutil.WriteFile(saveFile, jsonString, 0777)
|
ioutil.WriteFile(saveFile, jsonString, 0777)
|
||||||
|
saveCaddy()
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveCaddy() {
|
func saveCaddy() {
|
||||||
//var caddyFileString = ""
|
|
||||||
tmpl, _ := template.New("Caddy").Parse(`
|
tmpl, _ := template.New("Caddy").Parse(`
|
||||||
{{.Domain}} {
|
{{.Domain}} {
|
||||||
proxy {{.Source}} {{.Target}}
|
proxy {{.Source}} {{.Target}}
|
||||||
|
|
@ -65,43 +66,44 @@ func saveCaddy() {
|
||||||
}`)
|
}`)
|
||||||
|
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
buf2 := &bytes.Buffer{}
|
|
||||||
|
|
||||||
for _, value := range sites {
|
for _, value := range sites {
|
||||||
tmpl.Execute(buf, value)
|
tmpl.Execute(buf, value)
|
||||||
buf2.WriteString(buf.String())
|
|
||||||
}
|
}
|
||||||
fmt.Println("String: ", buf2.String())
|
|
||||||
|
fmt.Println("String: ", buf.String())
|
||||||
|
|
||||||
f, err := os.OpenFile(saveCaddyFile, os.O_CREATE|os.O_WRONLY, 0777)
|
f, err := os.OpenFile(saveCaddyFile, os.O_CREATE|os.O_WRONLY, 0777)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//clear file before writing
|
||||||
|
f.Truncate(0)
|
||||||
|
f.Seek(0, 0)
|
||||||
// do the actual work
|
// do the actual work
|
||||||
f.WriteString(buf2.String())
|
f.WriteString(buf.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func startCaddy() {
|
func startCaddy() {
|
||||||
cmd := exec.Command("nohup caddy -agree &")
|
cmd := exec.Command("nohup caddy -agree &")
|
||||||
stdout, err := cmd.Output()
|
stdout, err := cmd.Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
Print(string(stdout))
|
fmt.Print(string(stdout))
|
||||||
}
|
}
|
||||||
|
|
||||||
func restartCaddy() {
|
func restartCaddy() {
|
||||||
cmd := exec.Command("pkill -USR1 caddy")
|
cmd := exec.Command("pkill -USR1 caddy")
|
||||||
stdout, err := cmd.Output()
|
stdout, err := cmd.Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
Print(string(stdout))
|
fmt.Print(string(stdout))
|
||||||
}
|
}
|
||||||
|
|
||||||
// site controllers
|
// site controllers
|
||||||
|
|
@ -124,9 +126,8 @@ func updateSite(w http.ResponseWriter, r *http.Request) {
|
||||||
delete(sites, id)
|
delete(sites, id)
|
||||||
var site Site
|
var site Site
|
||||||
_ = json.NewDecoder(r.Body).Decode(&site)
|
_ = json.NewDecoder(r.Body).Decode(&site)
|
||||||
newID := getBiggest(sites) + 1
|
fmt.Print(site)
|
||||||
site.Id = newID
|
sites[id] = site
|
||||||
sites[newID] = site
|
|
||||||
json.NewEncoder(w).Encode(site)
|
json.NewEncoder(w).Encode(site)
|
||||||
save()
|
save()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
{"1":{"id":1,"title":"titrle 1","description":"dewsc1","domain":"www.j.de","source":"link1:80","target":"link/1","email":"dfasfasfsafdsafsd"}}
|
{"1":{"id":1,"title":"title 2","description":"desc 3452345235345","domain":"www.jpmatz.de","source":"link1:80","target":"link/1","email":"dfasfasfsafdsafsd"},"2":{"id":2,"title":"title 2","description":"desc 3452345235345","domain":"www.jpmatz.de","source":"link1:80","target":"link/1","email":"dfasfasfsafdsafsd"},"3":{"id":3,"title":"title 2","description":"desc 3452345235345","domain":"www.jpmatz.de","source":"link1:80","target":"link/1","email":"dfasfasfsafdsafsd"},"4":{"id":4,"title":"title 2","description":"desc 3452345235345","domain":"www.jpmatz.de","source":"link1:80","target":"link/1","email":"dfasfasfsafdsafsd"},"5":{"id":5,"title":"title 2","description":"desc 3452345235345","domain":"www.jpmatz.de","source":"link1:80","target":"link/1","email":"dfasfasfsafdsafsd"}}
|
||||||
Loading…
Reference in New Issue