Skip to content

Commit

Permalink
Merge pull request #93 from ccoverstreet/jabko-micro
Browse files Browse the repository at this point in the history
Jabko micro
  • Loading branch information
ccoverstreet authored Jun 22, 2021
2 parents ddb02a4 + 87121ea commit cbc657c
Show file tree
Hide file tree
Showing 18 changed files with 812 additions and 85 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ github.com/*
tmp
data
tags
log
30 changes: 30 additions & 0 deletions builtin/test/jablkohelpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Jablko Version 0.3.0
// Cale Overstreet
// Jun. 13, 2021

// Helper file for developing Jablko Applications. This file contains
// standardized functions for use in JMODs.

package main

import (
"bytes"
"net/http"
)

func JablkoSaveConfig(corePort string, modPort string, modKey string, config []byte) error {
client := &http.Client{}
reqPath := "http://localhost:" + corePort + "/service/saveConfig"

req, err := http.NewRequest("POST", reqPath, bytes.NewBuffer(config))
if err != nil {
return err
}

req.Header.Add("JMOD-KEY", modKey)
req.Header.Add("JMOD-PORT", modPort)

_, err = client.Do(req)

return err
}
94 changes: 60 additions & 34 deletions builtin/test/testmod.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
Expand All @@ -17,34 +16,57 @@ import (
"github.com/gorilla/websocket"
)

const gDefaultConfig string = `
{
"portUDP": 41111,
"instances": {
"inst0": {
"value": 32
},
"inst99": {
"value": 49
}
}
}
`

// Frontend instances
type testInstance struct {
Value int `json:"value"`
}

type testConfig struct {
type testGlobalConfig struct {
sync.Mutex
PortUDP int `json:"portUDP"`
Instances map[string]testInstance `json:"instances"`
}

//
type stateUDP struct {
sync.Mutex
Data string
}

// Global data
var curConfig testConfig
var curConfig testGlobalConfig
var curStateUDP = stateUDP{sync.Mutex{}, "Startup Message"}
var jablkoCorePort string
var jablkoModPort string
var jablkoModKey string

func main() {
curConfig.Instances = make(map[string]testInstance)
// Grabbing settings from environment variable
jablkoCorePort = os.Getenv("JABLKO_CORE_PORT")
jablkoModPort = os.Getenv("JABLKO_MOD_PORT")
jablkoModKey = os.Getenv("JABLKO_MOD_KEY")

// Pull in config from environment variable and marshal into
// state struct. Load default config and save if value is not
// present
configStr := os.Getenv("JABLKO_MOD_CONFIG")
loadConfig(configStr)

router := mux.NewRouter()

// Required Routes
router.HandleFunc("/webComponent", webComponentHandler) // Route called by Jablko
router.HandleFunc("/instanceData", instanceDataHandler) // Route called by Jablko
Expand All @@ -54,28 +76,39 @@ func main() {
router.HandleFunc("/jmod/getUDPState", UDPStateHandler) // Simple GET for UDP Server State
router.HandleFunc("/jmod/testConfigSave", TestConfigSave)

// Pull in port for running HTTP server that communicates with Jablko
port := os.Getenv("JABLKO_MOD_PORT")
log.Println(port)

jablkoCorePort = os.Getenv("JABLKO_CORE_PORT")
jablkoModPort = os.Getenv("JABLKO_MOD_PORT")
jablkoModKey = os.Getenv("JABLKO_MOD_KEY")

// Pull in config from environment variable and marshal into
// state struct
err := json.Unmarshal([]byte(os.Getenv("JABLKO_MOD_CONFIG")), &curConfig)
if err != nil {
panic(err)
}
log.Println(curConfig)

log.Println(curConfig.PortUDP)
// Start UDP server with in separate go routine
// This server just prints the output and echoes
go UDPServer(curConfig.PortUDP)

log.Println("Starting HTTP server...")
http.ListenAndServe(":"+port, router)
http.ListenAndServe(":"+jablkoModPort, router)
}

func loadConfig(config string) {
// If no config is provided
if len(config) < 3 {
err := json.Unmarshal([]byte(gDefaultConfig), &curConfig)

log.Println(curConfig)

if err != nil {
panic(err)
}

err = JablkoSaveConfig(jablkoCorePort, jablkoModPort, jablkoModKey, []byte(gDefaultConfig))
if err != nil {
panic(err)
}

return
}

err := json.Unmarshal([]byte(config), &curConfig)

if err != nil {
panic(err)
}
}

// The webcomponent handler returns the javascript for a WebComponent
Expand All @@ -97,7 +130,10 @@ func instanceDataHandler(w http.ResponseWriter, r *http.Request) {
curConfig.Lock()
defer curConfig.Unlock()

log.Println(curConfig)
log.Println(curConfig.Instances)
data, err := json.Marshal(curConfig.Instances)
log.Println("ASD", data, err)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Unable to marshal config data")
Expand Down Expand Up @@ -205,17 +241,7 @@ func TestConfigSave(w http.ResponseWriter, r *http.Request) {
return
}

client := &http.Client{}

req, err := http.NewRequest("POST", "http://localhost:"+jablkoCorePort+"/service/saveConfig", bytes.NewBuffer(b))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Couldn't create http request")
return
}

req.Header.Add("JMOD-KEY", jablkoModKey)
req.Header.Add("JMOD-PORT", jablkoModPort)
err = JablkoSaveConfig(jablkoCorePort, jablkoModPort, jablkoModKey, b)

client.Do(req)
log.Println(err)
}
145 changes: 143 additions & 2 deletions core/app/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,22 @@ func (app *JablkoCoreApp) AdminFuncHandler(w http.ResponseWriter, r *http.Reques
vars := mux.Vars(r)

switch vars["func"] {
case "installJMOD":
app.installJMOD(w, r)
case "getJMODData":
app.getJMODData(w, r)
case "startJMOD":
app.startJMOD(w, r)
case "stopJMOD":
app.stopJMOD(w, r)
case "buildJMOD":
app.buildJMOD(w, r)
case "deleteJMOD":
app.deleteJMOD(w, r)
case "applyJMODConfig":
app.applyJMODConfig(w, r)
case "getJMODLog":
app.getJMODLog(w, r)
case "getUserList":
app.getUserList(w, r)
case "createUser":
Expand All @@ -55,6 +63,47 @@ func (app *JablkoCoreApp) AdminFuncHandler(w http.ResponseWriter, r *http.Reques
}
}

func (app *JablkoCoreApp) installJMOD(w http.ResponseWriter, r *http.Request) {
type installData struct {
JMODPath string `json:"jmodPath"`
}

var reqData installData

err := jutil.ParseJSONBody(r.Body, &reqData)
if err != nil {
log.Error().
Err(err).
Msg("Unable to parse JSON body")

w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%v", err)
return
}

err = app.ModM.AddJMOD(reqData.JMODPath, nil)
if err != nil {
log.Error().
Err(err).
Msg("Unable to add JMOD")

w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%v", err)
return
}

err = app.ModM.StartJMOD(reqData.JMODPath)
if err != nil {
log.Error().
Err(err).
Msg("Unable to start JMOD")

w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%v", err)
return
}
}

func (app *JablkoCoreApp) getJMODData(w http.ResponseWriter, r *http.Request) {
data, err := app.ModM.JMODData()
if err != nil {
Expand All @@ -68,8 +117,6 @@ func (app *JablkoCoreApp) getJMODData(w http.ResponseWriter, r *http.Request) {
return
}

log.Printf("%s", data)

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, "%s", data)
Expand Down Expand Up @@ -149,6 +196,67 @@ func (app *JablkoCoreApp) stopJMOD(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Stopped jmod")
}

func (app *JablkoCoreApp) buildJMOD(w http.ResponseWriter, r *http.Request) {
type buildData struct {
JMODName string `json:"jmodName"`
}

var reqData buildData

err := jutil.ParseJSONBody(r.Body, &reqData)
if err != nil {
log.Error().
Err(err).
Msg("Unable to parse body")

w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%v", err)
return
}

err = app.ModM.BuildJMOD(reqData.JMODName)
if err != nil {
log.Error().
Err(err).
Msg("Unable to parse body")

w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%v", err)
return
}
}

func (app *JablkoCoreApp) deleteJMOD(w http.ResponseWriter, r *http.Request) {
type deleteJMODBody struct {
JMODName string `json:"jmodName"`
}

var reqBody deleteJMODBody

err := jutil.ParseJSONBody(r.Body, &reqBody)
if err != nil {
log.Error().
Err(err).
Msg("Unable to parse JSON body for delete JMOD")

w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Unable to parse JSON body: %v", err)
}

err = app.ModM.DeleteJMOD(reqBody.JMODName)
if err != nil {
log.Error().
Err(err).
Msg("Unable to delete JMOD")

w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Unable to delete JMOD: %v", err)
}

w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Deleted JMOD")
}

func (app *JablkoCoreApp) applyJMODConfig(w http.ResponseWriter, r *http.Request) {
type jmodConfig struct {
JMODName string `json:"jmodName"`
Expand Down Expand Up @@ -231,6 +339,39 @@ func (app *JablkoCoreApp) applyJMODConfig(w http.ResponseWriter, r *http.Request
fmt.Fprintf(w, "Applied new config")
}

func (app *JablkoCoreApp) getJMODLog(w http.ResponseWriter, r *http.Request) {
type reqDataStruct struct {
JMODName string `json:"jmodName"`
}

var reqData reqDataStruct

err := jutil.ParseJSONBody(r.Body, &reqData)
if err != nil {
log.Error().
Err(err).
Msg("Unable to parse JSON body")

w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Unable to get JMOD log: %v", err)
return
}

jmodLog, err := app.ModM.GetJMODLog(reqData.JMODName)
if err != nil {
log.Error().
Err(err).
Msg("Unable to retrieve JMOD log")

w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Unable to get JMOD log: %v", err)
return
}

w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "%s", jmodLog)
}

func (app *JablkoCoreApp) getUserList(w http.ResponseWriter, r *http.Request) {
userList := app.DBHandler.GetUserList()

Expand Down
Loading

0 comments on commit cbc657c

Please sign in to comment.