Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Ushieru Kokoran
Ushieru Kokoran

Posted on • Edited on

     

Dart - From scratch to deploy 🚀

Empecemos ✨

Creemos el proyecto desde 0.

Si tienes configurado Flutter, dart ya debería estar en tu PATH también.

$dart create server
Enter fullscreen modeExit fullscreen mode

Obtendrás algo como esto:

voidmain(List<String>arguments){print('Hello world!');}
Enter fullscreen modeExit fullscreen mode

Para este proyecto usaremos el poderoso server deShelf.

$dart pub add shelf
Enter fullscreen modeExit fullscreen mode

Agreguemos algo de código:

import'package:shelf/shelf.dart';import'package:shelf/shelf_io.dart'asshelf_io;voidmain()async{varserver=awaitshelf_io.serve((Requestrequest)=>Response.ok('Hello World!'),'0.0.0.0',8080);print('Serving at http://${server.address.host}:${server.port}');}
Enter fullscreen modeExit fullscreen mode

Entramos alocalhost:8080:
Image description

Preparemos Docker

Esto no es un tutorial de docker así que no me detendré a explicarlo, eso daría para una serie de publicaciones 🤷‍♂️

Dockerfile

FROMdart:stableASbuildWORKDIR /appCOPY pubspec.* ./RUNdart pub getCOPY . .RUNdart pub get--offlineRUNdart compile exe bin/server.dart-o bin/serverFROM scratchCOPY --from=build /runtime/ /COPY --from=build /app/bin/server /app/bin/CMD ["/app/bin/server"]
Enter fullscreen modeExit fullscreen mode

.dockerignore

.dockerignoreresourcesCHANGELOG.mdLICENSEREADME.mdDockerfilebuild/.dart_tool/.git/.github/.gitignore.packages
Enter fullscreen modeExit fullscreen mode

docker-compose.yml

version:'3'services:dart-server:build:.ports:-"8080:8080"
Enter fullscreen modeExit fullscreen mode

Vamos a probarlo 🐛

$docker compose up
Enter fullscreen modeExit fullscreen mode

Probamos de nuevolocalhost:8080 y todo normal.

Preparativos 👩‍🚀👨‍🚀

Para esto usaremosGithub yRailway (Por qué son excelentes herramientas y son gratis).

Railway nos dará un puerto para nuestra aplicación así que hay que hacer ese cambio.

  • Actualizamos nuestro código.
import'dart:io';import'package:shelf/shelf.dart';import'package:shelf/shelf_io.dart'asshelf_io;voidmain()async{varport=int.tryParse(Platform.environment['PORT']??'')??8080;varserver=awaitshelf_io.serve((Requestrequest)=>Response.ok('Hello World!'),'0.0.0.0',port);print('Serving at http://${server.address.host}:${server.port}');}
Enter fullscreen modeExit fullscreen mode
  • Lo subimos aGithub.
  • Inicializamos un nuevo proyecto con Github en Railway.
    Image description

  • Elegimos el repositorio, Railway detectara nuestroDockerfile y empezara a armarlo.
    Image description

  • Al terminar nos imprimirá el log de nuestra aplicación y nos dará un URL.
    Image description

Y listo 🚀🚀

Estamos online.

Te gusto la practica? intentamos con otro lenguaje?
Cuéntame en los comentarios y Happy Hacking 🧑‍💻🎉

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software engineer
  • Location
    Jalisco. México.
  • Education
    Software engineer at UNE
  • Work
    Software engineer at IBM
  • Joined

More fromUshieru Kokoran

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp