@@ -23,46 +23,54 @@ import { mergeObjects } from "~/utils/object";
2323
2424export async function create ( formData : FormData ) {
2525 const data = NewServiceSchema . parse ( getData ( formData ) ) ;
26- await db . transaction ( async tx => {
27- const service = await createService ( tx , data ) ;
28- if ( data . kind === "database" ) {
29- const envEntries : [ string , string ] [ ] = [ ] ;
30- const portEntries : [ string , string ] [ ] = [ ] ;
31- if ( data . repo === "postgres" ) {
32- envEntries . push ( [ "POSTGRES_PASSWORD" , crypto . randomUUID ( ) ] ) ;
33- // portEntries.push(["5432", "5432"]);
34- const volume = await createVolume ( tx , { containerPath : "/var/lib/postgresql/data" , serviceId : service . id } ) ;
35- await createDockerVolume ( volume . id ) ;
36- }
37- if ( data . repo === "mysql" ) {
38- envEntries . push ( [ "MYSQL_ROOT_PASSWORD" , "example" ] ) ;
39- portEntries . push ( [ "3306" , "3306" ] ) ;
40- const volume = await createVolume ( tx , { containerPath : "/var/lib/mysql" , serviceId : service . id } ) ;
41- await createDockerVolume ( volume . id ) ;
42- }
43- if ( data . repo === "mongo" ) {
44- envEntries . push ( [ "MONGO_INITDB_ROOT_USERNAME" , "root" ] ) ;
45- envEntries . push ( [ "MONGO_INITDB_ROOT_PASSWORD" , "example" ] ) ;
46- portEntries . push ( [ "27017" , "27017" ] ) ;
47- const volume = await createVolume ( tx , { containerPath : "/data/db" , serviceId : service . id } ) ;
48- await createDockerVolume ( volume . id ) ;
49- }
50- if ( data . repo === "redis" ) {
51- envEntries . push ( [ "REDIS_ARGS" , "--appendonly yes" ] ) ;
52- // portEntries.push(["6379", "6379"]);
53- const volume = await createVolume ( tx , { containerPath : "/data" , serviceId : service . id } ) ;
54- await createDockerVolume ( volume . id ) ;
26+ try {
27+ await db . transaction ( async tx => {
28+ const service = await createService ( tx , data ) ;
29+ if ( data . kind === "database" ) {
30+ const envEntries : [ string , string ] [ ] = [ ] ;
31+ const portEntries : [ string , string ] [ ] = [ ] ;
32+ if ( data . repo === "postgres" ) {
33+ envEntries . push ( [ "POSTGRES_PASSWORD" , crypto . randomUUID ( ) ] ) ;
34+ // portEntries.push(["5432", "5432"]);
35+ const volume = await createVolume ( tx , { containerPath : "/var/lib/postgresql/data" , serviceId : service . id } ) ;
36+ await createDockerVolume ( volume . id ) ;
37+ }
38+ if ( data . repo === "mysql" ) {
39+ envEntries . push ( [ "MYSQL_ROOT_PASSWORD" , "example" ] ) ;
40+ portEntries . push ( [ "3306" , "3306" ] ) ;
41+ const volume = await createVolume ( tx , { containerPath : "/var/lib/mysql" , serviceId : service . id } ) ;
42+ await createDockerVolume ( volume . id ) ;
43+ }
44+ if ( data . repo === "mongo" ) {
45+ envEntries . push ( [ "MONGO_INITDB_ROOT_USERNAME" , "root" ] ) ;
46+ envEntries . push ( [ "MONGO_INITDB_ROOT_PASSWORD" , "example" ] ) ;
47+ portEntries . push ( [ "27017" , "27017" ] ) ;
48+ const volume = await createVolume ( tx , { containerPath : "/data/db" , serviceId : service . id } ) ;
49+ await createDockerVolume ( volume . id ) ;
50+ }
51+ if ( data . repo === "redis" ) {
52+ envEntries . push ( [ "REDIS_ARGS" , "--appendonly yes" ] ) ;
53+ // portEntries.push(["6379", "6379"]);
54+ const volume = await createVolume ( tx , { containerPath : "/data" , serviceId : service . id } ) ;
55+ await createDockerVolume ( volume . id ) ;
56+ }
57+ if ( envEntries . length ) {
58+ await syncEnvironmentVariables ( tx , service . id , Object . fromEntries ( envEntries ) ) ;
59+ }
60+ if ( portEntries . length ) {
61+ await syncPorts ( tx , service . id , Object . fromEntries ( portEntries ) ) ;
62+ }
63+ const network = await createNetwork ( tx , service . id , "provider" ) ;
64+ await createDockerNetwork ( network . id ) ;
5565 }
56- if ( envEntries . length ) {
57- await syncEnvironmentVariables ( tx , service . id , Object . fromEntries ( envEntries ) ) ;
58- }
59- if ( portEntries . length ) {
60- await syncPorts ( tx , service . id , Object . fromEntries ( portEntries ) ) ;
61- }
62- const network = await createNetwork ( tx , service . id , "provider" ) ;
63- await createDockerNetwork ( network . id ) ;
66+ } ) ;
67+ } catch ( error ) {
68+ console . error ( error ) ;
69+ if ( error && typeof error === "object" && "message" in error ) {
70+ console . log ( error . message ) ;
6471 }
65- } ) ;
72+ throw error ;
73+ }
6674 redirect ( "/services" ) ;
6775}
6876
0 commit comments