main.v 737 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import json
  2. import syou.picoev
  3. import syou.picohttpparser as hp
  4. struct Message {
  5. message string
  6. }
  7. [inline]
  8. fn json_response() string {
  9. msg := Message{
  10. message: 'Hello, World!'
  11. }
  12. return json.encode(msg)
  13. }
  14. [inline]
  15. fn hello_response() string {
  16. return 'Hello, World!'
  17. }
  18. pub fn callback(req hp.Request, res mut hp.Response) {
  19. if hp.cmpn(req.method, 'GET ', 4) {
  20. if hp.cmpn(req.path, '/plaintext', 10) {
  21. res.http_ok().header_server().header_date().plain().body(hello_response())
  22. }
  23. else if hp.cmpn(req.path, '/json', 5) {
  24. res.http_ok().header_server().header_date().json().body(json_response())
  25. }
  26. else {
  27. res.http_404()
  28. }
  29. }
  30. else {
  31. res.http_405()
  32. }
  33. }
  34. pub fn main() {
  35. picoev.new(8088, &callback).serve()
  36. }