Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
temerk1n committed May 10, 2024
1 parent e8a548e commit 845e597
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 82 deletions.
31 changes: 1 addition & 30 deletions example/config.lua
Original file line number Diff line number Diff line change
@@ -1,30 +1 @@
require("controller")

local Routes = {
{
"/hello/:name",
index
}
}

function string.explode(str, delimiter)
assert(type(str) == "string" and type(delimiter) == "string", "invalid arguments")
local o = {}
while true do
local pos1,pos2 = str:find(delimiter)
if not pos1 then
o[#o+1] = str
break
end
o[#o+1],str = str:sub(1,pos1-1),str:sub(pos2+1)
end
return o
end

function Match(route)
for k, v in pairs(Routes) do
if(v[0]) then

end
end
end
Message = "Hello!"
3 changes: 0 additions & 3 deletions example/controller.lua

This file was deleted.

13 changes: 2 additions & 11 deletions example/main.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
require("config")
require "config"

<<<<<<< Updated upstream
function Main(route) --default entry point
local a = string.len(route)
a = a*12/(100-20)^4.2
return tostring("The result is "..a) --returned value must be always a string
end
=======
function Main(route)
local a = #(route)
return tostring("The result is "..a)
return tostring("Request uri is "..route.." "..Message) --returned value must be always a string
end
>>>>>>> Stashed changes
10 changes: 0 additions & 10 deletions internal/exceptions.go

This file was deleted.

13 changes: 0 additions & 13 deletions internal/util.go

This file was deleted.

26 changes: 11 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package main

import (
"errors"
"strings"
"volcanic/internal"
"io/ioutil"
"log"

"github.com/gofiber/fiber/v2"
lua "github.com/yuin/gopher-lua"
Expand All @@ -12,11 +11,13 @@ import (
func main() {
L := lua.NewState()
if err := L.DoFile("main.lua"); err != nil {
internal.Throw(2)
log.Fatal(err)
}
script_content, _ := ioutil.ReadFile("main.lua")
script_string := string(script_content)
entry_point := L.GetGlobal("Main")
if entry_point == lua.LNil {
internal.Throw(3, errors.New("undefined lua entry point"))
log.Fatal("undefined lua entry point")
}
L.Close()
app := fiber.New(fiber.Config{
Expand All @@ -26,21 +27,16 @@ func main() {
app.All("/*", func(c *fiber.Ctx) error {
L := lua.NewState()
defer L.Close()
if err := L.DoFile("main.lua"); err != nil {
if err := L.DoString(script_string); err != nil {
return c.SendString("error!")
}
trim_path := strings.Trim(c.Path(), "/")
res_arr := strings.Split(trim_path, "/")
pass_table := lua.LTable{}
for _, v := range res_arr {
pass_table.Append(lua.LString(string(v)))
}
pass_route := lua.LString(string(c.Path()))
if err := L.CallByParam(lua.P{
Fn: L.GetGlobal("Main"),
NRet: 1,
Protect: true,
}, &pass_table); err != nil {
internal.Throw(3, err)
}, pass_route); err != nil {
log.Fatal(err)
}
ret := L.Get(-1) // returned value
L.Pop(1)
Expand All @@ -50,5 +46,5 @@ func main() {
return c.SendString("error!")
}
})
internal.Startup(app)
app.Listen(":8000")
}

0 comments on commit 845e597

Please sign in to comment.