100行代码构建自己的html服务器

php -S 0.0.0.0:80

加了几行代码 可以支持预览md了

open Lwt
open Cohttp
open Cohttp_lwt_unix
open Omd

let root = "~/server"

let read_file file_name =
  let input = open_in file_name in
  let content = ref "" in
  try
    while true do
    let line =input_line input ^ "\n"  in
      content := (!content) ^ line;
    done;
    !content
  with End_of_file ->
    close_in input ;
    !content

let is_end_with path suf =
  let len_p = String.length path in
  let len_s = String.length suf in
  if len_p < len_s
  then false
  else
    (String.sub path (len_p - len_s) len_s) = suf

let is_md_file path = is_end_with path ".md"

let is_html_file path = is_end_with path ".html"

let html_to_md_name root_path =
  let len = String.length root_path in
  if is_html_file root_path
  then (String.sub root_path 0 (len - 5) ) ^ ".md"
  else
    root_path

let md_to_html md_file_name =
  let md_content = read_file md_file_name in
  let my_temp_head = read_file (root ^ "/md_head.html") in
  let my_temp_tail = read_file (root ^ "/md_tail.html") in
  let omd_md = Omd.of_string md_content in
  let omd_html = Omd.to_html omd_md in
  my_temp_head ^ omd_html ^ my_temp_tail

let file_cache = Hashtbl.create 100

let file_mtime file_name =
  let file_desc = Unix.openfile file_name [O_RDONLY] 7 in
  let file_state = Unix.fstat file_desc in
  Unix.close file_desc ;
  string_of_float file_state.st_mtime

let check_file path =
  let root_path = root ^ "/root" ^ path in
  try
    let file_desc = Unix.openfile root_path [O_RDONLY] 4 in
    Unix.close  file_desc ;
    root_path
  with Unix.Unix_error (_, _ ,_) ->
    html_to_md_name root_path ;;

let chong_res path =
  let root_path = check_file path in
  let cache_key = root_path ^ file_mtime root_path in
  try
    Printf.printf "%s" root_path;
    Hashtbl.find file_cache cache_key
  with Not_found ->
    if is_md_file root_path  then
      let html_of_md = md_to_html root_path in
      Hashtbl.add  file_cache cache_key html_of_md;
      html_of_md
    else
      let file_content = read_file root_path in
      Hashtbl.add  file_cache cache_key file_content;
      file_content

let server =
  let callback _conn req body =
    let uri = req |> Request.uri |> Uri.to_string in
    let path = req |> Request.uri |> Uri.path in
    let meth = req |> Request.meth |> Code.string_of_method in
    let headers = req |> Request.headers |> Header.to_string in
    body |> Cohttp_lwt.Body.to_string >|=
      (fun body ->
        (Printf.sprintf "Uri: %s\nMethod: %s\nHeaders\nHeaders: %s\nBody: %s"
         uri meth headers body))
    >>= (fun body -> Server.respond_string ~status:`OK
                       ~body:(chong_res path) ())
  in
  Server.create
    ~mode:(`TCP (`Port 8000))
    (Server.make ~callback ())

let () = ignore (Lwt_main.run server)

一行构建http服务器怎么样。

python -m SimpleHTTPServer 9999
1 个赞

要能看md文件的

用grip可以

pip install grip
grip . 8888
1 个赞

对 grip 我还有问题

能看 md 了 还能看pdf

https://video.h5.weibo.cn/1034:4414238224010555/4414241614174072

除了浪费时间还有啥, 你吧文件或者HTML 上传到 OSS 或者cdn, 给对方一个链接,既便宜 又方便快速, 性能还好。 现在都2091年了

@zhouchongzxc 指的应该是在一个大型的局域网内分享文件吧?(公司内?)这样的话感觉没有必要上传OSS或者cdn, 内网搞定就没必要外网了,毕竟外网在怎么弄,速度也没有内网快。我建的apache服务器,内网电脑从另一台电脑下载几个G的文件也几分钟就搞定了

你都用emacs了 还想着不浪费时间?

看了一圈回复没说nginx的

结合着 webslides ,可以用幻灯片的方式看文档了!

再实现一个类似于anki的功能就够用了

100行的代码在哪里呢,

作为 emacs 用户,为啥不用 simple-httpd

在第九楼

我希望能够在不打开emacs的时候能继续看网页

改了一下 css-mode 的 company

(with-eval-after-load "css-mode"

(defun css--complete-property-value ()
  "Complete property value at point."
  (let ((property
         (save-excursion
           (re-search-backward "[:;][^/]" (line-beginning-position) t)
           (when (eq (char-after) ?:)
             (let ((property-end (point)))
               (skip-chars-backward "-[:alnum:]")
               (let ((prop (buffer-substring (point) property-end)))
                 (car (member prop css-property-ids))))))))
    (when property
      (let ((end (point)))
        (save-excursion
          (skip-chars-backward "[:graph:]")
          (list (point) end
                (append '("inherit" "initial" "unset")
                        (css--property-values property))))))))

(defconst css-property-ids-chong
  (mapcar (lambda (id) (concat id ": ")) css-property-ids))

(defun css--complete-property ()
  "Complete property at point."
  (save-excursion
    (let ((pos (point)))
      (skip-chars-backward "-[:alnum:]")
      (let ((start (point)))
        (skip-chars-backward " \t\r\n")
        (when (memq (char-before) '(?\{ ?\;))
          (list start pos css-property-ids-chong))))))
)

用了半天的时间 加了个颜色

源码有点长 改动的地方有点多 调试macro的时间最长

css-mode对company-capf的支持挺好的

要想让company不删除你的属性 需要在backend中添加 pre-render 选项

奇怪的是 pre-render 在2015年就有了 可连个文档都没有找到

想给颜色加上个排序 发现 不是那么简单

排序可以了

nginx用作代理服务器不错,网站服务器的话感觉还是apache更好

目前还是apache的天下,不过nginx配置简单,用习惯了。

你会PR到 company-mode 么?