router.ex 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. defmodule KomutanWeb.Router do
  2. use Plug.Router
  3. use Plug.Debugger
  4. use Eml.HTML
  5. plug Plug.Static, at: "/doc", from: "doc"
  6. plug Plug.Static, at: "/static/css", from: "priv/static/css"
  7. #plug(Plug.Logger, log: :debug)
  8. plug(:match)
  9. plug(:dispatch)
  10. get "/" do
  11. conn
  12. |> put_resp_header("content-type", "text/html; charset=utf-8")
  13. |> send_resp(200, "Milis Web Tabanlı Sistem Yönetim Paneli: Komutan")
  14. end
  15. get("/servislerE", do: send_resp(conn, 200, servis_list()))
  16. get("/yardim", do: send_resp(conn, 200, "yardım"))
  17. get "/merhaba" do
  18. conn
  19. |> put_resp_header("content-type", "text/html; charset=utf-8")
  20. |> Plug.Conn.send_file(200, "priv/static/html/b.html")
  21. end
  22. get "/sistem/:bilgi" do
  23. conn
  24. |> put_resp_header("content-type", "text/html; charset=utf-8")
  25. |> send_resp(200, "<meta http-equiv='refresh' content='4' /> <p> #{Sistem.bilgi}")
  26. end
  27. get "/sistemA" do
  28. conn
  29. |> put_resp_header("content-type", "text/html; charset=utf-8")
  30. |> send_resp(200, "<meta http-equiv='refresh' content='4' /> <p> #{Sistem.aktif_bilgi}")
  31. end
  32. get "/surecler" do
  33. conn
  34. |> put_resp_header("content-type", "text/html; charset=utf-8")
  35. |> send_resp(200, "<meta http-equiv='refresh' content='4' /> <p> #{sistem_surec()}")
  36. end
  37. get "/servisler" do
  38. conn |> servisler_page
  39. end
  40. def redirect(conn,url) do
  41. html = Plug.HTML.html_escape(url)
  42. body = "<html><body>You are being <a href=\"#{html}\">redirected</a>.</body></html>"
  43. conn
  44. |> put_resp_header("location", url)
  45. |> Plug.Conn.send_resp(200, body)
  46. end
  47. def servisler_page(conn) do
  48. conn
  49. |> put_resp_header("content-type", "text/html; charset=utf-8")
  50. |> send_resp(200, servis_list() |> servisler_eml)
  51. end
  52. def servisler_eml(servisler) do
  53. html do
  54. #meta ["http-equiv": "refresh", content: "10"]
  55. link rel: "stylesheet", href: "/static/css/bootstrap.min.css"
  56. h2 "servisler"
  57. div class: "servisler" do
  58. table border: "1", class: "table-md table-dark table-hover" do
  59. thead class: "thead-dark" do
  60. tr do
  61. th "servis adı"
  62. th "durum"
  63. end
  64. end
  65. tbody do
  66. Enum.map(servisler, fn(x) ->
  67. tr do
  68. td a [href: "/servisler/" <> x], x
  69. case Servis.status(x) do
  70. true -> td([class: "bg-success"], a([ href: "/servisler/dur/" <> x , class: "text-dark"], "aktif"))
  71. false -> td([class: "bg-danger"], a([ href: "/servisler/basla/" <> x, class: "text-dark"], "pasif"))
  72. _ -> td [class: "bg-warning"], "hata"
  73. end
  74. end
  75. end)
  76. end
  77. end
  78. end
  79. end |> Eml.compile
  80. end
  81. def mytd() do
  82. end
  83. # Use a variable in the route
  84. get "/servisler/:service" do
  85. sb=Servis.info(service)
  86. conn
  87. |> put_resp_header("content-type", "text/html; charset=utf-8")
  88. |> send_resp(200, "#{service} #{sb} servisi bilgisi gösterilir")
  89. end
  90. get "/servisler/:oper/:servis" do
  91. case oper do
  92. "basla" ->
  93. case Servis.status(servis) do
  94. true -> conn |> servisler_page
  95. false -> servis |> Servis.start
  96. _ -> "hata"
  97. end
  98. "dur" ->
  99. case Servis.status(servis) do
  100. true -> servis |> Servis.stop
  101. false -> conn |> servisler_page
  102. _ -> "hata"
  103. end
  104. _ -> "hatalı"
  105. end
  106. conn
  107. |> put_resp_header("content-type", "text/html; charset=utf-8")
  108. |> send_resp(200, "<meta http-equiv='refresh' content='1' /> <p> #{servis} servisine #{oper} işlemi uygulandı.")
  109. end
  110. match(_, do: send_resp(conn, 404, "404 Bulunamadı!"))
  111. def servis_oper(servis,oper) do
  112. oper_type= case oper do
  113. "basla" -> "start"
  114. "dur" -> "stop"
  115. _ -> "hata"
  116. end
  117. case System.cmd("servis",[servis,oper_type]) do
  118. {_,0} -> "tamam"
  119. {_,1} -> "hata"
  120. end
  121. end
  122. def servis_list() do
  123. #servisler=Enum.reduce(File.ls!("/etc/init.d/"), fn(x, acc) -> "<span>#{x}</span>" <> "<p>" <> "<span>#{acc}</span>" end)
  124. #["<html>",servisler,"<html>"]
  125. #|> Enum.join("\n")
  126. {islev, 0}= __ENV__.function
  127. betik= [Application.get_env(:komutan, :betikyol), islev]
  128. |> Enum.join("/")
  129. case System.cmd("sh",[betik]) do
  130. {_, 127} -> []
  131. {list, 0} -> list |> String.split("\n", trim: true)
  132. end
  133. end
  134. def sistem_surec, do: :os.cmd('echo q | htop | aha --black --line-fix')
  135. end