Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Criando apps robustas com Macaron

Criando apps robustas com Macaron

Um overview pelo Macaron framework

Felipe Oliveira

October 08, 2016
Tweet

More Decks by Felipe Oliveira

Other Decks in Programming

Transcript

  1. 10/8/2016 Criando apps robustas com Macaron http://localhost:3999/macaron_overview/macaron.slide#28 2/30 Sobre mim

    (@_felipeweb) Atualmente trabalho como desenvolvedor Go na Novatrix Lider de desenvolvimento do Mamute Mamute (https://github.com/caelum/mamute) Autor da lib Gopher Utils Gopher Utils (https://github.com/felipeweb/gopher-utils) Autor da lib Osin Mysql Osin Mysql Osin Mysql (https://github.com/felipeweb/osin-mysql) Autor da lib HTML Render HTML Render (https://github.com/felipeweb/htmlrender) Contribuidor do framework Go Macaron Macaron (https://go-macaron.com)
  2. 10/8/2016 Criando apps robustas com Macaron http://localhost:3999/macaron_overview/macaron.slide#28 4/30 Qual era

    a nossa necessidade? Renderizar templates Jade I18n JWT Oauth 2 Con gs baseadas em arquivo Flash messages DB
  3. 10/8/2016 Criando apps robustas com Macaron http://localhost:3999/macaron_overview/macaron.slide#28 7/30 Começando com

    o Macaron p a c k a g e m a i n i m p o r t " g o p k g . i n / m a c a r o n . v 1 " f u n c m a i n ( ) { m : = m a c a r o n . N e w ( ) m . G e t ( " / " , f u n c ( ) s t r i n g { r e t u r n " H e l l o w o r l d ! " } ) m . R u n ( ) }
  4. 10/8/2016 Criando apps robustas com Macaron http://localhost:3999/macaron_overview/macaron.slide#28 8/30 Renderizando Jade

    d o c t y p e h t m l h t m l h e a d m e t a ( c h a r s e t = ' u t f - 8 ' ) m e t a ( h t t p - e q u i v = ' X - U A - C o m p a t i b l e ' , c o n t e n t = ' I E = e d g e ' ) m e t a ( n a m e = ' v i e w p o r t ' , c o n t e n t = ' w i d t h = d e v i c e - w i d t h , i n i t i a l - s c a l e = 1 ' ) t i t l e # { T r ( " a p p " ) } l i n k ( h r e f = ' h t t p s : / / m a x c d n . b o o t s t r a p c d n . c o m / b o o t s t r a p / 3 . 3 . 7 / c s s / b o o t s t r a p . m i n . c s s ' , r e l = ' s t y l e s h e e t ' ) l i n k ( h r e f = ' / c s s / l o g i n . c s s ' , r e l = ' s t y l e s h e e t ' ) . c o n t a i n e r . w r a p p e r f o r m . f o r m - s i g n i n ( a c t i o n = ' / l o g i n ' , m e t h o d = ' p o s t ' , n a m e = ' L o g i n _ F o r m ' ) i f f l a s h . E r r o r M s g . a l e r t . a l e r t - d a n g e r # { f l a s h . E r r o r M s g } h 3 . f o r m - s i g n i n - h e a d i n g # { T r ( " a p p " ) } i n p u t . f o r m - c o n t r o l ( t y p e = ' t e x t ' , n a m e = ' u s e r n a m e ' , p l a c e h o l d e r = ' U s e r n a m e ' , r e q u i r e d = ' ' , a u t i n p u t . f o r m - c o n t r o l ( t y p e = ' p a s s w o r d ' , n a m e = ' p a s s w o r d ' , p l a c e h o l d e r = ' P a s s w o r d ' , r e q u i r e d = ' ' ) b u t t o n . b t n . b t n - l g . b t n - p r i m a r y . b t n - b l o c k ( n a m e = ' s u b m i t ' , t y p e = ' S u b m i t ' ) # { T r ( " s i g n i n " ) } s c r i p t ( s r c = ' h t t p s : / / a j a x . g o o g l e a p i s . c o m / a j a x / l i b s / j q u e r y / 1 . 1 2 . 4 / j q u e r y . m i n . j s ' ) s c r i p t ( s r c = ' h t t p s : / / m a x c d n . b o o t s t r a p c d n . c o m / b o o t s t r a p / 3 . 3 . 7 / j s / b o o t s t r a p . m i n . j s ' )
  5. 10/8/2016 Criando apps robustas com Macaron http://localhost:3999/macaron_overview/macaron.slide#28 9/30 Redenrizando jade

    f u n c m a i n ( ) { m : = m a c a r o n . C l a s s i c ( ) / / r e n d e r h t m l t e m p l a t e s f r o m t e m p l a t e s d i r e c t o r y m . U s e ( j a d e . R e n d e r e r ( ) ) m . G e t ( " / " , f u n c ( r j a d e . R e n d e r ) { r . H T M L ( 2 0 0 , " h e l l o " , m a p [ s t r i n g ] s t r i n g { " f o o " : " b a r " , } ) } ) m . R u n ( ) }
  6. 10/8/2016 Criando apps robustas com Macaron http://localhost:3999/macaron_overview/macaron.slide#28 10/30 I18n i

    m p o r t ( " g i t h u b . c o m / g o - m a c a r o n / i 1 8 n " " g o p k g . i n / m a c a r o n . v 1 " ) f u n c m a i n ( ) { m : = m a c a r o n . C l a s s i c ( ) m . U s e ( i 1 8 n . I 1 8 n ( i 1 8 n . O p t i o n s { L a n g s : [ ] s t r i n g { " e n - U S " , " z h - C N " } , N a m e s : [ ] s t r i n g { " E n g l i s h " , " 简体中文" } , } ) ) m . G e t ( " / " , f u n c ( l o c a l e i 1 8 n . L o c a l e ) s t r i n g { r e t u r n " c u r r e n t l a n g u a g e i s " + l o c a l e . L a n g } ) / / U s e i n h a n d l e r . m . G e t ( " / t r a n s " , f u n c ( c t x * m a c a r o n . C o n t e x t ) s t r i n g { r e t u r n c t x . T r ( " h e l l o % s " , " w o r l d " ) } ) m . R u n ( ) } / / f u n ç ã o d i s p o n i v e l n a v i e w < h 2 > { { . i 1 8 n . T r " h e l l o % s " " w o r l d " } } ! < / h 2 >
  7. 10/8/2016 Criando apps robustas com Macaron http://localhost:3999/macaron_overview/macaron.slide#28 11/30 I18n a

    p p = M á q u i n a t a x i i d = I D a d v _ d e f a u l t = P r o p a g a n d a p a d r ã o I D a d v _ b y _ c a r = P r o p a g a n d a p e l o c a r r o I D b r a n d = M a r c a m o d e l = M o d e l o a n o = A n o g a s = C o m b u s t i v e l p r o p e r t i e s = P r o p r i e d a d e l o c a l = L o c a l d a t a = D a t a r o w s = L i n h a s r e t o r n a d a s u s e r n a m e = U s u á r i o p a s s w o r d = S e n h a s i g n i n = E n t r a r r e q u i r e d = n ã o p o d e s e r v a z i o d a s h = d e v e s e r a l f a v á l i d o o u ( - _ ) c a r a c t e r e s n u m é r i c o s o u t r a ç o . m u s t _ s i z e = d e v e t e r t a m a n h o u n k n o w n = E r r o d e s c o n h e c i d o a u t h _ f a i l = L o g i n o u s e n h a i n c o r r e t o s l o g o u t = S a i r
  8. 10/8/2016 Criando apps robustas com Macaron http://localhost:3999/macaron_overview/macaron.slide#28 12/30 I18n a

    p p = T a x i M a c h i n e i d = I D a d v _ d e f a u l t = A d v e r t i s i n g d e f a u l t I D a d v _ b y _ c a r = A d v e r t i s i n g b y c a r I D b r a n d = B r a n d m o d e l = M o d e l a n o = Y e a r g a s = G a s p r o p e r t i e s = P r o p e r t i e s l o c a l = L o c a l d a t a = D a t e r o w s = D a t a b a s e r o w s u s e r n a m e = U s e r n a m e p a s s w o r d = P a s s w o r d s i g n i n = S i g n i n r e q u i r e d = C a n n o t b e e m p t y d a s h = m u s t b e v a l i d a l p h a o r n u m e r i c o r d a s h ( - _ ) c h a r a c t e r s m u s t _ s i z e = m u s t h a v e s i z e u n k n o w n = U n k n o w n e r r o r l o g i n _ f a i l = I n c o r r e c t l o g i n o r p a s s w o r d l o g o u t = L o g o u t
  9. 10/8/2016 Criando apps robustas com Macaron http://localhost:3999/macaron_overview/macaron.slide#28 14/30 Jade +

    i18n f u n c S e t u p M i d d l e w a r e s ( a p p * m a c a r o n . M a c a r o n ) { a p p . U s e ( i 1 8 n . I 1 8 n ( i 1 8 n . O p t i o n s { D i r e c t o r y : " l o c a l e " , L a n g s : [ ] s t r i n g { " p t - B R " , " e n - U S " } , N a m e s : [ ] s t r i n g { " P r o t u g u ê s d o B r a s i l " , " A m e r i c a n E n g l i s h " } , } ) ) a p p . U s e ( j a d e . R e n d e r e r ( j a d e . O p t i o n s { D i r e c t o r y : " p u b l i c / t e m p l a t e s " , } ) ) } / / h a n d l e r m . G e t ( " / " , f u n c ( r j a d e . R e n d e r , c t x * m a c a r o n . C o n t e x t ) { r . H T M L ( h t t p . S t a t u s O K , " l o g i n " , c t x . D a t a ) } ) / / S e t l a n g u a g e p r o p e r t i e s . l o c a l e : = L o c a l e { i 1 8 n . L o c a l e { l a n g } } c t x . M a p ( l o c a l e ) c t x . L o c a l e = l o c a l e c t x . D a t a [ o p t . T m p l N a m e ] = l o c a l e c t x . D a t a [ " T r " ] = i 1 8 n . T r c t x . D a t a [ " L a n g " ] = l o c a l e . L a n g c t x . D a t a [ " L a n g N a m e " ] = c u r L a n g . N a m e c t x . D a t a [ " A l l L a n g s " ] = a p p e n d ( [ ] L a n g T y p e { c u r L a n g } , r e s t L a n g s . . . ) c t x . D a t a [ " R e s t L a n g s " ] = r e s t L a n g s
  10. 10/8/2016 Criando apps robustas com Macaron http://localhost:3999/macaron_overview/macaron.slide#28 18/30 Jade +

    i18n Para que o Tr seja considerado uma função disponivel na view precisamos passar no momento da con guração do middleware m . U s e ( j a d e . R e n d e r e r ( j a d e . O p t i o n s { F u n c s : [ ] t e m p l a t e . F u n c M a p { A p p H e l p e r s } , / / S p e c i f y h e l p e r f u n c t i o n m a p s f o r t e m p l a t e s t o a c c e s s . } ) )
  11. 10/8/2016 Criando apps robustas com Macaron http://localhost:3999/macaron_overview/macaron.slide#28 20/30 Solução!!! v

    a r c t x * C o n t e x t t y p e C o n t e x t s t r u c t { * m a c a r o n . C o n t e x t r e n d e r j a d e . R e n d e r S e s s i o n s e s s i o n . S t o r e F l a s h * s e s s i o n . F l a s h } f u n c C o n t e x t e r ( ) m a c a r o n . H a n d l e r { r e t u r n f u n c ( c * m a c a r o n . C o n t e x t , r j a d e . R e n d e r , s e s s i o n s e s s i o n . S t o r e , f l a s h * s e s s i o n . F l a s h ) { c t x = & C o n t e x t { C o n t e x t : c , r e n d e r : r , S e s s i o n : s e s s i o n , F l a s h : f l a s h , } c . M a p ( c t x ) } } f u n c ( c t x * C o n t e x t ) H T M L ( s t a t u s i n t , n a m e s t r i n g ) { c t x . r e n d e r . H T M L ( s t a t u s , n a m e , c t x . D a t a ) } f u n c I 1 8 n ( k e y s t r i n g ) s t r i n g { r e t u r n c t x . T r ( k e y ) }
  12. 10/8/2016 Criando apps robustas com Macaron http://localhost:3999/macaron_overview/macaron.slide#28 21/30 Suporte a

    arquivo de con gração nativamente v a r C f g * i n i . F i l e f u n c i n i t ( ) { v a r e r r e r r o r C f g , e r r = m a c a r o n . S e t C o n f i g ( " c o n f / a p p . i n i " ) i f e r r ! = n i l { p a n i c ( e r r ) } } / / U s a n d o o v a l o r C f g . S e c t i o n ( " " ) . K e y ( " d b _ t y p e " ) . V a l u e ( )
  13. 10/8/2016 Criando apps robustas com Macaron http://localhost:3999/macaron_overview/macaron.slide#28 22/30 Cache MySQL

    Postgres Redis i m p o r t ( " g i t h u b . c o m / g o - m a c a r o n / c a c h e " " g o p k g . i n / m a c a r o n . v 1 " ) f u n c m a i n ( ) { m : = m a c a r o n . C l a s s i c ( ) m . U s e ( c a c h e . C a c h e r ( ) ) m . G e t ( " / " , f u n c ( c c a c h e . C a c h e ) s t r i n g { c . P u t ( " c a c h e " , " c a c h e m i d d l e w a r e " , 1 2 0 ) r e t u r n c . G e t ( " c a c h e " ) } ) m . R u n ( ) }
  14. 10/8/2016 Criando apps robustas com Macaron http://localhost:3999/macaron_overview/macaron.slide#28 24/30 Login social

    m . U s e ( o a u t h 2 . G o o g l e ( & g o a u t h 2 . C o n f i g { C l i e n t I D : " c l i e n t _ i d " , C l i e n t S e c r e t : " c l i e n t _ s e c r e t " , S c o p e s : [ ] s t r i n g { " h t t p s : / / w w w . g o o g l e a p i s . c o m / a u t h / d r i v e " } , R e d i r e c t U R L : " r e d i r e c t _ u r l " , } , ) ) / / U R L s u s a d a s p e l o m i d d l e w a r e c o n s t ( K E Y _ T O K E N = " o a u t h 2 _ t o k e n " K E Y _ N E X T _ P A G E = " n e x t " ) v a r ( / / P a t h L o g i n i s t h e p a t h t o h a n d l e O A u t h 2 . 0 l o g i n s . P a t h L o g i n = " / l o g i n " / / P a t h L o g o u t i s t h e p a t h t o h a n d l e O A u t h 2 . 0 l o g o u t s . P a t h L o g o u t = " / l o g o u t " / / P a t h C a l l b a c k i s t h e p a t h t o h a n d l e c a l l b a c k f r o m O A u t h 2 . 0 b a c k e n d / / t o e x c h a n g e c r e d e n t i a l s . P a t h C a l l b a c k = " / o a u t h 2 c a l l b a c k " / / P a t h E r r o r i s t h e p a t h t o h a n d l e e r r o r c a s e s . P a t h E r r o r = " / o a u t h 2 e r r o r " )
  15. 10/8/2016 Criando apps robustas com Macaron http://localhost:3999/macaron_overview/macaron.slide#28 25/30 Existem varios

    middlewares disponiveis além desses :) Binding (https://github.com/go-macaron/binding) Toolbox (https://github.com/go-macaron/toolbox) Web sockets (https://github.com/go-macaron/sockets) CSRF (https://github.com/go-macaron/csrf)
  16. 10/8/2016 Criando apps robustas com Macaron http://localhost:3999/macaron_overview/macaron.slide#28 26/30 Existem varios

    middlewares disponiveis além desses :) GZIP (https://github.com/go-macaron/gzip) Bindata (https://github.com/go-macaron/bindata) Basic auth (https://github.com/go-macaron/auth) Switcher (https://github.com/go-macaron/switcher)
  17. 10/8/2016 Criando apps robustas com Macaron http://localhost:3999/macaron_overview/macaron.slide#28 27/30 Existem varios

    middlewares disponiveis além desses :) Method (https://github.com/go-macaron/method) Pongo 2 (https://github.com/go-macaron/pongo2)
  18. 10/8/2016 Criando apps robustas com Macaron http://localhost:3999/macaron_overview/macaron.slide#28 28/30 Mas e

    JWT Oauth server e DB? DB sqlx (https://github.com/jmoiron/sqlx) Oauth Server Parte de crypto e cipher da stdlib JWT jwt-go (https://github.com/dgrijalva/jwt-go)
  19. 10/8/2016 Criando apps robustas com Macaron http://localhost:3999/macaron_overview/macaron.slide#28 29/30 Thank you

    Felipe Paes de Oliveira [email protected] (mailto:[email protected]) https://www.felipeweb.net.br (https://www.felipeweb.net.br) https://github.com/felipeweb (https://github.com/felipeweb) @_felipeweb (http://twitter.com/_felipeweb) Je Prestes @je prestes (http://twitter.com/je prestes)